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):textValidationException: 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
I'll explore the repository structure and find the relevant Bedrock files to understand the issue.
Assistant
Now let me look at how
_customize_tool_defworks in the base model and how strict is set:Assistant
Assistant
Now I understand the issue clearly. Let me look at the existing Bedrock tests to understand the expected behavior:
Assistant
Let me trace the flow to understand where
_customize_tool_defis called:Assistant
Let me check the order of operations in
prepare_request- specifically whethercustomize_request_parametersis called before or after the output object strict forcing:Assistant
Now let me check the Bedrock
prepare_requestto see if the output object strict forcing happens beforecustomize_request_parameters:Assistant
Now I have a clear understanding. Let me look at the full Bedrock transformer and the Anthropic approach for reference:
Assistant
Now I have a clear understanding. The fix follows the Anthropic pattern: when
strict=None, don't auto-promote tostrict=True. Let me implement the fix:Assistant
Now let me implement the fix in the Bedrock transformer. I'll modify the
transformmethod to treatstrict=Nonelikestrict=False, and add awalk()override similar to Anthropic:Assistant
Now I'll implement the fix. I need to:
- Update the docstring
- Add a
walk()override that setsis_strict_compatible = self.strict is True(like Anthropic) - Remove the
elif self.strict is None:branches fromtransform()sostrict=Nonedoesn't get strict schema modifications
Assistant
I see there's a duplicate
@dataclass(init=False)line. Let me fix that:Assistant
Good. Now let me also check the
_map_tool_definitionin the bedrock model - it usesif f.strict and self.profile.bedrock_supports_strict_tool_definition. Sincef.strictwill now beFalse(notTrue) forstrict=Nonetools, this is fine. But let me also check if there's a need to useis Trueinstead of truthy:Assistant
The
_map_tool_definitionis fine sincef.strictwill beFalse(notTrue) forstrict=Nonetools after the fix. Now let me create a reproduction script to verify:Assistant
All tests pass. Now let me run the existing Bedrock tests to make sure nothing is broken:
Assistant
There's a test that checks the old behavior. Let me look at it to understand the scope:
Assistant
These tests are testing the old behavior that the issue explicitly says should change. Per the instructions, I should not modify tests that break because they need to be updated to reflect the changes. Let me run the full test suite without
-xto see the scope:Assistant
Let me check the 4th failing test at line 861 to confirm it's also testing the old
strict=Nonebehavior:Assistant
All 4 failures are tests directly testing the old
strict=Noneauto-promotion behavior - exactly the behavior the issue says should change. Let me verify the native output tests (where strict is forced to True) still pass:Assistant
Native output tests pass. Now let me check if there are any VCR cassette tests that send
strict=trueforstrict=Nonetools (which would now break since we no longer auto-promote):Assistant
Let me check the cassette tests more carefully - looking for tests that use
strict=None(default) tools and expectstrict: truein the request:Assistant
Let me check the main bedrock model test cassettes for any tools sent with
strict: truethat aren't explicitly strict:Assistant
No main bedrock cassettes use
strict: true. Good. Now let me run the full Bedrock test suite one more time to see the complete picture:Assistant
All bedrock model tests pass. Let me also run the broader test for models/cassettes/test_bedrock:
Assistant
All 122 main bedrock model tests pass. Now let me clean up the temp file and verify the final state of the changes:
Assistant
The changes look correct and complete. Let me verify there are no import/syntax errors by importing the module:
Assistant
Let me also run the Anthropic provider tests to make sure nothing cross-cutting is broken (though I only modified bedrock.py):
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.