Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
18 changes: 14 additions & 4 deletions src/strands/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ 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. Defaults to False.
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 +90,7 @@ class BedrockConfig(TypedDict, total=False):
guardrail_redact_output_message: Optional[str]
max_tokens: Optional[int]
model_id: str
remove_tool_result_status: Optional[bool]
stop_sequences: Optional[list[str]]
streaming: Optional[bool]
temperature: Optional[float]
Expand Down Expand Up @@ -276,10 +278,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", False):
# Remove status field when explicitly configured
cleaned_tool_result = ToolResult(
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
4 changes: 2 additions & 2 deletions src/strands/types/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Literal, Protocol, Union

from typing_extensions import TypedDict
from typing_extensions import NotRequired, TypedDict

from .media import DocumentContent, ImageContent

Expand Down Expand Up @@ -91,7 +91,7 @@ class ToolResult(TypedDict):
"""

content: list[ToolResultContent]
status: ToolResultStatus
status: NotRequired[ToolResultStatus]
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to modify this? It seems like globally we always want ToolResult to contain status - and consumers may depend on status being present.

Can we just perform the filtering within the bedrock when we send the data to bedrock without propagating the issue throughout our code.

Meaning if from a tool I want to pass a ToolResult, we internally still require status to be provided

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure I follow fully, but based on what I understand and experienced during testing -- there are some models available on bedrock that don't support the status field. In order to mitigate that, we do the filtering in bedrock.py. The change here to the type was needed to pass the linter where the status field is required in all cases. But since we have models that don't require status field, we don't require the status field. We DO return the status field for all models that accept the status field.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @dbschmigelski here. ToolResult is the type that any result of a "StrandsTool" needs to return. If someone implements a custom tool, we want to require them to return the status field as part of the tools execution. We can leave this code unchanged, and still filter out status when calling bedrock.

Suggested change
status: NotRequired[ToolResultStatus]
status: ToolResultStatus

toolUseId: str


Expand Down
55 changes: 55 additions & 0 deletions tests/strands/models/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,3 +1237,58 @@ 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_by_default(model, model_id):
"""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