Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/strands/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class BedrockConfig(TypedDict, total=False):
guardrail_redact_output_message: If a Bedrock Output guardrail triggers, replace output with this message.
max_tokens: Maximum number of tokens to generate in the response
model_id: The Bedrock model ID (e.g., "us.anthropic.claude-sonnet-4-20250514-v1:0")
remove_tool_result_status: Flag to remove status field from tool results.
True removes status, False keeps status, "auto" keeps status. Defaults to None.
stop_sequences: List of sequences that will stop generation when encountered
streaming: Flag to enable/disable streaming. Defaults to True.
temperature: Controls randomness in generation (higher = more random)
Expand All @@ -89,6 +91,7 @@ class BedrockConfig(TypedDict, total=False):
guardrail_redact_output_message: Optional[str]
max_tokens: Optional[int]
model_id: str
remove_tool_result_status: Optional[Literal["auto"] | bool]
stop_sequences: Optional[list[str]]
streaming: Optional[bool]
temperature: Optional[float]
Expand Down Expand Up @@ -276,10 +279,18 @@ def _format_bedrock_messages(self, messages: Messages) -> Messages:
# Create a new content block with only the cleaned toolResult
tool_result: ToolResult = content_block["toolResult"]

# Keep only the required fields for Bedrock
cleaned_tool_result = ToolResult(
content=tool_result["content"], toolUseId=tool_result["toolUseId"], status=tool_result["status"]
)
if self.config.get("remove_tool_result_status") is True:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto now has to be handled here

# Remove status field when explicitly configured
cleaned_tool_result = ToolResult( # type: ignore[typeddict-item]
toolUseId=tool_result["toolUseId"], content=tool_result["content"]
)
else:
# Keep status field by default
cleaned_tool_result = ToolResult(
content=tool_result["content"],
toolUseId=tool_result["toolUseId"],
status=tool_result["status"],
)

cleaned_block: ContentBlock = {"toolResult": cleaned_tool_result}
cleaned_content.append(cleaned_block)
Expand Down
83 changes: 83 additions & 0 deletions tests/strands/models/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,3 +1237,86 @@ def test_format_request_cleans_tool_result_content_blocks(model, model_id):
assert tool_result == expected
assert "extraField" not in tool_result
assert "mcpMetadata" not in tool_result


def test_format_request_removes_status_field_when_configured(model, model_id):
"""Test that format_request removes status field when remove_tool_result_status=True."""
# Configure model to remove status field
model.update_config(remove_tool_result_status=True)

messages = [
{
"role": "user",
"content": [
{
"toolResult": {
"content": [{"text": "Tool output"}],
"toolUseId": "tool123",
"status": "success",
}
},
],
}
]

formatted_request = model.format_request(messages)

# Verify toolResult does not contain status field when configured to remove
tool_result = formatted_request["messages"][0]["content"][0]["toolResult"]
expected = {"toolUseId": "tool123", "content": [{"text": "Tool output"}]}
assert tool_result == expected
assert "status" not in tool_result


def test_format_request_keeps_status_field_with_auto(model, model_id):
"""Test that format_request keeps status field when remove_tool_result_status="auto"."""
# Configure model with auto setting
model.update_config(remove_tool_result_status="auto")

messages = [
{
"role": "user",
"content": [
{
"toolResult": {
"content": [{"text": "Tool output"}],
"toolUseId": "tool123",
"status": "success",
}
},
],
}
]

formatted_request = model.format_request(messages)

# Verify toolResult contains status field with auto setting
tool_result = formatted_request["messages"][0]["content"][0]["toolResult"]
expected = {"content": [{"text": "Tool output"}], "toolUseId": "tool123", "status": "success"}
assert tool_result == expected
assert "status" in tool_result


"""Test that format_request keeps status field by default."""
messages = [
{
"role": "user",
"content": [
{
"toolResult": {
"content": [{"text": "Tool output"}],
"toolUseId": "tool123",
"status": "success",
}
},
],
}
]

formatted_request = model.format_request(messages)

# Verify toolResult contains status field by default
tool_result = formatted_request["messages"][0]["content"][0]["toolResult"]
expected = {"content": [{"text": "Tool output"}], "toolUseId": "tool123", "status": "success"}
assert tool_result == expected
assert "status" in tool_result
Loading