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
I'll start by exploring the repository structure to understand the codebase and locate relevant files for this issue. Let's first check the current directory and see what's available.
- Assistant
Let me examine the bedrock provider and the relevant code to understand how tool definitions are handled.
- Assistant
Let me now look at the Bedrock model file to see how tool definitions are mapped, specifically
_map_tool_definitionand_customize_tool_defmentioned in the issue. - Assistant
Let me search for
_map_tool_definitionand_customize_tool_deffunctions in the bedrock model file, and also look at how the Anthropic provider handles the strict field. - Assistant
Let me search for
_customize_tool_defin the codebase. - Assistant
Now let me look at the
_customize_tool_deffunction and the surrounding code: - Assistant
Now let me look at the Anthropic provider to see how it handles the
strict=Nonecase. - Assistant
Let me look at the
JsonSchemaTransformerbase class to understand howis_strict_compatibleis initialized: - Assistant
Now I understand the issue. Let me look at the
JsonSchemaTransformerclass: - Assistant
Now I understand the issue. Let me check how the bedrock model is calling
_customize_tool_def: - Assistant
Let me look at how the tool definitions get transformed in the Bedrock model file:
- Assistant
Now let me look at the
_map_tool_definitionfunction: - Assistant
Now I understand the flow. Let me look at the base
Modelclass to understand howprepare_requestworks with the transformer: - Assistant
Let me check if
BedrockJsonSchemaTransformerhas awalk()method: - Assistant
Let me look at the
prepare_requestmethod more closely: - Assistant
Now I understand the issue. Let me look at how the Anthropic provider handles this to confirm the fix:
- Assistant
Now let me look at the existing tests for bedrock strict behavior:
- Assistant
Now I understand the full issue. Let me look at the existing tests to understand the expected behavior:
- Assistant
Let me look at the bedrock strict tool tests:
- Assistant
Now I understand the issue. Let me look at how the Anthropic provider handles the
is_strict_compatibleflag and also check the tests for the Bedrock schema transformer: - Assistant
Let me find the test file for bedrock:
- Assistant
Let me look at the test for bedrock in the providers directory to see what they test for strict behavior:
- Assistant
Now I understand the issue! Let me trace the flow:
_customize_tool_definmodels/__init__.pycalls the transformer withstrict=tool_def.strict(which isNoneby default)- The transformer's
walk()method callstransform()which setsis_strict_compatiblebased on the schema - For simple schemas,
is_strict_compatiblestaysTrue(default) - Back in
_customize_tool_def, whentool_def.strict is None, it usesschema_transformer.is_strict_compatibleto setstrict - So simple schemas get
strict=Trueautomatically - Then in
_map_tool_definition,if f.strict and self.profile.bedrock_supports_strict_tool_definition:sendstoolSpec.strict = trueto Bedrock
The fix should be similar to Anthropic: when
strict=None, don't auto-promote tostrict=True. Let me check what tests exist for this: - Assistant
Let me see the full test for
test_strict_none_simple_schema: - Assistant
Now I understand the issue. Let me look at the full flow to understand the problem:
_customize_tool_definmodels/__init__.pycreates aBedrockJsonSchemaTransformerwithstrict=tool_def.strict(which isNoneby default)- After
walk(), it usesschema_transformer.is_strict_compatibleto set the tool'sstrictwhentool_def.strict is None - The
BedrockJsonSchemaTransformer.transform()method only setsis_strict_compatible = Falsefor incompatible schemas, so simple schemas keepis_strict_compatible = True - This causes tools with
strict=Noneto be auto-promoted tostrict=True
The fix should be similar to Anthropic: don't auto-promote
strict=Nonetostrict=True. Let me examine the Anthropic approach and implement the fix: - Assistant
Now I understand the issue. Let me trace the flow:
_customize_tool_defcreates aBedrockJsonSchemaTransformerwithstrict=tool_def.strict(which isNoneby default)- After
walk(), it usesschema_transformer.is_strict_compatibleto set the tool'sstrictwhentool_def.strict is None - The
BedrockJsonSchemaTransformer.transform()only setsis_strict_compatible = Falsefor incompatible schemas, so simple schemas keepis_strict_compatible = True - This causes tools with
strict=Noneto be auto-promoted tostrict=True, which then sendstoolSpec.strict = trueto Bedrock
The fix should be similar to Anthropic: don't auto-promote
strict=Nonetostrict=True. Let me look at the full BedrockJsonSchemaTransformer: - Assistant
Now I understand the issue. Let me look at the
walk()method in the base class to understand the flow: - Assistant
Now I understand the issue completely. Let me trace through the flow:
_customize_tool_defcreates aBedrockJsonSchemaTransformerwithstrict=tool_def.strict(which isNoneby default)- After
walk(), it usesschema_transformer.is_strict_compatibleto set the tool'sstrictwhentool_def.strict is None - The
BedrockJsonSchemaTransformer.transform()only setsis_strict_compatible = Falsefor incompatible schemas, so simple schemas keepis_strict_compatible = True - This causes tools with
strict=Noneto be auto-promoted tostrict=True, which then sendstoolSpec.strict = trueto Bedrock
The fix should be similar to Anthropic: don't auto-promote
strict=Nonetostrict=True. Let me implement the fix by adding awalk()method toBedrockJsonSchemaTransformerthat setsself.is_strict_compatible = self.strict is Trueafter callingsuper().walk(), just like Anthropic does: - Assistant
Now I'll implement the fix. The key is to add a
walk()method toBedrockJsonSchemaTransformerthat setsself.is_strict_compatible = self.strict is Trueafter callingsuper().walk(), similar to what Anthropic does. This will prevent auto-promotion ofstrict=Nonetostrict=True: - Assistant
Now I need to add a
walk()method toBedrockJsonSchemaTransformerthat setsself.is_strict_compatible = self.strict is Trueafter callingsuper().walk(), similar to the Anthropic approach. This will prevent auto-promotion ofstrict=Nonetostrict=True: - Assistant
Now let me verify the change was applied correctly:
- Assistant
Good. Now I need to update the tests that check
is_strict_compatible is Truewhenstrict=None. Let me look at the relevant tests: - Assistant
Let me look at all the tests for
strict=Noneto understand what needs to be updated: - Assistant
Let me look at the tests that check
strict=Nonebehavior to see what needs to be updated: - Assistant
Now I need to update the tests that check
strict=Nonebehavior. These tests expectis_strict_compatible=Trueforstrict=Nonewith simple schemas, but after our fix,is_strict_compatibleshould always beFalsewhenstrict=None. Let me update the tests: - Assistant
Let me check the exact line numbers and update the assertions.
- Assistant
Let me update the assertion for
test_strict_none_preserves_schema: - Assistant
Let me use the bash terminal to do the replacement instead.
- Assistant
Let me update the two tests that check
is_strict_compatible is Truewhenstrict=None: - Assistant
Now update the second test:
- Assistant
Now update the assertion on line 516:
- Assistant
Good. Now let me check if there are any other tests in this file that have
strict=Noneand checkis_strict_compatible is True: - Assistant
Let me check the other test at line 862:
- Assistant
Good - this test already has
is_strict_compatible is False, which is correct. Let me run the bedrock tests to see if my changes work: - Assistant
I need to install the package first.
- Assistant
Let me check how to properly install the package:
- Assistant
Let me check the virtual env setup
- Assistant
Let me use the venv's Python to run the tests.
- Assistant
All the strict_none tests pass. Let me run all the bedrock tests to make sure nothing else is broken:
- Assistant
All 48 tests pass. Let me also run the bedrock strict tool call tests:
- Assistant
Let me run the native output tests too:
- Assistant
All 20 tests pass. Let me also check if there are any tests related to the
models/__init__.py_customize_tool_deffunction that might be affected: - Assistant
Let me check the test cases in test_bedrock.py related to
customize_request_parameters: - Assistant
Let me check the test at line 3600 to see if it tests the strict auto-promotion:
- Assistant
Now let me run a broader set of tests to make sure nothing is broken:
- Assistant
Let me also run the broader model tests that involve Bedrock:
- Assistant
All 122 bedrock model tests pass. Let me now write a quick script to reproduce the issue and verify it's fixed:
- Assistant
Excellent! The fix works. Let me also run the anthropic provider tests to make sure nothing is broken there:
- Assistant
Let me also run the openai profile tests to make sure nothing is broken:
- Assistant
All tests pass. Let me clean up the temporary test script:
- Assistant
Let me verify the final state of my changes:
- Assistant
The changes look correct and minimal. Let me run a final comprehensive test to make sure everything is working:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.