Skip to content

fix(aws): flatten tool blocks when toolConfig is omitted#5850

Open
u9g wants to merge 3 commits into
mainfrom
fix/aws-bedrock-tool-choice-none
Open

fix(aws): flatten tool blocks when toolConfig is omitted#5850
u9g wants to merge 3 commits into
mainfrom
fix/aws-bedrock-tool-choice-none

Conversation

@u9g
Copy link
Copy Markdown
Contributor

@u9g u9g commented May 26, 2026

Summary

  • Fixes #5589: AWS Bedrock LLM raises ValidationException when tool_choice=\"none\" is requested while the chat context contains toolUse/toolResult blocks.
  • Drive-by: removes unreachable if self._tool_call_id is None branch in _parse_chunk.

Root cause

Bedrock Converse's ToolChoice union only accepts auto / any / tool — there is no none. Two facts collide:

  1. To request "no tools this turn," we'd want to omit toolConfig entirely.

  2. Bedrock rejects any request whose messages contain toolUse or toolResult blocks unless toolConfig is also present:

    ValidationException: The toolConfig field must be defined when using toolUse and toolResult content blocks.
    

This bites every agent handoff that returns (NewAgent(), \"string\"): the draining old agent is forced to generate the string reply with tool_choice=\"none\" while the just-completed tool call/result blocks are still in chat history.

Fix

Follow the approach landed in langchain-ai/langchain-aws#595: when toolConfig is going to be omitted (either because tool_choice=\"none\" or because no tools are provided), rewrite any toolUse / toolResult content blocks in the outgoing messages as plain text ([Called <name> with <args>], [Tool output: ...]). Bedrock no longer sees structured tool blocks, so the validation error disappears, and because no tools list is sent the model genuinely cannot call a tool — no reliance on downstream filtering to suppress unwanted tool calls.

Test plan

  • Manually reproduced via the basic_agent example with an (Agent, str) handoff tool against us.anthropic.claude-sonnet-4-6; pre-fix request fails with ValidationException, post-fix the handoff completes cleanly.
  • Unit-level smoke test of _flatten_tool_blocks confirms toolUse and toolResult blocks are rewritten to text blocks while plain-text content passes through unchanged.

@chenghao-mou chenghao-mou requested a review from a team May 26, 2026 14:46
u9g added 2 commits May 26, 2026 10:47
AWS Bedrock's Converse API rejects requests with ValidationException
when messages contain toolUse/toolResult blocks but toolConfig is
missing. Previously, tool_choice="none" caused _get_tool_config() to
return None entirely, breaking any draining agent handoff that
returned (NewAgent, str) (issue #5589).

Bedrock has no native "none" choice (only auto/any/tool), so emit
toolConfig.tools and let toolChoice default to auto. Upstream filter
in voice/generation.py:545 already discards any tool calls the model
emits when tool_choice="none".
`if self._tool_call_id is None` was nested inside `if self._tool_call_id:`,
so the inner branch could never run.
@u9g u9g force-pushed the fix/aws-bedrock-tool-choice-none branch from f13e395 to b163c02 Compare May 26, 2026 14:47
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

When tool_choice="none" (or tools is empty) but chat history contains
toolUse/toolResult blocks, Bedrock Converse rejects the request because
toolConfig is required whenever tool blocks appear in messages. Switch
to the langchain-aws approach (PR #595): drop toolConfig entirely and
rewrite tool blocks as plain text. The model genuinely cannot call a
tool (no tools list is sent) but still sees what happened on prior
turns.
@u9g u9g changed the title fix(aws): send toolConfig when tool_choice="none" with tool history fix(aws): flatten tool blocks when toolConfig is omitted May 26, 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.

AWS Bedrock LLM Plugin raises ValidationException when tool_choice=None

2 participants