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 in the Bedrock provider where
ToolDefinition.strict=Noneis being auto-promoted tostrict=Truefor simple schemas - This causes Bedrock to reject requests with more than 20 strict tools
- The fix should be similar to what was done for Anthropic - don't auto-promote Bedrock tool definitions from
strict=Nonetostrict=True
Let me start by exploring the repository structure to understand the codebase.
- The problem is in the Bedrock provider where
Assistant
Let me explore the repository structure more to find the Bedrock provider and the Anthropic provider to understand how they handle the strict field.
Assistant
)
Now let me look at the Bedrock provider file to understand how it handles tool definitions.
Assistant
I need to use the correct format for the open command without extra closing parentheses.
Assistant
I need to use the correct format for the open command without extra closing parentheses.
Assistant
Let me also look at the Anthropic provider to see how they handle the strict field, as mentioned in the issue as the reference implementation:
Assistant
Now I understand the issue better. Looking at the
AnthropicJsonSchemaTransformer, I can see that in itswalk()method, it setsself.is_strict_compatible = self.strict is True, meaning it only auto-promotes to strict whenstrictis explicitlyTrue.But in
BedrockJsonSchemaTransformer, there's nowalk()method, so it uses the default behavior fromJsonSchemaTransformer. Let me look at the base class to understand the default behavior:Assistant
Now I understand the issue. The base
JsonSchemaTransformerclass initializesself.is_strict_compatible = Trueby default. Whenstrict=None, the caller usesis_strict_compatibleto determine if the tool should be promoted to strict mode.In
AnthropicJsonSchemaTransformer, thewalk()method overrides this behavior by settingself.is_strict_compatible = self.strict is True, so it only promotes to strict whenstrictis explicitlyTrue.But
BedrockJsonSchemaTransformerdoes not overridewalk(), so it keeps the defaultis_strict_compatible = True, and only sets it toFalsewhen it finds incompatible keys. This means simple schemas are auto-promoted to strict.The fix is to add a
walk()method toBedrockJsonSchemaTransformersimilar to the Anthropic one, so thatstrict=Nonedoes not auto-promote to strict.Let me also check how
_customize_tool_defuses this:Assistant
Now I can see the issue. At line 1743, when
tool_def.strict is None, it usesschema_transformer.is_strict_compatibleto determine the strict value. SinceBedrockJsonSchemaTransformerdoesn't overridewalk()to setis_strict_compatiblebased on explicit strict value, simple schemas get auto-promoted to strict.Let me look at the full context of the
_customize_tool_deffunction:Assistant
Now I understand the full picture. Let me check if there are any tests for the Bedrock provider that I should be aware of:
Assistant
Let me look at the specific test cases to understand the expected behavior:
Assistant
Interesting! Looking at the tests, I can see:
test_strict_none_simple_schemaexpectsis_strict_compatible is Truewhen the schema is simple (no constraints)test_strict_none_incompatible_schema_disables_auto_strictexpectsis_strict_compatible is Falsewhen schema has incompatible constraints
But the issue is that even simple schemas are being auto-promoted to strict, which causes problems when there are many tools (>20). The issue description says the fix should be: "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."
So the fix should make
strict=NoneNOT auto-promote to strict, similar to Anthropic. Let me look at lines 860-870 of the test file for more context:Assistant
Let me look at tests that are specifically about the auto-promotion behavior for simple schemas. According to the issue, these tests expected
is_strict_compatible is Truebut should now expectis_strict_compatible is Falseto match Anthropic behavior.Let me check the Anthropic tests to understand the expected behavior:
Assistant
Now I understand the expected behavior. Looking at lines 10-12 in the Anthropic test:
strict=True→is_strict_compatible=Truestrict=False/None→is_strict_compatible=False
The fix for
BedrockJsonSchemaTransformershould be similar - whenstrict=None, it should NOT auto-promote to strict. Only whenstrict=Trueexplicitly should it be considered strict-compatible.Let me now implement the fix by adding a
walk()method toBedrockJsonSchemaTransformersimilar toAnthropicJsonSchemaTransformer:Assistant
Now I understand the full picture. The fix needs to:
- Add a
walk()method toBedrockJsonSchemaTransformerthat setsself.is_strict_compatible = self.strict is Trueat the beginning, so that only explicitstrict=Trueresults in strict-compatible tools.
Let me look at the complete
BedrockJsonSchemaTransformerclass:- Add a
Assistant
Now I need to add a
walk()method toBedrockJsonSchemaTransformerthat setsself.is_strict_compatible = self.strict is Truebefore calling the parent'swalk(). This is exactly whatAnthropicJsonSchemaTransformerdoes.Let me create the fix by adding a
walk()method right after the class docstring and before thetransformmethod:Assistant
I need to add a
walk()method after line 76 and update the docstring. The fix involves:- Update the docstring to reflect the new behavior (no auto-promotion when
strict=None) - Add a
walk()method that setsself.is_strict_compatible = self.strict is True
Let me also update the
transformmethod since the check forself.strict is Nonethat setsself.is_strict_compatible = Falseis now unnecessary (we default to False for None).- Update the docstring to reflect the new behavior (no auto-promotion when
Assistant
It seems the
editcommand is having trouble parsing my arguments. Let me try using a simpler syntax without thefileparameter and instead just use the currently open file.Actually, looking at the error more carefully, it seems like the edit command arguments are being parsed incorrectly. Let me try using the heredoc syntax for multi-line replacement.
Assistant
The edit command is having issues. Let me use sed instead to make the changes:
Assistant
Let me use a Python script to make the changes:
Assistant
Let me verify the changes were applied correctly:
Assistant
The
walk()method has been added. Now, I notice that in thetransformmethod, there are still checks forself.strict is Nonethat setself.is_strict_compatible = False. Since thewalk()method now setsis_strict_compatible = self.strict is Trueafter calling the parent'swalk()(which in turn callstransform()for each node), these checks intransform()are still valid for marking schemas as incompatible during traversal, but the final value will be set bywalk().Wait, I need to think about this more carefully. The
walk()method callssuper().walk()first, which calls_handle()which callstransform(). Then after the walk is complete, we setself.is_strict_compatible = self.strict is True.Assistant
Looking at the
transform()method, I can see that:- When
strict=None, it still setsadditionalProperties = Falseon object schemas ifadditionalPropertiesis not explicitlyTrue - When
strict=Noneand there are incompatible keys, it setsself.is_strict_compatible = False - When
strict=Trueand there are incompatible keys, it strips them and adds to description
Since our
walk()method now overridesis_strict_compatibleto always beself.strict is True, the lines that setself.is_strict_compatible = Falseintransform()are now dead code. However, they don't hurt anything either since they will be overwritten.But I think we should also consider whether the behavior of the
transform()method forstrict=Noneis still correct. Looking at it:- When
strict=None, the code setsadditionalProperties = Falseif the schema doesn't have incompatibleadditionalProperties - This seems like a reasonable default behavior even when not in strict mode
Wait, but looking at the Anthropic transformer, it doesn't set when . Let me check:
- When
Assistant
Hmm, Anthropic's
transform()only removestitleand$schemafields. TheadditionalPropertieshandling is done by Anthropic's SDKtransform_schema()whenstrict=True.For Bedrock, the current
transform()method also setsadditionalProperties = Falsewhenstrict=None. This might be intentional for Bedrock. Let me think about what the expected behavior should be:- When
strict=True: Transform schema for strict mode (strip incompatible keys, set additionalProperties=False, etc.) - When
strict=None: Do minimal transformation - should we still setadditionalProperties=False?
- When
Assistant
Let me install the package first:
Assistant
set PYTHONPATH to include the source directories
Assistant
I need to install the required dependencies. Let me install pydantic first.
Assistant
I need to install the required dependencies. Let me install pydantic first.
Assistant
Cannot extract command from tool call
pip install opentelemetry-api boto3 -q 2>&1 | tail -2with arguments{}Assistant
Let me continue installing the dependencies and then test the fix.
Assistant
Let me continue testing the fix by running the import again.
Assistant
I need to install more dependencies. Let me install them one by one or find a better way to set up the environment.
Assistant
Let me try a different approach - install all the dependencies from the pyproject.toml or find a simpler way to test.
Assistant
Let me create a minimal test script to verify the fix works correctly.
Assistant
The fix is working correctly. Now let me also test that the actual module file I edited works correctly by testing it directly:
Assistant
The fix is working. Now let me check if I should clean up the dead code in
transform()that setsself.is_strict_compatible = Falsewhenstrict=None. Sincewalk()now always overridesis_strict_compatibletoself.strict is True, those assignments are dead code. Let me clean them up:Assistant
Let me clean up the dead code in
transform()since theis_strict_compatibleassignments there are now overwritten bywalk():Assistant
Continuing from where I left off. I need to verify the cleanup was done correctly and then run tests to make sure everything works.
Assistant
Let me verify the fix still works correctly after the cleanup, then run the actual tests to see if any need to be updated.
Assistant
The fix is working correctly. Let me verify the final state of my changes by looking at the diff:
Assistant
I have verified the fix works correctly. Let me review the final state of the changes and then submit.
Assistant
Now let me submit the changes.
Assistant
The submit command failed because of an extra closing parenthesis. Let me try again without the extra parenthesis.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.