fix(models/bedrock): validate strict_tools constraints at build time#2822
Open
FadelT wants to merge 1 commit into
Open
fix(models/bedrock): validate strict_tools constraints at build time#2822FadelT wants to merge 1 commit into
FadelT wants to merge 1 commit into
Conversation
Fixes strands-agents#2664 When BedrockModel(strict_tools=True) is used with tools that contain oneOf in their JSON schema or collectively have >24 optional parameters, the entire converse_stream() call fails with a runtime ValidationException deep in boto3. This caused an 18-hour production outage with opaque error messages. This change adds build-time validation that enforces Bedrock strict-mode constraints before the request reaches the API: 1. **oneOf detection** - Recursively scans tool schemas and rejects any containing oneOf (unsupported in Bedrock strict mode) 2. **Optional parameter limit** - Counts optional parameters across all tools (properties not in required array) and rejects if aggregate exceeds 24 3. **Clear error messages** - Names specific tools and violations with actionable remediation steps Changes: - Add validate_bedrock_strict_constraints() to _strict_schema.py - Add _has_oneof() recursive scanner - Add _count_optional_params() counter - Call validation in bedrock.py _format_request() before building request - Add 20 comprehensive tests in test_bedrock_strict.py Verified: - 20/20 new tests passing - 166/166 existing Bedrock tests still passing - Manual verification with AWS Bedrock API confirms build-time validation - Error messages list all tools with optional param counts The fix follows the fail-fast principle: validation happens at agent/model registration time (when _format_request is called), not when the API is invoked, turning an opaque production outage into an immediate, local error.
9d679a0 to
5dcc406
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
BedrockModel(strict_tools=True)is used with tools that:oneOfin their JSON schema, OR...the entire
converse_stream()call fails with a runtimeValidationExceptiondeep in boto3.Real-world impact:
strands-agents/toolsAgentCore browser tool ships with aoneOfschemaCloses #2664
Root Cause
ensure_strict_json_schema()in_strict_schema.py:additionalProperties: false)oneOfwithout checking Bedrock rejects it in strict modeValidation only happens at runtime when Bedrock API is called.
Solution
Add build-time validation in
_strict_schema.pythat checks:1. oneOf Detection
Recursively scan tool schemas for
oneOfkeyword. If found withstrict_tools=True, fail fast with:2. Optional Parameter Limit
Count optional parameters (properties not in
required) across all tools. If total > 24, fail with:3. Integration
Call
validate_bedrock_strict_constraints()in_format_request()before building the Bedrock request, only whenstrict_tools=True.Why this approach:
Verification
Unit Tests (20/20 passing)
Comprehensive test coverage in
test_bedrock_strict.py:strict_tools=Falsebypasses validationExisting Tests (166/166 still passing)
All Bedrock model tests continue to pass - no regressions.
Manual Verification with Real AWS Bedrock
Tested against actual Bedrock API (
us.anthropic.claude-haiku-4-5-20251001-v1:0):Test 1: oneOf Rejection
Test 2: Optional Parameter Limit
Test 3: Valid Tools Pass
Test 4: strict_tools=False Bypasses
Changes
src/strands/models/_strict_schema.py(+132 lines)validate_bedrock_strict_constraints()- main validation entry point_has_oneof()- recursive oneOf detector with cycle prevention_count_optional_params()- counts optional parameters in schemasrc/strands/models/bedrock.py(+3 lines)validate_bedrock_strict_constraints_format_request()before building request (only ifstrict_tools=True)tests/strands/models/test_bedrock_strict.py(+342 lines, new file)Checklist
Strategic Context
This fix prevents production outages caused by Bedrock strict-mode constraints. The fail-fast approach transforms an 18-hour, 100%-of-invocations outage with opaque errors into an immediate, local error at agent registration time with clear remediation steps.
Impact:
strict_tools=Trueactually usableReferences
strands-agents/tools)