Skip to content

fix: Remove status field from toolResult for non-claude 3 models in Bedrock model provider #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
17 changes: 13 additions & 4 deletions src/strands/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,19 @@ 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"]
)
model_id = self.config.get("model_id")
if "claude-3" in model_id:
Copy link
Member

Choose a reason for hiding this comment

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

In a comment you indicated:

During my testing, I was able to test with multiple models and I had varying results in comparison with the issue reporter in #554. For example, I was able to get successful tool execution with the status field for models:

anthropic.claude-3-7-sonnet-20250219-v1:0
anthropic.claude-opus-4-20250514-v1:0

Which makes me wonder if we need to expand this to include all anthropic models.

# Keep the status field for Claude models
cleaned_tool_result = ToolResult(
content=tool_result["content"],
toolUseId=tool_result["toolUseId"],
status=tool_result["status"],
)
else:
# For non-Claude models, use ToolResult without status
cleaned_tool_result = ToolResult(
toolUseId=tool_result["toolUseId"], content=tool_result["content"]
)

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 @@ -7,7 +7,7 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Literal, Protocol, Union
from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Literal, Optional, Protocol, Union

from typing_extensions import TypedDict

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

content: list[ToolResultContent]
status: ToolResultStatus
status: Optional[ToolResultStatus]
toolUseId: str


Expand Down
60 changes: 59 additions & 1 deletion tests/strands/models/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,65 @@ def test_format_request_cleans_tool_result_content_blocks(model, model_id):

# Verify toolResult only contains allowed fields in the formatted request
tool_result = formatted_request["messages"][0]["content"][0]["toolResult"]
expected = {"content": [{"text": "Tool output"}], "toolUseId": "tool123", "status": "success"}
expected = {"content": [{"text": "Tool output"}], "toolUseId": "tool123"}
assert tool_result == expected
assert "extraField" not in tool_result
assert "mcpMetadata" not in tool_result


def test_format_request_removes_status_field_for_non_claude_models(model, model_id):
"""Test that format_request removes status field for non-Claude models."""
# Update model to use a non-Claude model
model.update_config(model_id="us.writer.palmyra-x4-v1:0")

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 for non-Claude models
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_for_claude_models(model, model_id):
"""Test that format_request keeps status field for Claude models."""
# Update model to use a Claude model
model.update_config(model_id="anthropic.claude-3-sonnet-20240229-v1:0")

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

formatted_request = model.format_request(messages)

# Verify toolResult contains status field for Claude models
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