Skip to content

fix(s02): handle Anthropic SDK content blocks in normalize_messages#207

Open
wadeKeith wants to merge 1 commit intoshareAI-lab:mainfrom
wadeKeith:fix/s02-tool-use-normalize-messages
Open

fix(s02): handle Anthropic SDK content blocks in normalize_messages#207
wadeKeith wants to merge 1 commit intoshareAI-lab:mainfrom
wadeKeith:fix/s02-tool-use-normalize-messages

Conversation

@wadeKeith
Copy link
Copy Markdown

Problem

s02_tool_use.py crashes with:

anthropic.BadRequestError: 400 - tool result's tool_use_id not found

Root Cause

normalize_messages() uses isinstance(block, dict) to filter content blocks, but the Anthropic SDK stores response.content as pydantic model objects (ToolUseBlock, TextBlock), not plain dicts. This silently drops all tool_use blocks during normalization, so the subsequent tool_result references an id the API never saw.

Fix

Added a _block_to_dict() helper that converts SDK objects to plain dicts via model_dump() (with vars() fallback), and removed the isinstance(block, dict) guard so all content blocks are properly serialized.

Changes

  • agents/s02_tool_use.py: +13 / -3 lines
    • New _block_to_dict(block) function
    • Updated list comprehension in normalize_messages() to use it

Tested locally — the agent loop now completes tool calls without errors.

The original normalize_messages() used isinstance(block, dict) to filter
content blocks, but Anthropic SDK returns pydantic model objects (e.g.
ToolUseBlock, TextBlock), not plain dicts. This caused tool_use blocks
to be silently dropped, leading to 'tool_result tool_use_id not found'
errors (400) on the next API call.

Added _block_to_dict() helper that converts SDK objects via model_dump()
or vars() fallback, and removed the isinstance(block, dict) guard so
all content blocks are properly serialized.
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 10, 2026

@wadeKeith is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

@wadeKeith wadeKeith mentioned this pull request Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant