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 start by understanding the issue:
- In pydantic-ai version 1.101.0,
ToolDefinition.strict=Noneis auto-promoted tostrict=Truefor simple schemas in the Bedrock provider. - Bedrock/Claude has a limit of 20 strict tools, so when many tools are sent, it fails.
- The fix should be similar to what was done for Anthropic provider: 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 to understand the structure and find the relevant files.
- In pydantic-ai version 1.101.0,
Assistant
Let me find the relevant files. The issue mentions:
_customize_tool_def()- this likely auto-promotes strict=None to strict=TrueBedrockConverseModel._map_tool_definition()- this sends toolSpec.strict- The Anthropic provider fix at
pydantic_ai_slim/pydantic_ai/providers/anthropic.py#L129
Let me explore the providers directory.
Assistant
Let me look at the bedrock.py and anthropic.py files to understand the issue.
Assistant
Let me look at the specific sections of the bedrock.py file.
Assistant
Let me search more broadly for these function names.
Assistant
Let me look at the key files. First, the Anthropic provider fix:
Assistant
Let me use the correct format.
Assistant
Now let me look at the Bedrock transformer:
Assistant
Now let me look at the base class
JsonSchemaTransformerto understand whatis_strict_compatibledefaults to:Assistant
Let me look at the
_customize_tool_deffunction and the base class:Assistant
Now let me find where
is_strict_compatibleis defined in the base class:Assistant
Let me search for the JsonSchemaTransformer class definition.
Assistant
Let me look at the JsonSchemaTransformer class to understand the is_strict_compatible property.
Assistant
Now let me look at the Bedrock
_map_tool_definitionto see how strictAssistant
Now I understand the issue fully. Let me also check where the
BedrockJsonSchemaTransformerclass definition ends to see if there's already awalk()Assistant
Now let me also check existing tests and understand the test structure
Assistant
Let me look at the test that will be affected by our change:
Assistant
Let me also check the incompatible schema test and look at the full test context:
Assistant
Now let me also look at the relevant
_customize_tool_deftests in models:Assistant
Let me look at the test_bedrock_strict_tool_definition_none test
Assistant
Now I have a complete understanding of the issue. Let me implement the fix.
The problem:
- In
BedrockJsonSchemaTransformer, whenstrict=None, the defaultis_strict_compatible = Truefrom the base class is not changed for simple schemas. _customize_tool_definmodels/__init__.pythen promotestool_def.strictfromNonetoTrue(viais_strict_compatible)._map_tool_definitioninmodels/bedrock.pysendstoolSpec.strict = Truewhenf.strictis truthy.- Bedrock rejects requests with >20 strict tools.
The fix: Add a
walk()override inBedrockJsonSchemaTransformerthat setsis_strict_compatible = self.strict is True, identical to what the Anthropic provider does. This way,strict=Nonenever auto-promotes tostrict=True.- In
Assistant
Let me open the file first and view the exact lines:
Assistant
Now let me apply the fix. I'll use the
editcommand to insert thewalk()method at line 78 (before thetransformmethod):Assistant
The edit removed the
def transformmethod header. Let me fix this by adding it back:Assistant
I notice
schema.pop('title', None)is also missing. Let me add it backAssistant
I see line 90 is now empty (was
schema.pop('$schema', None)). Let me addAssistant
Let me remove the extra blank line at line 78
Assistant
Now I should also update the docstring to reflect the new behavior. Let me update
Assistant
Now let me verify the fix works correctly. First, let me create a small test script to confirm the behavior:
Assistant
Let me set up the proper Python path:
Assistant
Let me install the package first
Assistant
Let me understand the project structure better:
Assistant
Let me try to install using the local dependencies:
Assistant
Let me try installing both packages locally.
Assistant
Now let me verify the fix with the test script:
Assistant
The first three checks passed! Let me find the correct import:
Assistant
The fix works correctly. Now let me run the existing Bedrock provider tests:
Assistant
Let me install the test dependencies.
Assistant
Now let me run the Bedrock provider tests
Assistant
Let me install more test dependencies.
Assistant
Let me try running the tests again.
Assistant
Let me try running the tests differently:
Assistant
Let me install the missing pytest plugin and run the tests
Assistant
Let me try running the
Assistant
Let me try running specific tests that don't require VCR cass
Assistant
Let me run the tests that are related to the schema transformer specifically, filtering for the strict-related ones
Assistant
Let me understand the skip condition:
Assistant
Let me look
Assistant
Let me look at the specific test file to understand the test fixtures
Assistant
Let me install the missing dependency for the Bedrock tests:
Assistant
Now let me run the strict-related tests again
Assistant
As expected, some tests fail due to the behavior change. Let me run all strict-related tests to see the full picture
Assistant
Let me verify the
test_strict_none_preserves_numeric_constraintstest passes correctlyAssistant
Now let me also look at the models tests for Bedrock strict tool
Assistant
Let me install brotli and run again
Assistant
Now let me run the models tests for Bedrock strict tool
Assistant
All models tests pass. Let me verify the
test_bedrock_strict_tool_definition_nonetest is checking the right thingAssistant
Let me run all Bedrock provider tests (without VCR) to see the complete
Assistant
The results show 46 passed, only 2 failed - exactly the two tests that expected
is_strict_compatible=Trueforstrict=None, which is the behavior we intentionally changed.Let me verify the final state of our change with a diff:
Assistant
The diff looks good. There's a minor whitespace change - a blank line was removed. Let me restore it to keep the diff minimal
Assistant
Oh no, the edit doubled
schema.pop('$schema', None). Let me fix this by removingAssistant
Let me verify the final diff
Assistant
Let me run a final verification:
Assistant
The fix is working correctly. Let me now go through the checklist:
Checklist
-
If an error was reported in the issue description, I have successfully reproduced it.
- ✅ Confirmed: Before the fix,
BedrockJsonSchemaTransformerwithstrict=Nonehadis_strict_compatible=True(the default from the base class) for simple schemas, causing to auto-promote to , which could hit Bedrock's 20-strict-tool limit.
- ✅ Confirmed: Before the fix,
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.