-
Notifications
You must be signed in to change notification settings - Fork 535
Description
Bug Description
When using BedrockModel with Anthropic Claude models and adding cachePoint content blocks to messages (as documented in the integration tests), the Bedrock API throws a ValidationException because the _format_bedrock_messages method passes through cachePoint blocks as-is without proper transformation.
Error:
botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the ConverseStream operation: The model returned the following errors: messages.0.content.2.type: Field required
Steps to Reproduce
from strands import Agent
from strands.models import BedrockModel
from strands.types.content import Messages
model = BedrockModel(
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
region="us-east-2"
)
messages: Messages = [
{
"role": "user",
"content": [
{"text": "Some long text..." * 1000},
{"cachePoint": {"type": "default"}}, # This causes the error
],
},
]
agent = Agent(model=model, messages=messages)
agent("What is the content about?")Expected Behavior
The cachePoint content block should be properly handled, similar to how AnthropicModel handles it (see src/strands/models/anthropic.py:186-188). The cache point should be applied to the previous content block and then the cachePoint block itself should be removed from the content array before sending to Bedrock.
Actual Behavior
The cachePoint block is passed through as-is in the content array to Bedrock's API, which doesn't recognize it as a valid content block type (expecting types like text, image, document, etc.). This causes a validation error.
Root Cause
In src/strands/models/bedrock.py:276-354, the _format_bedrock_messages method doesn't handle cachePoint content blocks. It only filters:
SDK_UNKNOWN_MEMBERblocks- DeepSeek
reasoningContentblocks - Cleans
toolResultblocks
But it doesn't handle cachePoint blocks like the Anthropic model provider does.
Proposed Fix
Add cache point handling in _format_bedrock_messages, similar to the Anthropic model:
# Handle cache points by applying them to the previous content block
if "cachePoint" in content_block:
if cleaned_content:
# Add cachePoint to the previous block
cleaned_content[-1] = {**cleaned_content[-1], "cachePoint": content_block["cachePoint"]}
continueAdditional Context
- The integration test
tests_integ/test_bedrock_cache_point.pydemonstrates that cache points should work with Bedrock - Currently,
BedrockModelonly supports cache points for system prompts (cache_prompt) and tools (cache_tools), but not for message content blocks - Related issues: [FEATURE] cache_messages argument for Bedrock model provider #404, [FEATURE] Amazon Nova tool caching with cachePoint in messages #449 (but those are feature requests for automatic cache point insertion, not bug reports about manual insertion)
Environment
- Strands SDK version: [latest from repo]
- Python version: 3.12.11
- boto3 version: [latest]
- Model:
us.anthropic.claude-sonnet-4-20250514-v1:0 - Region:
us-east-2