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
Let me explore the repository structure and find the relevant Bedrock files.
Assistant
Let me look at the
_customize_tool_defmethod and how the strict inference happens.Assistant
Now let me look at the Anthropic provider's approach, which the issue references as a model for the fix:
Assistant
The Anthropic approach:
self.is_strict_compatible = self.strict is True— only compatible when explicitly set to True.For Bedrock, the fix should be similar: only set
strict=Truewhen the user explicitly setstrict=True. Let me look at the full Bedrock transformer'swalkandtransformmethods.Assistant
Now I understand the issue. The Bedrock transformer auto-promotes
strict=Nonetostrict=Trueby settingis_strict_compatible = Trueby default (in the base class), and only setting it toFalsewhen there are incompatible keys.The fix should be similar to Anthropic: only set
is_strict_compatible = Truewhenself.strict is True.Let me look at the base
JsonSchemaTransformerto understandis_strict_compatibledefault andwalk.Assistant
Now I understand. The base class defaults
is_strict_compatible = True. The Bedrock transformer only sets it toFalsewhen there are incompatible keys. Sostrict=Noneschemas get auto-promoted tostrict=True.The fix: similar to Anthropic, the Bedrock transformer should only set
is_strict_compatible = Truewhenself.strict is True. Let me add awalkmethod to the BedrockJsonSchemaTransformer that setsis_strict_compatible = self.strict is Trueat the end, like Anthropic does.Wait, but the Bedrock transformer's
transformmethod does meaningful work forstrict=None(auto-promoting). The issue is that this auto-promotion is causing the >20 strict tools limit.Let me re-read the issue carefully. The issue says: "For Bedrock, strict=None should not silently turn large toolsets into strict toolsets that violate Bedrock limits." And the suggested fix: "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."
Assistant
There's a test
test_bedrock_strict_tool_definition_nonethat saysstrict=None → no strict field. But that test calls_map_tool_definitiondirectly withstrict=None(not going through_customize_tool_def). So_map_tool_definitionchecksif f.strict and ...— when strict is None,f.strictis falsy, so it doesn't set strict.But the issue is about the path through
_customize_tool_def, which replacesstrict=Nonewithis_strict_compatible. So after customization, strict becomes True for simple schemas.Now, the test
test_bedrock_strict_tool_definition_nonedirectly tests_map_tool_definitionwithstrict=None, which won't go through customization. That's fine.But what about the auto-promotion behavior? Let me check what tests exist in
tests/providers/test_bedrock.pyfor the schema transformer.Assistant
Assistant
Assistant
is_strict_compatibleis only initialized to True in the base class and not referenced elsewhere in the base. So overriding it inwalkafter super().walk() is fine.Now, the question is whether I should keep the
transformmethod's strict=True behavior intact (it adds additionalProperties: False and strips incompatible keys for strict=True). Yes, that's correct behavior for explicit strict=True.Let me now look at the
_handle_object,_handle_arrayin base to make sure the Bedrock transformer doesn't override them.Assistant
The Bedrock transformer doesn't override
_handle_objectetc., onlytransform. Good.Now let me write the fix. I'll modify the
transformmethod to only apply strict transformations whenself.strictis True, and add awalkmethod to setis_strict_compatible = self.strict is True.Let me also update the docstring of the transformer class.
Let me write the new transform method and walk method.
Assistant
Now let me check the NativeOutput path in models/bedrock.py where it forces strict=True for the output object.
Assistant
The NativeOutput path forces strict=True on the output object, which is correct. That path explicitly sets strict=True, so my
walkoverride will setis_strict_compatible = self.strict is True= True, and the transform will apply. Good.Now let me write a reproduction/test script to verify the behavior.
Assistant
The fix works. Now let me verify the full flow through
_customize_tool_defand_map_tool_definition.Assistant
Now let me run the existing transformer tests in
tests/providers/test_bedrock.pyto see which ones break (they should only break due to the intended behavior change).Assistant
The test
test_strict_none_preserves_schemanow assertsis_strict_compatible is Trueand the schema hasadditionalProperties: False. Both will fail because:is_strict_compatibleis now False- The schema no longer has
additionalProperties: Falseadded for strict=None
These are the intended behavior changes. Per the instructions, I should NOT modify existing tests, and tests that break because they need to be updated to reflect the change should be ignored. The evaluation system won't take them into account.
But wait, let me re-read the instructions carefully:
"* However UNDER NO CIRCUMSTANCES should you modify existing tests or add new tests to the repository. This will lead to an error in the system that evaluates your performance. Instead, you can just a temporary script, use it to test changes and remove it before submitting.
- 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."
So I should NOT modify existing tests. These tests will break and that's expected. The evaluation system will use its own updated tests.
Assistant
Let me look at the 4 failing tests more carefully to confirm they all reflect intended changes:
Assistant
The 4 failing tests all reflect the intended behavior change. Note
test_strict_none_with_additional_properties_truepasses because it assertsis_strict_compatible is False(which is still False) and the schema preservesadditionalProperties: True.These are all the intended behavior changes per the issue. The evaluation system will have its own updated tests.
Now let me check the VCR-based tests that might be affected. Let me look for tests using strict=None tools (not explicit) that would generate different requests.
Assistant
The VCR tests use strict=True explicitly (the cassettes have
"strict": true). Thetest_bedrock_mixed_strict_tool_runusesstrict=Trueandstrict=Falseexplicitly. Let me check the cassette for the mixed run to make sure my change doesn't break the strict=False tool's schema.Assistant
Interesting. The cassette shows
capital_lookup(strict=False) withadditionalProperties: falseand NO strict field. With the OLD code, strict=False would NOT add additionalProperties: false via the transformer. Let me check whether pydantic's generated tool schema already includes additionalProperties: false.Assistant
So pydantic-ai's tool schema generation already includes
additionalProperties: Falseby default. So the cassette havingadditionalProperties: falsefor capital_lookup comes from pydantic's schema generation, NOT from the transformer.So my change does NOT break the cassette for
capital_lookup(strict=False) — the additionalProperties: false is already there from pydantic. Good.Assistant
Only the strict tool call cassettes have
"strict": true, and those are for tools with explicitstrict=True. The maintests/models/test_bedrock.pycassettes don't have"strict": truefor tools. Let me check why - maybe the default test model doesn't support strict tool definitions.Assistant
So the
_StubBedrockProviderusesDEFAULT_PROFILEwhich hassupports_json_schema_output: bool = False. But that's a plainModelProfile, not aBedrockModelProfile. Sobedrock_supports_strict_tool_definitionwould not be set (it's a different profile class). Let me check howbedrock_supports_strict_tool_definitionis accessed.Actually wait, the stub provider returns DEFAULT_PROFILE which is
ModelProfile(). The BedrockConverseModel expects aBedrockModelProfile. But since these are dataclasses, accessingbedrock_supports_strict_tool_definitionon a plain ModelProfile would fail with AttributeError unless... let me check how the profile is used.Actually, let me check how the profile is merged/used. The
Model.profileis constructed via_infer_profileor passed. Let me check.Actually, the key point is: the VCR tests in tests/models/test_bedrock.py use the real
bedrock_providerfixture (which uses the real bedrock profile for the model). Let me check the conftest for bedrock_provider.Assistant
The VCR tests use the real
BedrockProviderwhich uses the real model profile. For models that support strict (like claude-sonnet-4-5), the OLD behavior would sendstrict: truefor default tools (strict=None). Let me check the main test_bedrock.py cassettes directory for"strict".Assistant
No
"strict": truein the main test_bedrock cassettes. Let me check what "strict" matches in those 4 cassettes that mention strict.Assistant
Those are just
strict-transport-securityheaders. So no actual"strict": truetool fields in the main test_bedrock cassettes.Let me find cassettes in test_bedrock that have
toolSpecto understand whether tools are sent with strict.Assistant
So in
test_bedrock_cache_write_and_read.yaml, the toolscatalog_lookupanddiagnosticsare sent WITHOUT a"strict": truefield, even though the model isclaude-sonnet-4-5which supports strict tool definitions. Let me check the test itself to understand.Assistant
These tools
catalog_lookupanddiagnosticsare@agent.tool_plain(default strict=None). The cassette shows no"strict": truefield. But the model is claude-sonnet-4-5 which supports strict.So the cassette was recorded BEFORE the auto-promotion feature (1.101.0). Let me verify the current state - let me check git log and whether the OLD code would have caused this cassette to mismatch.
Assistant
So the #4237 PR added the auto-promotion feature. Let me understand the VCR matching config to know whether body differences cause failures.
Assistant
The VCR config doesn't set
match_on, so it uses the default which is['method', 'scheme', 'host', 'port', 'path', 'query']— NOT body. So VCR matches on method+uri, NOT request body. This means request body differences (like adding/removingstrict: true) would NOT cause a cassette mismatch during replay!So VCR tests won't break due to body differences. But let me check if any tests assert on request bodies containing
strictfor default tools.Assistant
The native output tests use NativeOutput, which forces strict=True on the output object. So those are unaffected by my change (the output object explicitly has strict=True, so is_strict_compatible = True, transform applies). Good.
Let me verify the order:
_customize_output_objectis called AFTER prepare_request forces strict=True.Assistant
So
prepare_requestcallscustomize_request_parameterswhich calls_customize_tool_defand_customize_output_object. But the BedrockConverseModel.prepare_request (overridden) forces strict=True on the output object BEFORE callingsuper().prepare_request(). Let me verify the order in the Bedrock model.Assistant
The Bedrock
prepare_requestforces strict=True on the output object, then callssuper().prepare_requestwhich callscustomize_request_parameters→_customize_output_objectwith strict=True. So mywalkoverride setsis_strict_compatible = self.strict is True= True, and the transform applies additionalProperties: False.Let me verify the transformer behavior directly for NativeOutput (strict=True on a nested object without extra='forbid'), which is what
test_bedrock_native_output_nested_objects_without_extra_forbidrelies on.Assistant
My test assertion was wrong — the nested address is a
$refresolved into$defs, and$defs.Addresscorrectly hasadditionalProperties: False. The code is fine. Let me fix the test assertion.Assistant
Now let me verify the strict=True stripping behavior for incompatible keys still works.
Assistant
The strict=True stripping behavior still works. Now let me run the full provider
test_bedrock.pytest suite to see the overall state.Assistant
So 4 failures, all in the strict=None tests, all reflecting the intended behavior change. These tests document the OLD auto-promotion behavior and need updating to reflect the new behavior (per instructions, I should not modify them; the evaluation system will have updated versions).
Let me reconsider my approach to ensure it faithfully mirrors Anthropic (which the issue explicitly references). Let me look at the Anthropic transformer once more and confirm my Bedrock implementation matches its pattern for strict=None.
Assistant
My implementation mirrors Anthropic exactly. Now let me write a comprehensive end-to-end test simulating the issue scenario (73 tools on a strict-supporting model), verifying no tool gets a
strictfield.Assistant
Let me get the profile via the provider's model_profile method instead.
Assistant
The fix works end-to-end. Now let me run the broader test suite to make sure I didn't break unrelated things. Let me run the bedrock model tests and the strict tool call tests that don't require network.
Assistant
Now let me run the full provider
test_bedrock.pyto confirm only the 4 expected failures remain (the ones that assert the old auto-promotion behavior).Assistant
Only the 4 expected failures remain — all asserting the OLD auto-promotion behavior. These need to be updated to reflect the new behavior, which the evaluation system will handle.
Let me run the non-VCR bedrock model tests (stub provider) to make sure nothing else broke.
Assistant
All tests in test_bedrock.py are VCR tests. Let me try running a couple of VCR tests in replay mode to confirm they still pass with my change (since VCR matches on URI not body, body changes shouldn't matter, but let me verify).
Assistant
The VCR test passes. Let me run the strict tool calls VCR tests (explicit strict=True, should pass) and a few tool-using tests.
Assistant
All 20 VCR tests in the bedrock feature tests pass. Let me run a broader set of the test_bedrock.py VCR tests, especially tool-using ones.
Assistant
All 54 tool/structured/cache/retry/code_execution/anthropic VCR tests pass. Let me run the entire
test_bedrock.pyto be thorough.Assistant
All 122 tests in test_bedrock.py pass. Let me also run the full bedrock feature tests directory and the providers test (excluding the 4 known-expected failures).
Assistant
Only the 4 expected failures (which assert the old auto-promotion behavior) remain. Everything else passes — all 122 test_bedrock.py VCR tests and all 20 bedrock feature tests.
Let me do a final review of my change.
Assistant
The diff looks clean and faithful to the Anthropic approach. Let me verify the file compiles and imports correctly, then clean up temp files.
Assistant
Let me check if there are other places that rely on the Bedrock transformer's old strict=None auto-promotion behavior.
Assistant
The transformer is only used in
providers/bedrock.py(registered on profiles) and in tests. My change is contained to the transformer class itself. Let me verify thetest_strict_none_with_additional_properties_truetest still passes (it assertsis_strict_compatible is FalseandadditionalProperties: Truepreserved).Assistant
All the strict=False and additionalProperties:True preservation tests pass. Let me run a quick lint check on the modified file if ruff is available.
Assistant
Ruff passes. Let me refine the comment in my change — I referenced issue #4321 which I guessed; let me make it reference the known PR #4237 that introduced the auto-promotion, to avoid an inaccurate issue number.
Assistant
The change looks good. Let me run ruff once more and re-run the key tests to confirm everything still works.
Assistant
All 142 bedrock model tests pass and ruff is clean. Let me verify there's no code path where a tool with
strict=Nonereaches_map_tool_definitionwithout going through_customize_tool_def(which would now correctly resolve strict to False).Assistant
All tool definitions go through
_customize_tool_def(which buildstool_defsfromfunction_tools+output_tools, both customized incustomize_request_parameters). So every tool'sstrictis resolved (None →is_strict_compatible, now False for Bedrock) before reaching_map_tool_definition. The fix is complete and consistent.Let me do a final full diff review.
Assistant
The diff is clean and complete. Let me run one final verification of the complete bedrock test suite plus the e2e reproduction scenario to confirm everything is in order.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.