Skip to content

feat(tools): support for strict tool execution#1862

Open
BV-Venky wants to merge 4 commits intostrands-agents:mainfrom
BV-Venky:feat/strict-tool-use
Open

feat(tools): support for strict tool execution#1862
BV-Venky wants to merge 4 commits intostrands-agents:mainfrom
BV-Venky:feat/strict-tool-use

Conversation

@BV-Venky
Copy link
Copy Markdown
Contributor

@BV-Venky BV-Venky commented Mar 11, 2026

…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 @tool decorator has been updated to accept a new strict: bool | None optional parameter, which propagates down via the ToolSpec definitions into the respective underlying Model provider payloads.

The implementation robustly supports passing explicit strict=True and strict=False states (for providers that differentiate behavior), while continuing to safely omit the field entirely from the payload when it is absent or explicitly instantiated as None. All logic has been wrapped in unit tests mapping behavior across omission, True, and False values.

Strict Support by Provider

Provider Supported Behavior
Anthropic ✅ Yes Dynamically injects strict key if present
Bedrock ✅ Yes Dynamically injects strict key if present
OpenAI ✅ Yes Dynamically injects strict key if present (Chat Completions)
OpenAI Responses ✅ Yes Dynamically injects strict key if present (Responses API)

For the remaining model providers (e.g., Gemini, Mistral), strict tool execution is not natively supported by their respective APIs. If the strict parameter 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 errors

Example Usage

import strands
# The model will be strictly constrained to output only the exact parameters defined by the function signature
@strands.tool(strict=True)
def calculate_sum(a: int, b: int) -> int:
    """Calculates the sum of two integers."""
    return a + b

Includes an incidental formatting adjustment in _format_request_message_content within src/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 to True, explicitly False, or natively omitted.

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@BV-Venky BV-Venky changed the title Feat/strict tool use feat(tools): support for strict tool execution Mar 13, 2026
@github-actions github-actions Bot added size/m and removed size/m labels Mar 15, 2026
@github-actions github-actions Bot added size/m and removed size/m labels Mar 17, 2026
@github-actions github-actions Bot removed the size/m label Mar 19, 2026
@github-actions github-actions Bot added size/m and removed size/m labels Mar 20, 2026
@github-actions github-actions Bot removed the size/m label Apr 2, 2026
@github-actions github-actions Bot added the size/m label Apr 2, 2026
@BV-Venky
Copy link
Copy Markdown
Contributor Author

BV-Venky commented Apr 2, 2026

Hi @pgrayy
Friendly ping: Please review this. Will be happy to address any concerns

@BV-Venky
Copy link
Copy Markdown
Contributor Author

Hi @pgrayy
Friendly ping: Please review this. Will be happy to address any concerns

@github-actions github-actions Bot added size/l and removed size/m labels Apr 28, 2026
@BV-Venky BV-Venky force-pushed the feat/strict-tool-use branch from bb78fdc to 2d20361 Compare April 28, 2026 14:02
@github-actions github-actions Bot added size/l and removed size/l labels Apr 28, 2026
kaghatim pushed a commit to kaghatim/sdk-python that referenced this pull request Apr 28, 2026
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
kaghatim added a commit to kaghatim/sdk-python that referenced this pull request Apr 29, 2026
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>
@BV-Venky
Copy link
Copy Markdown
Contributor Author

Hi @zastrowm @pgrayy

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).
Granular Precedence: Model-level strictness (strict_tools=True/False) and tool-level strictness (@tool(strict=True/False)) now work together perfectly. Tool-level specifications will granularly override the model-level configuration, giving developers maximum control.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Support strict tool use

1 participant