ISSUE DESCRIPTION Bedrock: strict=None auto-promotes many tools to strict and hits…
- User
ISSUE DESCRIPTION
Bedrock:
strict=Noneauto-promotes many tools to strict and hits provider limitInitial Checks
- I'm using the latest version of Pydantic AI
- I've searched for my issue in the issue tracker before opening this issue
Description
Summary
pydantic-ai-slim==1.101.0can now fail Bedrock Claude requests with many tools becauseToolDefinition.strict=Noneis inferred asstrict=Truefor simple schemas. Bedrock/Claude rejects requests with more than 20 strict tools (source):ValidationException: Too many strict tools (73). The maximum number of strict tools supported is 20.This is a behavior regression for callers that did not explicitly opt into strict tool definitions.
What happened
A production agent sends 73 function tools to Bedrock. Before 1.101.0 these tools were sent as normal, non-strict Bedrock tools.
After 1.101.0, these tool definitions have strict=None, and the Bedrock schema transformer infers many/all simple schemas as strict-compatible. _customize_tool_def() then replaces strict=None with strict=True, and BedrockConverseModel._map_tool_definition() sends toolSpec.strict = true. Bedrock rejects the request because it only supports 20 strict tools.
Disclosure: I authored #4237 ✋ . My tests and vcr cassettes didn't account for the >20-tool case.
Expected behavior
For Bedrock, strict=None should not silently turn large toolsets into strict toolsets that violate Bedrock limits.
Possible fixes:
Similar to Anthropic, do not auto-promote Bedrock tool definitions from strict=None to strict=True; only send toolSpec.strict = true when the user explicitly set strict=True.
Workaround
Callers can explicitly set strict=False on Bedrock tool definitions, but this requires every application with larger Bedrock toolsets to discover and patch the behavior.
Assistant
Let me understand the issue:
- In
pydantic-ai-slim==1.101.0, Bedrock Claude requests can fail when there are many tools becauseToolDefinition.strict=Noneis inferred asstrict=Truefor simple schemas. - Bedrock/Claude rejects requests with more than 20 strict tools.
- The fix should be similar to Anthropic's provider - don't auto-promote Bedrock tool definitions from
strict=Nonetostrict=True; only sendtoolSpec.strict = truewhen the user explicitly setstrict=True.
Let me explore the repository structure first to understand the codebase and find the relevant files.
- In
Assistant
Let me find the relevant files related to Bedrock providers.
Assistant
Let me look at the key files. First, the Bedrock model and the Anthropic model for comparison.
Assistant
Let me search for the relevant parts related to
strictand_customize_tool_def:Assistant
Let me look at the Anthropic provider for comparison
Assistant
Let me see the Anthropic provider's relevant code:
Assistant
Let me find where
_customize_tool_defis defined and howstrictis resolvedAssistant
Let me see the
_customize_tool_deffunction in models/init.pyAssistant
Now let me find the Bedrock JSON schema transformer:
Assistant
There's no JsonSchemaTransformer in bedrock.py. Let me search more broadly.
Assistant
Now let me look at the Bedrock JSON schema transformer in `providers/bedrock.py
Assistant
Let me check the base
JsonSchemaTransformerclass to understandis_strict_compatible:Assistant
Now I have a clear understanding of the issue. Let me trace the logic:
JsonSchemaTransformer.__init__(base class) setsself.is_strict_compatible = True.BedrockJsonSchemaTransformer.transform()may setis_strict_compatible = Falsefor incompatible schemas.- But for simple schemas without incompatible constraints,
is_strict_compatiblestaysTrue. _customize_tool_defthen promotesstrict=Nonetostrict=Truefor these simple schemas.- In
bedrock.pyline 502-503, this causestoolSpec.strict = trueto be sent to Bedrock. - Bedrock rejects >20 strict tools.
The fix should follow Anthropic's approach: set
is_strict_compatible = self.strict is True, so it's onlyTruewhen the user explicitly opted in.Let me look at the exact lines in
bedrock.pywherestrictis checkedAssistant
Now I understand the full picture. Let me implement the fix. The key is to override
walk()inBedrockJsonSchemaTransformerto setis_strict_compatible = self.strict is True, following the Anthropic patternAssistant
Let me try again with the proper tool invocation.
Assistant
Now I need to add a
walk()method toBedrockJsonSchemaTransformerthat follows the Anthropic pattern. Let me make the edit:Assistant
Let me try a different approach with the replace command
Assistant
Now let me update the docstring to reflect the new behavior:
Assistant
Good. Now let me run the existing tests to ensure the change doesn't break anything:
Assistant
Let me install the package first
Assistant
Let me check the repository structure and see how to properly
Assistant
There's a
.venvalready. Let me check if pydantic_ai is installed there.Assistant
Let me check if pydantic-ai is installed.
Assistant
Good, pydantic-ai-slim is installed as editable. Let me run the relevant tests.
Assistant
The test failure is expected - it's testing the old behavior where
strict=Noneauto-promotes tostrict=True. The issue explicitly says this is the wrong behavior. Let me look at the test to understand betterAssistant
Let me see the rest of the test file
Assistant
Let me see if there are more tests with `is_strict_compatible is
Assistant
Let me also check the other test file for strict
Assistant
Let me write a quick verification script to confirm the fix works as expected
Assistant
Let me also verify that explicit
strict=Truestill works:Assistant
The fix works correctly. Let me verify the final state of the file:
Assistant
Let me run all the bedrock-related tests to see the full picture:
Assistant
The fix is complete. Let me verify the git diff to see the exact
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.