feat(tools): support for strict tool execution#1862
feat(tools): support for strict tool execution#1862BV-Venky wants to merge 4 commits intostrands-agents:mainfrom
Conversation
|
Hi @pgrayy |
|
Hi @pgrayy |
bb78fdc to
2d20361
Compare
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. Includes require_all_properties flag for OpenAI support. From PR strands-agents#1862
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>
2d20361 to
7ff586d
Compare
|
Update: This PR has been rebased and updated to fully accommodate the changes from PR #2213 The implementations have been brought completely in sync to ensure that our vision for strict tool usage works harmoniously across the SDK. Specifically: Unified Schema Patching: The recursive schema patching logic (enforcing additionalProperties: False and required parameters) now reliably handles the requirements for strict mode across all supported providers (OpenAI, Anthropic, Bedrock, and OpenAI Responses). All unit and integration tests across the supported model providers have been updated and are passing locally. Please review this. Will be happy to address any concerns or comments. |
…nd OpenAI Responses
Description
This PR introduces support for strict tool execution, allowing models to be explicitly constrained to output tool parameters that perfectly match the provided JSON schema.
The
@tooldecorator has been updated to accept a newstrict: bool | Noneoptional parameter, which propagates down via theToolSpecdefinitions into the respective underlying Model provider payloads.The implementation robustly supports passing explicit
strict=Trueandstrict=Falsestates (for providers that differentiate behavior), while continuing to safely omit the field entirely from the payload when it is absent or explicitly instantiated asNone. All logic has been wrapped in unit tests mapping behavior across omission, True, and False values.Strict Support by Provider
For the remaining model providers (e.g., Gemini, Mistral), strict tool execution is not natively supported by their respective APIs. If the
strictparameter is provided to these models via the decorator, we gracefully handle this by not considering the parameter during request formatting, ensuring seamless compatibility without triggering validation errorsExample Usage
Includes an incidental formatting adjustment in
_format_request_message_contentwithinsrc/strands/models/openai_responses.py. No functional changes were made to this method.Related Issues
Resolves #1642
Type of Change
New feature
Testing
I added corresponding unit tests across the tool decorators and all supporting model providers (
test_anthropic.py, test_bedrock.py, test_openai.py, test_openai_responses.py). I exhaustively tested that the providers accurately handle scenarios where strict is set toTrue, explicitlyFalse, or natively omitted.hatch 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.