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 first. The problem is that in Pydantic AI v1.101.0, when
strict=Noneis set on tool definitions, the Bedrock provider auto-promotes them tostrict=Truefor simple schemas. This causes Bedrock/Claude to reject requests with more than 20 strict tools.The fix should be similar to what's done for Anthropic - 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 to understand the codebase and find the relevant files.
- Assistant
Let me find the Bedrock provider file.
- Assistant
Let me look at the Bedrock provider file to understand how tool definitions are handled.
- Assistant
Let me look at the full Bedrock provider file to understand the structure.
- Assistant
Now let me look at the Bedrock model file to see how tool definitions are mapped.
- Assistant
Now let me see the
_map_tool_definitionfunction in the Bedrock model file. - Assistant
Now let me find the
_customize_tool_deffunction mentioned in the issue. - Assistant
Let me look at the
_customize_tool_deffunction inmodels/__init__.py. - Assistant
Now let me look at how the Anthropic provider handles this issue.
- Assistant
Let me look at the full Anthropic transformer to understand how it handles
strict=None. - Assistant
Now let me look at the
JsonSchemaTransformerbase class to understand theis_strict_compatibleproperty. - Assistant
So by default
is_strict_compatibleis set toTrue. This means ifstrict=None, the tool will be auto-promoted tostrict=True.The BedrockJsonSchemaTransformer sets
is_strict_compatible = Falsewhen it encounters incompatible constraints, but for simple schemas it staysTrue.The fix should be similar to Anthropic: set
is_strict_compatible = Falsewhenstrict=Nonein theBedrockJsonSchemaTransformer.But wait, let me re-read the issue more carefully. The issue says that before v1.101.0, tools were sent as non-strict. After v1.101.0, they're auto-promoted to strict. So the behavior change is in how
strict=Noneis handled.Let me check the git log to see what changed in v1.101.0.
- Assistant
Let me look at the Bedrock strict tool tests.
- Assistant
Now let me look at the test for
test_bedrock_strict_tool_definition_nonemore closely. It seems to expect that whenstrict=None, the strict field is not sent. But the issue says that_customize_tool_def()auto-promotesstrict=Nonetostrict=Truefor simple schemas. Let me check the flow more carefully. - Assistant
Now let me look at how the
_customize_tool_deffunction usesis_strict_compatible. - Assistant
Now I understand the issue. Let me look at the
walk()method in the base class and in the Anthropic transformer to understand how they handleis_strict_compatible. - Assistant
Let me look at the
walk()method in the base class and the Anthropic transformer to understand how they handleis_strict_compatible. - Assistant
Now let me look at the Anthropic provider more closely to see how they handle
strict=None: - Assistant
Now I have a clear picture of the issue. Let me trace through the problem:
- Tools are created with
strict=None(default) BedrockJsonSchemaTransformer.transform()whenstrict=Noneonly setsis_strict_compatible = Falseif the schema has incompatible keys- For simple schemas,
is_strict_compatibleremainsTrue(the default from base class) _customize_tool_def()seesis_strict_compatible=Trueand setsstrict=Trueon the tool definition_map_tool_definition()sendstoolSpec.strict = trueto Bedrock- Bedrock rejects when >20 strict tools
The fix: Similar to Anthropic's
AnthropicJsonSchemaTransformer, theBedrockJsonSchemaTransformershould not auto-promotestrict=Nonetostrict=True. Let me implement this.Let me first check the existing test that verifies the
strict=Nonebehavior: - Tools are created with
- Assistant
Now let me look at the full test file for
test_bedrock.pyto understand the tests that will need updating: - Assistant
Now I understand the full issue. The
BedrockJsonSchemaTransformerauto-promotesstrict=Nonetostrict=Truefor simple schemas becauseis_strict_compatibledefaults toTrueand only gets set toFalsewhen incompatible constraints are found. This causes Bedrock to reject requests with >20 strict tools.The fix should be similar to Anthropic's approach: don't auto-promote
strict=Nonetostrict=True. I need to add awalk()method toBedrockJsonSchemaTransformerthat overridesis_strict_compatibletoFalsewhenstrict is None. - Assistant
Now I understand the problem clearly. The
BedrockJsonSchemaTransformerauto-promotesstrict=Nonetostrict=Truefor simple schemas becauseis_strict_compatibledefaults toTrue. The fix should be similar to Anthropic's approach: don't auto-promotestrict=Nonetostrict=True.Let me implement the fix:
- Assistant
Let me use the
openspecial command. - Assistant
Now I have a clear understanding of the issue. The
BedrockJsonSchemaTransformerauto-promotesstrict=Nonetostrict=Truefor simple schemas (whenis_strict_compatiblestaysTrue). The fix should be similar to Anthropic's approach: don't auto-promotestrict=Nonetostrict=True.Let me implement the fix by adding a
walk()method toBedrockJsonSchemaTransformer: - Assistant
Let me try using the edit command with the correct syntax:
- Assistant
Good, the edit was applied. Now let me verify the change looks correct:
- Assistant
Now let me update the tests that expect
is_strict_compatible is Truewhenstrict=None. Let me look at all the tests that need updating: - Assistant
Let me check which of these tests use
strict=None(the ones that need updating): - Assistant
Let me look at each test in detail to find which ones use
strict=None: - Assistant
Let me look at each test that uses
strict=Noneand check what they assert: - Assistant
Let me also look at the numeric constraints test:
- Assistant
Now let me check all tests that use
strict=Noneand assertis_strict_compatible is True. Let me look at each one: - Assistant
Now I need to update the tests that assert
is_strict_compatible is Truewhenstrict=None. Let me update each one: - Assistant
Now I need to update the two assertions. Let me fix them:
- Assistant
Now let me update the second test:
- Assistant
Now let me update the assertion on line 516:
- Assistant
Now let me run the tests to verify the changes work:
- Assistant
I need to use the virtual environment:
- Assistant
All 48 tests in
test_bedrock.pypass. Now let me run the other Bedrock-related tests: - Assistant
All tests pass. Let me also run the other bedrock-related tests:
- Assistant
Let me also run the main bedrock model test:
- Assistant
All 122 tests pass. Let me now create a quick verification script to confirm the fix works for the exact scenario described in the issue:
- Assistant
Let me also verify that explicitly set
strict=Truestill works: - Assistant
All tests pass. Let me also run any broader test suites that might be affected:
- Assistant
All 269 bedrock-related tests pass. Let me review the final diff to make sure everything is clean:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.