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
Let me analyze the issue:
- The problem is that
strict=Noneis being auto-promoted tostrict=Truefor Bedrock tool definitions - This causes Bedrock to reject requests with more than 20 strict tools
- The fix should be similar to how Anthropic handles this - only send
toolSpec.strict = truewhen the user explicitly setstrict=True
Let me start by exploring the repository structure to understand the codebase.
- The problem is that
Assistant
Let me explore the Bedrock provider code and the Anthropic provider code to understand how they handle tool definitions.
Assistant
Let me look at the Bedrock model and provider files to understand how tool definitions are handled.
Assistant
Let me look at both the Anthropic provider (which the issue mentions as having the correct behavior) and the Bedrock provider to understand how they handle tool definitions.
Assistant
Now let me look at the Bedrock provider:
Assistant
Now I need to look at the Bedrock model file to see how it handles tool definitions:
<tool_call> <function=bash_terminal> <parameter=command> cat /pydantic-ai/pydantic_ai_slim/pydantic_ai/models/bedrock.py </tool_call>
Assistant
Let me try reading the bedrock model file again.
Assistant
The file is 1556 lines. Let me read it in chunks to understand how tool definitions are handled.
Assistant
Now let me look at the
_map_tool_definitionfunction around line 502 to understand how tool definitions are being mapped:Assistant
The issue is with the sed syntax. Let me use the correct syntax.
Assistant
Now let me look at how
_customize_tool_defworks in the base model:Assistant
Now I need to look at the
_customize_tool_deffunction in the base model:Assistant
Let me check the base
JsonSchemaTransformerclass to understand the default value ofis_strict_compatible:Assistant
Let me read the JsonSchemaTransformer class to understand how it handles strict compatibility.
Assistant
Now I understand the issue. Let me look at the full
BedrockJsonSchemaTransformerclass to understand the flow:Assistant
Now I understand the issue completely:
BedrockJsonSchemaTransformerinherits fromJsonSchemaTransformerwhich initializesis_strict_compatible = Trueby default- When
strict=None, simple schemas don't setis_strict_compatible = False, so it remainsTrue - In
_customize_tool_def, this auto-promotesstrict=Nonetostrict=Truefor simple schemas - Bedrock then receives
toolSpec.strict = truefor many tools and rejects the request (>20 strict tools limit)
The fix is to make
BedrockJsonSchemaTransformerbehave like Anthropic's transformer: only setis_strict_compatible = Truewhenstrictis explicitlyTrue, not when it'sNone.Assistant
Now I understand the issue. Let me look at how the AnthropicJsonSchemaTransformer handles this:
Assistant
Now I understand the issue. Let me examine the key parts of the code:
BedrockJsonSchemaTransformerhas a defaultis_strict_compatible = True(from parent class)- When
strict=None, simple schemas don't setis_strict_compatible = False, so it staysTrue _customize_tool_def()usesis_strict_compatibleto auto-promotestrict=Nonetostrict=True- Bedrock rejects requests with >20 strict tools
The fix should be similar to Anthropic's approach: only set
is_strict_compatible = Truewhenstrict=Trueis explicitly set.Let me first check the test files for Bedrock:
Assistant
Let me explore the test files to understand the existing test structure and then make the fix.
Assistant
Now I have a clear understanding of the issue. Let me create a simple test to reproduce the problem and then fix it.
The issue is:
BedrockJsonSchemaTransformerinheritsis_strict_compatible = Truefrom the parent class- When
strict=Noneand the schema has no incompatible constraints,is_strict_compatiblestaysTrue _customize_tool_def()auto-promotes this tostrict=True, exceeding Bedrock's 20 strict tools limit
The fix should be: Don't auto-promote from
strict=Nonetostrict=Truewhen the schema doesn't have incompatible constraints. Only setis_strict_compatible = Truewhenstrictis explicitlyTrue.Assistant
Now I understand the issue completely. Let me analyze it:
Root Cause:
BedrockJsonSchemaTransformerinheritsis_strict_compatible = TruefromJsonSchemaTransformer- When
strict=None(default) and schemas have no incompatible constraints,is_strict_compatibleremainsTrue _customize_tool_def()then auto-promotes this tostrict=True- Bedrock rejects requests with more than 20 strict tools
The Fix: Similar to Anthropic's approach, I need to override
walk()inBedrockJsonSchemaTransformerto only setis_strict_compatible = Truewhenstrictis explicitlyTrue.Let me implement the fix:
Assistant
Now I understand the issue completely. Let me implement the fix:
- The
BedrockJsonSchemaTransformerclass auto-promotesstrict=Nonetostrict=Truewhen schemas don't have incompatible constraints - This causes Bedrock to reject requests with >20 strict tools
- The fix is to override
walk()like Anthropic does, sois_strict_compatibleis onlyTruewhenstrictis explicitlyTrue
Let me edit the
BedrockJsonSchemaTransformerclass:- The
Assistant
Looking at this issue, I need to understand the root cause:
BedrockJsonSchemaTransformerinheritsis_strict_compatible = Truefrom the parent class- When
strict=None, if the schema has no incompatible constraints,is_strict_compatiblestaysTrue _customize_tool_def()then auto-promotesstrict=Nonetostrict=True- Bedrock only supports 20 strict tools max, so this causes failures with large toolsets
The fix is similar to Anthropic's approach: override
walk()to only setis_strict_compatible = Truewhenstrictis explicitlyTrue, not when it'sNone.Let me implement the fix:
Assistant
Now let me verify that the changes were applied correctly:
Assistant
Let me run the existing tests to verify the fix works:
Assistant
Let me check if there's a virtual environment or if I need to install the package first.
Assistant
Let me use the virtual environment to run the tests:
Assistant
The tests show that the fix is working correctly. The two failing tests (
test_strict_none_preserves_schemaandtest_strict_none_simple_schema) are checking the OLD behavior wherestrict=Nonewould auto-promote tostrict=True. After our fix, these tests now correctly fail becauseis_strict_compatibleisFalsewhenstrict=None(preventing auto-promotion).Per the checklist instructions:
"If existing tests break because they need to be updated to reflect the changes you made, just ignore it. Evaluation system will not take it into account."
Let me create a quick verification script to confirm the fix works as expected:
Assistant
The fix is working correctly. Let me verify the full test suite for the bedrock provider passes (except for the 2 tests that were testing the old behavior which we intentionally changed):
Assistant
The fix is working correctly. Let me verify the edge cases are handled properly:
Assistant
The fix is working correctly. Let me verify the final state of the changed file and run one more comprehensive check:
Assistant
The fix is complete and working correctly. Here's a summary of what was changed:
Summary of Changes
File modified:
pydantic_ai_slim/pydantic_ai/providers/bedrock.pyProblem:
BedrockJsonSchemaTransformerwas auto-promotingstrict=Nonetools tostrict=Truefor simple schemas. This caused Bedrock requests with >20 tools to fail withValidationException: Too many strict tools (73). The maximum number of strict tools supported is 20.Solution: Added a
walk()method override toBedrockJsonSchemaTransformerthat only setsis_strict_compatible = Truewhenstrictis explicitlyTrue. This mirrors the behavior ofAnthropicJsonSchemaTransformer.walk()and prevents auto-promotion.Key changes:
- Added
walk()method that setsself.is_strict_compatible = self.strict is True - Updated docstring to clarify that
strict=Noneno longer auto-promotes to strict
- Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.