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 analyze the issue:
- The problem is that in Pydantic AI 1.101.0, when
ToolDefinition.strict=None, the Bedrock schema transformer is auto-promoting it tostrict=Truefor simple schemas. - Bedrock/Claude only supports 20 strict tools maximum, so when there are many tools (e.g., 73), the request fails.
- The fix should be similar to how Anthropic handles this - only send
toolSpec.strict = truewhen the user explicitly setstrict=True, not when it'sNone.
Let me start by exploring the repository structure to understand the codebase.
- The problem is that in Pydantic AI 1.101.0, when
Assistant
Let me look at the Bedrock provider file and the Anthropic provider file to understand how they handle strict mode.
Assistant
I need to use the correct syntax for the open command. Let me just use the path without the --file argument.
Assistant
Now let me look at the Bedrock model file to see how
_map_tool_definitionhandles strict mode:Assistant
Now I can see the issue. On line 502, the code checks
if f.strict and self.profile.bedrock_supports_strict_tool_definition:. Whenf.strictisNone, this evaluates toFalsebecauseNoneis falsy. But the issue mentions_customize_tool_def()which replacesstrict=Nonewithstrict=True. Let me find where_customize_tool_defis defined:Assistant
Now I understand the issue. The
_customize_tool_deffunction (line 1733-1743) does the following:- Creates a
schema_transformerwithstrict=tool_def.strict(which isNoneby default) - Walks the schema, which may set
is_strict_compatibletoTrueif the schema is simple enough - Returns a new tool definition with
strict=schema_transformer.is_strict_compatible if tool_def.strict is None else tool_def.strict
So when
tool_def.strict is None, the returned tool definition will havestrict=Trueif the schema is "simple enough" (i.e.,is_strict_compatibleisTrue).Then in
bedrock.pyline 502, it checksif f.strict and ..., and sincef.strictis nowTrue, it setstool_spec['strict'] = f.strict.The issue suggests that we should not auto-promote from
strict=Nonetostrict=Truefor Bedrock tools, similar to how Anthropic handles it. Let me look at how Anthropic handles this:- Creates a
Assistant
Let me see the full AnthropicJsonSchemaTransformer:
Assistant
Now I understand the key difference. In
AnthropicJsonSchemaTransformer.walk(), line 158 sets:self.is_strict_compatible = self.strict is True # not compatible when strict is False/NoneThis means that for Anthropic,
is_strict_compatibleis onlyTruewhenstrictis explicitlyTrue. WhenstrictisNone,is_strict_compatibleisFalse.But in
BedrockJsonSchemaTransformer, there's no override ofwalk(), so it uses the default behavior fromJsonSchemaTransformerwhich auto-promotes simple schemas to strict-compatible whenstrict=None.Let me check the base
JsonSchemaTransformerclass:Assistant
Now I understand the full picture:
- In the base
JsonSchemaTransformer.__init__,is_strict_compatibleis initialized toTrue(line 46). - In
BedrockJsonSchemaTransformer.transform(), if there are incompatible keys, it setsself.is_strict_compatible = Falsewhenself.strict is None. - So for simple schemas without incompatible keys,
is_strict_compatiblestaysTrue, and_customize_tool_defwill setstrict=True. - This causes Bedrock to reject requests with >20 strict tools.
The fix should be similar to Anthropic: in
BedrockJsonSchemaTransformer, overridewalk()(or modify the logic) so thatis_strict_compatibleis onlyTruewhenstrictis explicitlyTrue(not when it'sNone).Let me look at the existing tests to understand the expected behavior better:
- In the base
Assistant
I see there's a test
test_bedrock_strict_tool_definition_nonethat expectsstrict=Noneto result in no strict field being sent. Let me also check if there are tests for the_customize_tool_defbehavior with Bedrock:Assistant
Let me search for tests related to
_customize_tool_defin the test files.Assistant
Let me look at the Bedrock provider tests to understand the expected behavior better:
Assistant
Now I can see the issue clearly. The tests at line 483-492 show that with
strict=None, when the schema is compatible (simple),is_strict_compatibleisTrue. This is what causes the problem because_customize_tool_defthen setsstrict=Truefor these tools, leading to Bedrock rejecting requests with >20 strict tools.The fix should make
BedrockJsonSchemaTransformerbehave likeAnthropicJsonSchemaTransformer- only setis_strict_compatible = Truewhenstrictis explicitlyTrue. Let me implement this fix:Assistant
I need to use a different approach to open the file. Let me try without the file parameter.
Assistant
Now I understand the structure. The fix is similar to what Anthropic does - override the
walk()method inBedrockJsonSchemaTransformerto setis_strict_compatiblebased on whetherstrictis explicitlyTrue.Let me now create a test script first to reproduce the issue:
Assistant
Let me check how to run Python in this environment.
Assistant
I need to use the virtual environment's Python to run the test script.
Assistant
I've confirmed the bug. The issue is that when
strict=None, the_customize_tool_deffunction auto-promotes it tostrict=Truefor simple schemas. This causes Bedrock to reject requests with more than 20 tools.Now let me implement the fix. Similar to
AnthropicJsonSchemaTransformer, I need to override thewalk()method inBedrockJsonSchemaTransformerto setis_strict_compatibletoFalsewhenstrictis not explicitlyTrue:Assistant
Now I'll modify the
BedrockJsonSchemaTransformerclass. I need to:- Override the
walk()method to setis_strict_compatible = self.strict is Trueat the end, similar to Anthropic. - Update the docstring to reflect the new behavior.
- Remove the code that sets
is_strict_compatible = Falseintransform()whenstrict is None, sincewalk()will now handle this.
Let me make the changes:
- Override the
Assistant
The fix has been applied. Now let me verify the changes and run the test script to confirm the issue is resolved.
Assistant
Good, the changes look correct. Now let me run my test script again to confirm the fix works:
Assistant
The fix works. Now let me also verify that the
_map_tool_definitioninBedrockConverseModelstill works correctly with the new behavior:Assistant
All assertions passed. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
Run existing tests to validate my changes </tool_call>
Assistant
Let me run the existing tests without the timeout flag.
Assistant
I see the test failures. The tests expect the old behavior where
strict=Noneauto-promotes tostrict=True. According to the instructions, I should not modify tests, but let me check if there's a way to fix my implementation while still addressing the core issue.Looking at the failures more carefully, I see that my fix also changed the behavior of adding
additionalProperties: Falsewhenstrict=None. The core issue is about auto-promotingstrict=Nonetostrict=Trueon the tool definition, not about schema transformations. Let me update my fix to preserve theadditionalProperties: Falsebehavior while still not auto-promoting:Assistant
The test failures are expected because the tests were written for the old behavior. According to the instructions, I should NOT modify existing tests. The issue description says:
"If existing tests break because they need to be updated to reflect the changes you made, just ignore it."
So these test failures are expected and I should proceed. But let me verify my fix is correct by checking the behavior more carefully. The key insight is:
- When
strict=None, we should NOT auto-promote tostrict=True(this is the fix) - The schema transformation behavior (like adding
additionalProperties: False) should still work for schema guidance purposes
However, looking at the Anthropic implementation more carefully, I see that Anthropic's
walk()method only callstransform_schema()whenstrict is True, and otherwise returns the schema as-is. Let me look at what the tests are actually checking and see if my fix is correct.- When
Assistant
Good, only 2 tests failed now (down from 4), and both are asserting the old behavior where
strict=Noneshould result inis_strict_compatible=True. These tests need to be updated to reflect the new behavior (which is intentional), but according to the instructions, I should NOT modify tests.Let me also run the strict tool calls tests for Bedrock to make sure they still pass:
Assistant
All 7 strict tool call tests passed. Let me also run my reproduction test again to confirm the fix:
Assistant
The test confirmed that the fix works. Let me also run the tool mapping test to make sure everything is working correctly.
Assistant
All tests pass. Let me verify the final state of my changes and run a broader set of tests to make sure nothing else is broken.
Assistant
Let me run a broader test to ensure the fix doesn't break other parts of the codebase.
Assistant
All Bedrock model tests pass. Let me view the final diff of my changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.