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
pydantic-ai-slimversion 1.101.0 has a regression whereToolDefinition.strict=Noneis being auto-promoted tostrict=Truefor Bedrock/Claude tools. - This causes Bedrock to reject requests with more than 20 strict tools.
- The fix should be similar to how Anthropic handles it - only send
toolSpec.strict = truewhen the user explicitly setstrict=True, not when it'sNone.
Let me first explore the repository structure to understand the codebase.
- The problem is that
Assistant
I need to look at the Bedrock provider code to understand the issue. Let me search for files related to Bedrock.
Assistant
Let me examine the Bedrock model and provider files to understand the issue:
Assistant
The environment doesn't support the
opencommand with file argument. Let me usebashto read the files.Assistant
Now let me look at the Bedrock model code where tool definitions are mapped:
<tool_call> <function=bash_terminal> <parameter=command> cat /pydantic-ai/pydantic_ai_slim/pydantic_ai/models/bedrock.py </tool_call>
Assistant
Let me try using bash to read the bedrock model file.
Assistant
Let me search for the relevant part about
_customize_tool_defandstricthandling in the bedrock model:Assistant
Let me look at the
_map_tool_definitionmethod more closely:Assistant
Now let me look at the Anthropic provider to see how they handle the
strictcase:Assistant
Let me look at how Anthropic provider's tool definition customization works:
Assistant
I see! The key difference is:
In Anthropic's provider (line 158):
self.is_strict_compatible = self.strict is True # not compatible when strict is False/NoneBut in Bedrock's provider, looking at the
BedrockJsonSchemaTransformer.transform()method, I can see:if self.strict is None: ... else: self.is_strict_compatible = FalseWait, let me look at the Bedrock transformer more carefully to see the issue. The problem is that the Bedrock transformer may be setting
is_strict_compatible = Truefor simple schemas whenstrict=None, whereas Anthropic only sets it whenstrict is True.Let me look at the full Bedrock transformer:
Assistant
Let me look at the parent class
JsonSchemaTransformerto understand the default value ofis_strict_compatible:Assistant
Now I understand the issue. Let me look at the parent class to understand the full picture.
Assistant
Now let me find where
is_strict_compatibleis used to determineToolDefinition.strict:Assistant
Now let me look at the
_customize_tool_deffunction and understand the flow better:Assistant
Now I understand the issue. Let me check the test file for Bedrock to understand the test setup:
Assistant
Now I fully understand the issue. Let me summarize:
- The
BedrockJsonSchemaTransformersetsis_strict_compatible = Trueby default (inherited fromJsonSchemaTransformerparent class). - When
strict=Noneand the schema has no incompatible keys,is_strict_compatiblestaysTrue. - In
_customize_tool_def(), whentool_def.strict is None, it usesschema_transformer.is_strict_compatibleto set the finalstrictvalue. - This causes tools with
strict=None(the default) to be auto-promoted tostrict=Truefor Bedrock, leading to the "Too many strict tools" error when there are >20 tools.
The fix is to change the Bedrock transformer to NOT auto-promote
strict=Nonetostrict=True, similar to how Anthropic handles it. Let me first look at the tests to understand what edge cases need to be preserved:- The
Assistant
Now I understand the issue. Let me trace through the code:
BedrockJsonSchemaTransformerinherits fromJsonSchemaTransformer, which defaultsis_strict_compatible = True(line 41 in_json_schema.py)- When
strict=Noneand the schema has no incompatible keys,is_strict_compatiblestaysTrue _customize_tool_def()usesis_strict_compatibleto overridetool_def.strictwhen it'sNone- This causes tools with
strict=Noneto be auto-promoted tostrict=Truefor Bedrock
The fix is similar to Anthropic's provider: set
is_strict_compatible = Falsewhenstrict is None, so only explicitly setstrict=Truetools get auto-promoted.Let me implement the fix:
Assistant
Now I understand the issue clearly. Let me fix it by modifying the
BedrockJsonSchemaTransformer.transform()method to NOT auto-promotestrict=Nonetostrict=True, similar to how Anthropic handles it.Assistant
Let me try using the edit command with the proper format using heredoc syntax.
Assistant
Let me try a different approach using sed to make the changes:
Assistant
Let me try using the edit command with a different approach. I'll need to use proper line range syntax.
Assistant
Let me use Python to make the edit to the file:
Assistant
Let me verify the change was applied correctly:
Assistant
The fix has been applied. Now let me run the existing tests to verify the fix works correctly. Let me first check the test files:
Assistant
Let me install the package first:
Assistant
Let me try a different approach to install the package.
Assistant
Now let me run the tests to verify the fix works correctly:
Assistant
Let me install the required dependencies:
Assistant
I need to install more dependencies to run the tests. Let me try installing what's needed.
Assistant
Let me create a simple test script to verify the fix works:
Assistant
Let me create a simple test script to verify the fix works correctly:
Assistant
I need to install boto3 to run the test.
Assistant
Good, boto3 is now installed. Let me run the test script again.
Assistant
All tests pass. Let me also run a test to verify the Bedrock model's
_map_tool_definitionbehavior to make sure the fix propagates correctly:Assistant
Let me update the test to use a mock client:
Assistant
All tests pass. The fix is working correctly. Now let me check if there are any existing tests that might be affected by this change:
Assistant
Let me look at the test file that will be affected by this change:
Assistant
The tests show that the current behavior expects
is_strict_compatible is Trueforstrict=Nonewith simple schemas. My fix changes this behavior to NOT auto-promotestrict=Nonetostrict=True(which is the correct fix for the issue).As per the instructions: "If existing tests break because they need to be updated to reflect the changes you made, just ignore it. Evaluation system will not take it into account."
Let me verify the complete change is correct and look at the final diff:
Assistant
The fix is correctly applied. Let me verify one more time that the fix is complete by running a comprehensive test that simulates the original issue scenario:
Assistant
The fix is complete and verified. Let me provide a summary of what was done:
Summary of Changes
The issue was that
pydantic-ai-slimversion 1.101.0 auto-promotedToolDefinition.strict=Nonetostrict=Truefor Bedrock/Claude tools. This caused Bedrock to reject requests with more than 20 strict tools (the provider limit).Root Cause
The
BedrockJsonSchemaTransformerclass in/pydantic-ai/pydantic_ai_slim/pydantic_ai/providers/bedrock.pywas inheritingis_strict_compatible = Truefrom the parentJsonSchemaTransformerclass and not explicitly setting it toFalsewhenstrict=None. This meant simple schemas were auto-promoted to strict mode.Fix Applied
Modified the
BedrockJsonSchemaTransformer.transform()method to:- Set
self.is_strict_compatible = self.strict is Trueat the beginning of the method - This ensures tools are NOT auto-promoted to
- Set
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.