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, when
ToolDefinition.strict=None, the Bedrock schema transformer infers many simple schemas as strict-compatible and auto-promotes them tostrict=True. - Bedrock/Claude only supports 20 strict tools maximum, so when there are many tools (e.g., 73), it fails with a validation error.
- The fix should be similar to what was done for Anthropic - do not auto-promote Bedrock tool definitions from
strict=Nonetostrict=True; only sendtoolSpec.strict = truewhen the user explicitly setstrict=True.
Let me first explore the repository structure to understand the codebase and find the relevant files.
- In pydantic-ai-slim==1.101.0, when
Assistant
Let me explore more to find the Bedrock provider files.
Assistant
Same error as before - the special commands don't work with quoted arguments. Let me try without quotes.
Assistant
I need to use the bash_terminal command to read files. Let me use cat to view the bedrock.py file.
Assistant
Now let me look at the Bedrock models file to find
_map_tool_definitionand_customize_tool_defthat are mentioned in the issue.Assistant
Let me search for the specific functions mentioned in the issue.
Assistant
I can see the issue now. In
_map_tool_definition, the code checksif f.strict and self.profile.bedrock_supports_strict_tool_definition:which means it only sendsstrictwhen it's truthy. But the issue is about_customize_tool_def()which replacesstrict=Nonewithstrict=True. Let me search for_customize_tool_defin the codebase.Assistant
Let me look at the
_customize_tool_deffunction inmodels/__init__.py:Assistant
Now I understand the issue. In
_customize_tool_def, whentool_def.strict is None, it setsstrict=schema_transformer.is_strict_compatible. For theBedrockJsonSchemaTransformer, simple schemas will be considered strict-compatible andis_strict_compatiblewill beTrue, causing the auto-promotion issue.Let me also look at the Anthropic provider to see how they handle this (as mentioned in the issue):
Assistant
Now I see the key difference. In
AnthropicJsonSchemaTransformer.walk(), there's this critical line:self.is_strict_compatible = self.strict is True # not compatible when strict is False/NoneThis means for Anthropic,
is_strict_compatibleis onlyTruewhen the user explicitly setstrict=True. Whenstrict=None, it staysFalse.But for
BedrockJsonSchemaTransformer, there's no such override inwalk(), so it inherits the base class behavior. Let me check the base class:Assistant
Looking at the issue, I need to find where
is_strict_compatibleis set in the base class and understand how the BedrockJsonSchemaTransformer should behave. The fix should be to makeBedrockJsonSchemaTransformernot auto-promote fromstrict=Nonetostrict=True, similar to how Anthropic does it.Assistant
I see the issue now. In the base class
JsonSchemaTransformer.__init__,is_strict_compatibleis initialized toTrue. ForBedrockJsonSchemaTransformer, it never overrides this behavior in itswalk()method, sois_strict_compatiblestaysTrueunless the schema has incompatible keys.The fix should make
BedrockJsonSchemaTransformerbehave likeAnthropicJsonSchemaTransformer- only setis_strict_compatible = Truewhenstrict is Truewas explicitly set by the user.Let me look at the tests to understand the expected behavior:
Assistant
I see the test file is quite long. Let me also look at the test_strict_tool_calls.py to understand the behavior better.
Assistant
Now let me also look at how the test
test_strict_none_preserves_numeric_constraintsworks to understand what behavior is expected:Assistant
Now I have a clear picture of the issue. Let me check the Anthropic approach more closely and understand what change is needed.
Assistant
The key fix is in the Anthropic provider's
walk()method at line 149-165. It overridesis_strict_compatibleto only beTruewhenstrict is True:self.is_strict_compatible = self.strict is True # not compatible when strict is False/NoneAssistant
Now I understand the code. The fix needs to add a
walk()method override similar to the Anthropic one. Let me see the fullBedrockJsonSchemaTransformerclass:Assistant
Now I understand the current behavior. The
transform()method currently:- When
strict is None: Setsis_strict_compatible = Falseonly when there are incompatible keys or whenadditionalPropertiesis notNone/False - When
strict is True: Strips incompatible keys and adds them to description
The issue is that when
strict=Noneand the schema is "simple" (no incompatible keys),is_strict_compatiblestaysTrue(the default from the base class), which causes auto-promotion.The fix is to add a
walk()method that, similar to Anthropic, setsis_strict_compatible = self.strict is Trueat the end, so that whenstrict=None, it never auto-promotes.However, I need to think about this more carefully. Looking at the docstring:
When
strict=None(the default), simple schemas without incompatible constraints are auto-promoted tostrict=True.This docstring describes the current behavior. The issue asks to change this behavior. So I need to:
- Update the docstring
- When
Assistant
Let me fix the typo:
Assistant
Let me verify the fix was applied correctly:
Assistant
The fix is applied. Now I need to also check if the
transformmethod still setsself.is_strict_compatible = Falsewhenstrict is Noneand there are incompatible keys. This is now redundant becausewalk()will setis_strict_compatible = Falseforstrict=Noneanyway. But I should keep the code clean - the assignments intransformwhenstrict is Noneare now dead code sincewalk()overrides them. Let me simplify the code:Actually, let me think about this more carefully. The
transformmethod is called during the walk (through_handle), so theis_strict_compatiblevalues set there will be overwritten bywalk(). The lines that setself.is_strict_compatible = Falsewhenself.strict is Noneare now effectively dead code, sincewalk()will override them at the end.However, I should keep the behavior correct. The current behavior of
transform()is:- When
strict=Noneand is not /: set (dead code now)
- When
Assistant
The dead code is the
elif self.strict is Nonebranches that setis_strict_compatible = False. Since these are now handled bywalk(), I can simplify the code. But let me think about whether I should remove them or leave them as is.Looking at the code flow:
- When
strict=NoneandadditionalPropertiesis notNone/False, it setsis_strict_compatible = Falsebut doesn't setadditionalProperties = False(keeps the original value) - When
strict=Noneand there are incompatible keys, it setsis_strict_compatible = Falsebut doesn't strip the keys
The important behavior is:
- When
strict=None, preserve the originaladditionalPropertiesvalue if it's notNone/False - When
strict=None, preserve the incompatible keys (don't strip them)
This behavior should still work correctly with my fix, since
walk()only affectsis_strict_compatibleand doesn't change the schema transformation logic.However, I should clean up the dead code assignments to
is_strict_compatibleto make the code clearer. Let me update the method:- When
Assistant
Let me verify the changes:
Assistant
Good, the changes look correct. Now let me run the existing tests to see which ones pass and which ones fail:
Assistant
Let me check if the package is already installed or if there's another way to run tests:
Assistant
I need to activate the virtual environment first before running tests.
Assistant
The two failing tests are expected - they were testing the old (problematic) behavior where
strict=Nonewould auto-promote tostrict=Truefor simple schemas. According to the issue and the checklist:"If existing tests break because they need to be updated to reflect the changes you made, just ignore it."
These tests need to be updated to reflect the new behavior, but I should not modify them.
Let me now run the strict tool calls tests to make sure the core functionality still works:
Assistant
All the strict tool calls tests pass. Now let me create a simple test script to verify that my fix works correctly for the issue described - that
strict=Noneno longer auto-promotes tostrict=True:Assistant
Excellent! The fix works correctly. Now let me also verify that the
_customize_tool_deffunction in the models module correctly uses this behavior. Let me create a more comprehensive test that simulates the actual issue scenario:Assistant
The fix works correctly. Now let me also run some related tests to make sure I haven't broken anything else:
Assistant
I need to run more tests to ensure the fix doesn't break anything else. Let me run tests related to json schema and tools.
Assistant
Let me also run the tests for tools to make sure nothing is broken:
Assistant
Let me also check if there are any Anthropic-related tests that might be affected since we're making Bedrock behave similarly to Anthropic:
Assistant
Let me review my changes one more time and check the final state of the file:
Assistant
The diff appears to be truncated. Let me verify the complete file changes are correct:
Assistant
The changes look good. Let me now run a broader set of tests to make sure everything is still working:
Assistant
The only two failing tests are the ones that tested the old behavior (
test_strict_none_preserves_schemaandtest_strict_none_simple_schema) which expectedis_strict_compatibleto beTruewhenstrict=Nonefor simple schemas. This is exactly the behavior we're changing to fix the issue.Let me also run a quick check to make sure the models tests pass:
Assistant
-
Added a
walk()method toBedrockJsonSchemaTransformerthat setsis_strict_compatible = self.strict is Trueat the end of the walk, which means:- When
strict=None(the default):is_strict_compatible = False(no auto-promotion) - When :
- When
-
Assistant
Let me try the command again.
Assistant
Let me do one final verification that my fix works correctly for the exact scenario described in the issue:
Assistant
I have successfully resolved the issue. Here's a summary of what I did:
Summary of Changes
The fix modifies
BedrockJsonSchemaTransformerin/pydantic_ai_slim/pydantic_ai/providers/bedrock.pyto prevent auto-promotion ofstrict=Nonetostrict=True.The Problem
When
ToolDefinition.strict=None(the default), the Bedrock schema transformer was inferring simple schemas as strict-compatible and auto-promoting them tostrict=True. This caused Bedrock/Claude to reject requests with more than 20 tools with:ValidationException: Too many strict tools (73). The maximum number of strict tools supported is 20.The Fix
Added a
walk()method override toBedrockJsonSchemaTransformerthat setsis_strict_compatible = self.strict is True, similar to the Anthropic provider. This ensures:strict=None→is_strict_compatible = False(no auto-promotion)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.