feat(bedrock): add strict_tools config with auto-inject of additional…#2213
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Assessment: Comment Clean implementation of Review Categories
Well-structured PR with thorough documentation and a thoughtful approach to the schema transformation. |
1ba9420 to
7f66b49
Compare
|
Assessment: Comment The refactoring into a dedicated Review Details
Good evolution of the implementation — the module separation and comprehensive schema handling are solid. |
|
I intentionally omitted the citations warning after testing against Bedrock. Citations in the Converse API are configured per-document inside message content blocks ( |
Add ensure_strict_json_schema utility that recursively injects additionalProperties: false on all object types in tool input schemas. Handles $defs, definitions, anyOf, allOf, oneOf, items, and $ref resolution with deep copy safety. Includes require_all_properties flag for OpenAI support. From PR strands-agents#1862 Co-authored-by: Hatim Kagalwala <kaghatim@amazon.com>
Add strict_tools boolean option to BedrockConfig that injects strict: true into each toolSpec and uses ensure_strict_json_schema from _strict_schema to automatically add additionalProperties: false to all object types in tool input schemas. This makes strict mode work out of the box with @tool decorated functions. Resolves strands-agents#2210
7f66b49 to
30f3e2f
Compare
|
@zastrowm Thanks for the review! I have addressed the feedback:
Let me know if there's anything else you'd like adjusted or changed. |
|
Assessment: Approve All previous review feedback has been thoroughly addressed. The refactored Minor Item
The author's explanation for omitting the citations warning is reasonable — citations are configured per-document in content blocks rather than at request level, making a config-time check infeasible. |
|
it seems like the integration tests failure are unrelated to this change, but happy to take a look if anything needs to be changed. |
Description
Motivation
Bedrock's Converse API supports a
strictfield onToolSpecificationthat enables constrained decoding on tool names and inputs, preventing the model from hallucinating tool names or generating inputs that don't match the defined schema. Currently there is no way to enable this through the SDK because_format_requestonly forwardsname,description, andinputSchemafrom each tool spec.A key challenge with strict mode is that Bedrock requires
"additionalProperties": falseon everyobjecttype in the tool's input schema, validated recursively including nested objects. The Strands@tooldecorator does not include this field in generated schemas, and in fact actively strips it in_clean_pydantic_schema. This means simply injectingstrict: trueinto the request is not enough: every@tool-decorated function would fail with aValidationException.This PR adds a
strict_toolsconfig option toBedrockModelthat both injectsstrict: trueand automatically adds"additionalProperties": falseto all object types in tool input schemas, making strict mode work out of the box with@tool-decorated functions. The schema transformation operates on a deep copy so original tool specs are never mutated. This follows the same pattern as OpenAI's Python SDK (_ensure_strict_json_schema).Resolves #2210
Public API Changes
BedrockConfignow accepts an optionalstrict_toolsboolean parameter:When
strict_tools=True, everytoolSpecin the formatted request includes"strict": trueand allobjecttypes in tool input schemas have"additionalProperties": falseinjected. When unset,None, orFalse, tool specs remain unchanged.Use Cases
store-case-syummary-toolinstead ofstore-case-summary-tool), avoiding wasted cycles and tokens on self-correctionRelated Issues
#2210
Type of Change
New feature
Testing
Verified with live Bedrock calls against
us.anthropic.claude-sonnet-4-6:strict_tools=Truewith@tooldecorated functionValidationException)strict_tools=Truewith manually defined schemastrict_toolsunset /False/Nonehatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.