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.
Minimal, Reproducible Example
Logfire Trace
No response
Python, Pydantic AI & LLM client version
- Python: 3.14
- Pydantic AI: 1.101.0
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pydantic-ai, current file: none) bash-$
Assistant
Let me analyze the issue:
- The problem is that in
pydantic-ai-slim==1.101.0, whenToolDefinition.strict=None, the Bedrock schema transformer auto-promotes it tostrict=Truefor simple schemas. - Bedrock/Claude only supports 20 strict tools, so when there are many tools (e.g., 73), the request gets rejected.
- The fix should be similar to how Anthropic handles it - do not auto-promote Bedrock tool definitions from
strict=Nonetostrict=True; only sendtoolSpec.strict = truewhen the user explicitly setstrict=True.
Let me start by exploring the repository structure to understand the codebase.
- The problem is that in
Assistant
Let me explore the Bedrock provider files to understand the current implementation.
Assistant
I need to use the correct syntax for the open command without the file parameter.
Assistant
Let me also look at the models/bedrock.py file where the BedrockConverseModel is defined:
Assistant
Let me search for the
_map_tool_definitionmethod in the Bedrock model:Assistant
Now let me look at the
_map_tool_definitionmethod and the surrounding context more closely:Assistant
I see that
_map_tool_definitiononly setstool_spec['strict'] = f.strictwhenf.strictis truthy (i.e.,True). But the issue is that theBedrockJsonSchemaTransformeris auto-promotingstrict=Nonetostrict=Truefor simple schemas. Let me look at how the_customize_tool_defmethod works in the codebase:Assistant
Let me look at the
_customize_tool_deffunction:Assistant
Now I see the issue! In
_customize_tool_def, whentool_def.strict is None, the result is:strict=schema_transformer.is_strict_compatible if tool_def.strict is None else tool_def.strictSo if
is_strict_compatibleisTrue(which is the default for simple schemas), the tool definition gets auto-promoted tostrict=True.Let me now look at how the Anthropic provider handles this to understand the expected fix:
Assistant
I can see the key difference now. In the
AnthropicJsonSchemaTransformer.walk()method, it sets:self.is_strict_compatible = self.strict is True # not compatible when strict is False/NoneThis means when
strict=None,is_strict_compatibleisFalse, so_customize_tool_defwon't auto-promote it tostrict=True.But looking at the
BedrockJsonSchemaTransformer, it doesn't overridewalk()and doesn't setis_strict_compatible = self.strict is True. The baseJsonSchemaTransformerclass defaultsis_strict_compatible = True.Let me check the base class:
Assistant
Let me look at the
JsonSchemaTransformerclass:Assistant
Now I understand the issue clearly. The base
JsonSchemaTransformerclass setsself.is_strict_compatible = Trueby default. When theBedrockJsonSchemaTransformerprocesses a schema withstrict=None, it may setis_strict_compatible = Falseif it finds incompatible keys. But for simple schemas (without those incompatible keys),is_strict_compatibleremainsTrue, which causes_customize_tool_defto auto-promotestrict=Nonetostrict=True.The fix should be similar to the Anthropic approach: in the
BedrockJsonSchemaTransformer, we need to override thewalk()method (or setis_strict_compatible = Falsewhenstrict is None) so that simple schemas withstrict=Noneare not auto-promoted tostrict=True.Let me look at the Anthropic implementation more closely:
Assistant
Now I understand. The fix is clear: the
BedrockJsonSchemaTransformerneeds to overridewalk()and setself.is_strict_compatible = self.strict is Trueto prevent auto-promotion ofstrict=Nonetostrict=True.But wait, there's another consideration. Looking at the
BedrockJsonSchemaTransformer.transform()method:elif self.strict is None: if schema.get('additionalProperties', None) not in (None, False): self.is_strict_compatible = False else: schema['additionalProperties'] = FalseAnd:
elif self.strict is None: self.is_strict_compatible = FalseSo currently, the
BedrockJsonSchemaTransformeronly setsis_strict_compatible = Falsewhen there are incompatible keys. For simple schemas,is_strict_compatiblestaysTrue, which causes the auto-promotion issue.The fix should be: after the
walk()method from the base class runs, we should check ifstrict is Noneand, if so, setis_strict_compatible = False(similar to how Anthropic does it).Let me now check the tests for Bedrock to understand the expected behavior better:
Assistant
I see that the existing tests expect
is_strict_compatible is Truewhenstrict=None. This is the behavior that needs to change. Let me look at more tests:Assistant
Now I can see that the tests have mixed expectations. Some tests (like
test_strict_none_preserves_schema,test_strict_none_simple_schema) expectis_strict_compatible is Truewhenstrict=None, while others (liketest_strict_none_incompatible_schema_disables_auto_strict,test_strict_none_with_additional_properties_true,test_strict_none_preserves_numeric_constraints) expectis_strict_compatible is False.Wait, looking more carefully at the tests, I notice that
test_strict_none_preserves_numeric_constraintsexpectsis_strict_compatible is Falsefor schemas with numeric constraints. Buttest_strict_none_simple_schemaexpectsis_strict_compatible is Truefor simple schemas.The issue is that for simple schemas (without incompatible keys),
is_strict_compatiblestaysTrue, which causes the auto-promotion problem reported in the issue.The fix should be: for Bedrock, when
strict=None,is_strict_compatibleshould always beFalse, regardless of whether the schema is "simple" or has incompatible keys. This is consistent with the Anthropic approach.Let me verify my understanding by looking at how the Anthropic provider tests are structured:
Assistant
I see that the Anthropic profile tests confirm the expected behavior:
strict=True→is_strict_compatible=Truestrict=False/None→is_strict_compatible=False
Now let me implement the fix. I need to modify the
BedrockJsonSchemaTransformerto override thewalk()method and setis_strict_compatible = Falsewhenstrict is None.Let me first look at the current
BedrockJsonSchemaTransformerimplementation more closely:Assistant
Now I understand the full picture. The fix is to override the
walk()method inBedrockJsonSchemaTransformerand, after callingsuper().walk(), setself.is_strict_compatible = self.strict is TruewhenstrictisNoneorFalse.Let me implement the fix:
Assistant
Now I'll create the fix. I need to add a
walk()method toBedrockJsonSchemaTransformerthat overrides the parent'swalk()and setsis_strict_compatible = Falsewhenstrict is not True.Assistant
Now I need to add the
walk()method toBedrockJsonSchemaTransformer. The fix should:- Override
walk()to callsuper().walk()first - After walking, if
strict is not True, setis_strict_compatible = Falseto prevent auto-promotion
Let me also update the docstring to reflect the new behavior:
- Override
Assistant
Let me verify the fix was applied correctly:
Assistant
I see that the
walk()method was not added. Let me check what happened:Assistant
The
walk()method wasn't added. Let me check the exact content around the transform method:Assistant
I see. The pattern replacement didn't work because there were only two newlines between
return schemaand@dataclass, not three. Let me fix this:Assistant
command </tool_call>
Assistant
The previous fix was applied, let me verify it now by checking the file content.
Assistant
The
walkmethod was successfully added to theBedrockJsonSchemaTransformerclass. Now let me verify the implementation by looking at the full method.Assistant
Let me verify the complete class looks correct:
Assistant
Now let me create a test script to verify the fix works correctly:
Assistant
The module is not installed. Let me check how to run tests in this repository.
Assistant
The package can't be installed via pip because of a local dependency. Let me try running Python with the proper PYTHONPATH set.
Assistant
Let me install the missing dependencies or try a different approach to test the fix.
Assistant
Let me try installing more dependencies or check if there's a simpler way to test this. Let me look for how tests are run in this repository.
Assistant
Let me try to install the dependencies needed to run the tests. The repo uses uv, let me check if uv is available and use it to install the project.
Assistant
Good, uv is available. Let me try to install the dependencies using uv.
Assistant
The installation seems to have succeeded. Let me now run the test script.
Assistant
The fix is working. Now let me run the existing Bedrock provider tests to verify the change and see which tests might be affected:
Assistant
head -50
Assistant
The two failing tests (
test_strict_none_preserves_schemaandtest_strict_none_simple_schema) are testing the old behavior wherestrict=Noneauto-promoted tois_strict_compatible=True. My fix intentionally changes this behavior - these tests reflect the bug that the issue is reporting.Let me also run some related tests to make sure the fix doesn't break other functionality:
Assistant
All 18 tool-related Bedrock model tests pass. Let me also verify that the integration between
_customize_tool_defandBedrockJsonSchemaTransformerworks correctly by creating a more comprehensive test:Assistant
The fix is working correctly. Now let me verify that the
_map_tool_definitionmethod in the Bedrock model also works correctly with this change:Assistant
Let me run all bedrock-related tests to see the full picture:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →