fix(bedrock): fall back to toolChoice.auto in structured_output for unsupported models#1887
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…odels that don't support toolChoice.any
Some Bedrock models (e.g., Llama) reject the toolConfig.toolChoice.any
field with a ValidationException. When structured_output() forces
tool_choice={'any': {}} to guarantee tool invocation, models that don't
support this field fail with:
ValidationException: This model doesn't support the
toolConfig.toolChoice.any field.
Fix: catch ClientError mentioning 'toolChoice' and retry the stream
with tool_choice={'auto': {}}, allowing the model to return a tool
call without the unsupported constraint.
Closes strands-agents#1241
Contributor
Author
|
Friendly ping — adds a fallback to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Some Bedrock models (e.g., Meta Llama) do not support the
toolConfig.toolChoice.anyfield. Whenstructured_output()forcestool_choice={"any": {}}, these models fail with:Root Cause
BedrockModel.structured_output()unconditionally passestool_choice={"any": {}}(line 1082) to guarantee the model invokes the schema tool. However, not all Bedrock models support theanytool choice constraint — Llama models, for instance, only supportauto.Solution
Catch
ClientErrormentioningtoolChoicein the error message and transparently retry the stream withtool_choice={"auto": {}}. This allows structured output to work with models that only support theautotool choice, while preserving the stricteranyconstraint for models that support it (e.g., Claude).Changes
src/strands/models/bedrock.py: Wrapped the stream call in a try/except that catchesClientErrorabouttoolChoiceand retries withautotests/strands/models/test_bedrock.py: Added test verifying the fallback behavior (first call usesany, second call usesauto)Testing
All 126 Bedrock unit tests pass (125 existing + 1 new).
Closes #1241