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 understand the issue:
pydantic-ai-slim==1.101.0has a regression wherestrict=Nonefor Bedrock tools is auto-promoted tostrict=Truefor simple schemas.- Bedrock/Claude has a limit of 20 strict tools, so when there are more than 20 tools, the request fails.
- The fix should be similar to the Anthropic provider: don't auto-promote
strict=Nonetostrict=True; only sendtoolSpec.strict = truewhen the user explicitly setstrict=True.
Let me explore the repository structure and find the relevant files.
Assistant
Let me look at the Bedrock provider and the Anthropic provider to understand the current behavior and the desired fix.
Assistant
Let me issue one command at a time.
Assistant
Let me read the relevant sections of both files. First, let me find the
_customize_tool_deffunction and how strict is handled in bedrock.py.Assistant
Let me look at the relevant code in bedrock.py.
Assistant
Let me use the open command correctly - it seems the parameter name should be LINE_NUMBER not line_number. Let me try again.
Assistant
Let me try just:
open /pydantic-ai/pydantic_ai_slim/pydantic_ai/providers/bedrock.py 60Assistant
Let me look at the
_map_tool_definitionmethod:Assistant
Let me search more broadly for
_customize_tool_def:Assistant
Let me look at these key areas:
Assistant
Now let me look at the Anthropic provider to see how they handle strict:
Assistant
Now let me look at the bedrock model's
_map_tool_definition:Assistant
Let me look at the
JsonSchemaTransformerbase class:Assistant
Let me look at the base class
JsonSchemaTransformerto understandis_strict_compatible:Assistant
Let me look at the test files for Bedrock strict tool calls:
Assistant
Let me see the full Anthropic
walkmethod and its_handle_object:Assistant
Let me look at the base class
walk()method:Assistant
Let me look at the Anthropic
_handle_objectto see how it handles non-strict cases:Assistant
Now let me look at the provider tests for Bedrock strict unsupported keys:
Assistant
Let me search for those test functions:
Assistant
Let me look at the provider test file:
Assistant
Let me look at the Bedrock provider test file for relevant tests:
Assistant
Let me look at the key test functions:
Assistant
Let me also look at the test at line 861:
Assistant
Let me check how the transformer is resolved for different models:
Assistant
Let me check how
json_schema_transformeris set for Bedrock:Assistant
Now I have a clear understanding. Let me implement the fix. I need to:
- Add a
walk()method toBedrockJsonSchemaTransformerthat setsis_strict_compatible = self.strict is True(following Anthropic's pattern) - Simplify
transform()to only apply strict-specific transformations whenself.strict is True
Let me first look at the current
transformmethod one more time- Add a
Assistant
Now let me implement the changes. First, I'll modify the
transform()method:Assistant
Now let me remove the
elif self.strict is Nonebranch from the incompatible section:Assistant
Now let me add the
walk()method and update the docstring:Assistant
Now let me add the
walk()method aftertransform():Assistant
Good, now I need to update the tests. Let me look at the tests that need to change:
Assistant
Let me see the full
test_strict_none_incompatible_schema_disables_auto_strict:Assistant
Let me also check
test_strict_none_with_additional_properties_true:Assistant
Now let me update the tests. First,
test_strict_none_preserves_schema:Assistant
Now update
test_strict_none_simple_schema:Assistant
Now update
test_strict_none_incompatible_schema_disables_auto_strictto removeadditionalProperties:Assistant
Now let me check how the snapshot testing works - do snapshots need to be regenerated?
Assistant
Let me understand the snapshot mechanism:
Assistant
Let me check how
NativeOutputsetsstrict:Assistant
Let me check the
OutputObjectDefinitionclass:Assistant
Let me check how output_object.strict is used in Bedrock's prepare_request:
Assistant
Let me look at lines 483-495 more closely:
Assistant
Let me verify the final state of the Bedrock transformer:
Assistant
Good. Now let me run the Bedrock provider tests to verify the changes work:
Assistant
Let me install pydantic_ai in development mode:
Assistant
Let me try installing from the monorepo:
Assistant
Now let me install pydantic_ai_slim:
Assistant
Now let me run the Bedrock provider tests:
Assistant
Let me install test dependencies:
Assistant
Now let me try running the tests:
Assistant
Let me install VCR and any other missing test dependencies:
Assistant
Let me try running the tests again:
Assistant
Let me install the remaining test dependencies:
Assistant
Let me try running tests again:
Assistant
Let me install the pytest VCR recorder plugin:
Assistant
Let me try running the tests again:
Assistant
The tests are skipped - likely because
bedrockextra isn't installed. Let me check what the test requires:Assistant
Let me install the bedrock test dependencies:
Assistant
Let me install
mypy_boto3_bedrock_runtime:Assistant
Now let me run the tests again:
Assistant
Let me update this last test that needs fixing. Let me look at it:
Assistant
Let me see the full test:
Assistant
Let me fix this test:
Assistant
Let me check if there are extra lines at the end:
Assistant
There are duplicate closing lines. Let me fix this:
Assistant
I see the closing
)is missing. Let me check lines 875-876 and fix:Assistant
Let me add back the closing
):Assistant
I see the
}closing the dict is missing. Let me fix this properly:Assistant
Let me run the tests again:
Assistant
All 21 strict-related tests pass. Let me also run all tests in this file to make sure nothing else is broken:
Assistant
All 48 tests pass. Let me also run the Bedrock model tests related to strict tool calls:
Assistant
This is a missing dependency. Let me install it and run the specific model tests:
Assistant
Let me run the model tests again:
Assistant
All model tests pass. Let me also verify the Anthropic provider tests still pass:
Assistant
Let me now run a broader set of tests to make sure nothing else is broken. Let me run the full Bedrock model tests:
Assistant
All tests pass. Let me now review the final changes:
Assistant
The diff looks correct. Let me now create a quick reproduce script to verify the fix works end-to-end:
Assistant
Let me fix the script:
Assistant
The fix works correctly. Let me go through the checklist:
-
✅ Reproduced the error: The issue describes that
strict=Noneauto-promotes tools tostrict=True. My reproduce script confirmed the old behavior would have auto-promoted all 73 tools. -
✅ Fix resolves the error: After the fix,
is_strict_compatible=Falseforstrict=None, meaning no auto-promotion. -
✅ Edge cases considered:
strict=None: No auto-promotion (fixed)strict=True: Strict mode preservedstrict=False: No strict mode (unchanged)NativeOutput: Bedrock already forcesstrict=Trueon output objects at line 491 ofmodels/bedrock.py
-
✅ Existing tests pass:
- All 48 tests in
tests/providers/test_bedrock.pypass - All 7 tests in
tests/models/bedrock/test_strict_tool_calls.pypass
- All 48 tests in
Let me also verify there are no other tests that could be affected by running the broader Anthropic and model tests:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.