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
I'll start by exploring the repository structure to understand the codebase and locate relevant files for the issue. Let's first get an overview.
- Assistant
Let me explore the Bedrock model file to understand the issue better.
- Assistant
Let me look at the Bedrock model and provider files to understand the issue better.
- Assistant
Let me look at the specific part of the bedrock model where tool definitions are mapped, specifically looking for the
_customize_tool_defand_map_tool_definitionfunctions. - Assistant
Let me see the
_map_tool_definitionmethod more closely. - Assistant
Now let me look at the
_customize_tool_defmethod that's mentioned in the issue description. - Assistant
Let me search more broadly for
_customize_tool_defor tool customization in the codebase: - Assistant
Let me look at the
_customize_tool_deffunction: - Assistant
Now let me look at the Anthropic provider to understand how they handle this:
- Assistant
Now let me look at the
prepare_requestmethod in the bedrock model: - Assistant
Let me also check
bedrock_supports_strict_tool_definition: - Assistant
Let me look at the base
JsonSchemaTransformerclass and howis_strict_compatibledefaults work: - Assistant
Let me look at the full JsonSchemaTransformer class to understand the is_strict_compatible logic.
- Assistant
Let me look at the
BedrockJsonSchemaTransformer: - Assistant
Now I understand the issue clearly. Let me look at the full
BedrockJsonSchemaTransformerclass to understand all the logic: - Assistant
Now I understand the issue completely. Let me create a reproducer script first to confirm the issue:
- Assistant
Let me check the environment for the right way to run Python.
- Assistant
Let me check if there's a virtual environment or if the package is installed.
- Assistant
It seems like pydantic is not installed. Let me install it and the necessary dependencies.
- Assistant
Let me try installing it from the local directory directly.
- Assistant
Let me look at the repository structure more carefully.
- Assistant
Let me check if there's a virtual environment already set up.
- Assistant
Let me use the virtual environment. Let me run my reproducer script using it:
- Assistant
The issue is confirmed. When
strict=None(the default),BedrockJsonSchemaTransformerauto-promotes simple schemas tostrict=True, hitting Bedrock's limit of 20 strict tools. Let me now understand the full picture and implement the fix.Looking at the code flow:
_customize_tool_def()inmodels/__init__.pycreates aBedrockJsonSchemaTransformerwithstrict=tool_def.strict(which isNoneby default)- If
is_strict_compatibleremainsTrue(the default),tool_def.strictgets set toTrue - Then
_map_tool_definition()sendstoolSpec.strict = trueto Bedrock - Bedrock rejects requests with >20 strict tools
The fix mirrors Anthropic's approach: don't auto-promote
strict=Nonetostrict=True. - Assistant
Now I have a clear picture. The fix is straightforward - similar to the Anthropic provider's approach. When
strict=None, don't auto-promote tostrict=True. Let me implement the fix: - Assistant
The edit seems to have gone wrong. Let me check the current state:
- Assistant
The edit broke the file. Let me revert it and try again more carefully:
- Assistant
Good. Now let me use the proper edit syntax:
- Assistant
Now I can see the exact content. Let me edit the docstring and the transform method:
- Assistant
Good, the edit looks correct. Now let me verify the fix by running my reproducer script:
- Assistant
The basic test passes. Now let me create a more comprehensive test to verify the end-to-end behavior:
- Assistant
All tests pass. Now let me also verify the integration with
_customize_tool_defto ensure tools withstrict=Nonedon't get promoted: - Assistant
Now let me run the existing Bedrock-related tests to make sure I haven't broken anything:
- Assistant
Let me look at the provider test for bedrock to see what tests exist for the strict tool definition behavior:
- Assistant
I can see there are existing tests that assert
is_strict_compatible is Trueforstrict=Nonewith simple schemas. These tests need to be updated to reflect the new behavior. Let me look at those specific tests: - Assistant
Now let me look at the existing tests that will be affected:
- Assistant
Let me run the existing tests to see which ones are affected by the change:
- Assistant
The tests that check
strict=Noneauto-promotes tostrict=Truewill fail, which is exactly the behavior we're fixing. Let me continue running to see all failures: - Assistant
Two tests fail that explicitly test the old behavior that the issue considers buggy. Let me also check for other test files that might be affected:
- Assistant
Let me check the specific test that verifies
strict=Nonebehavior: - Assistant
Now I see the full picture. The two failing tests are testing the exact buggy behavior described in the issue. They were introduced in PR #4237 (which the issue author acknowledges didn't account for the >20-tool case). These tests need to be updated to reflect the correct behavior.
Let me update them:
- Assistant
Now let me update the two tests that assert the old buggy behavior:
- Assistant
Now update the assertion:
- Assistant
Now update the second test:
- Assistant
Now let me run the tests again to confirm all pass:
- Assistant
All 48 tests pass. Now let me also run the model-level bedrock tests to make sure nothing else is broken:
- Assistant
All tests pass. Let me also run the broader test suite to make sure nothing else is broken:
- Assistant
All tests pass. Let me now clean up the temporary test files and do a final review of my changes:
- Assistant
Let me review all the changes I've made:
- Assistant
The changes look clean and correct. Let me now go through the checklist and verify everything:
Checklist
✅ If an error was reported in the issue description, I have successfully reproduced it.
I confirmed the error: with
strict=Noneand a simple schema,BedrockJsonSchemaTransformer.is_strict_compatiblewasTrue, causing auto-promotion tostrict=True.✅ If an error was reported in the issue description, I have confirmed that my fix resolves the error.
After my fix,
strict=Nonecorrectly setsis_strict_compatible = False, preventing auto-promotion. Tools only getstrict=Truewhen the user explicitly sets it.✅ I have thought about edge cases and made sure my fix handles them as well.
- (default) → (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.