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
20 changes: 18 additions & 2 deletions src/strands_tools/code_interpreter/agent_core_code_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,32 @@ def write_files(self, action: WriteFilesAction) -> Dict[str, Any]:
return self._create_tool_result(response)

def _create_tool_result(self, response) -> Dict[str, Any]:
"""Create tool result from response."""
"""Create tool result from response without stringifying binary content.

When reading binary files (e.g., PNG images) via readFiles, the content
may already be properly structured as a list. Stringifying it destroys
the binary data. This method preserves content structure when it's
already a list, and only wraps non-list content in text format.
"""
if "stream" in response:
event_stream = response["stream"]
for event in event_stream:
if "result" in event:
result = event["result"]
content = result.get("content")
is_error = response.get("isError", False)

# Preserve content structure if already a list (e.g., binary file content)
if isinstance(content, list):
return {
"status": "success" if not is_error else "error",
"content": content,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is content guaranteed to be the right shape? This payload comes from AgentCore.

}

# Fall back to text wrapping for non-list content
return {
"status": "success" if not is_error else "error",
"content": [{"text": str(result.get("content"))}],
"content": [{"text": str(content)}],
}

return {"status": "error", "content": [{"text": f"Failed to create tool result: {str(response)}"}]}
Expand Down
44 changes: 44 additions & 0 deletions tests/code_interpreter/test_agent_core_code_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,47 @@ def test_module_level_session_mapping():
mock_client2.get_session.assert_called_once_with(
interpreter_id="aws.codeinterpreter.v1", session_id="aws-session-123"
)


def test_create_tool_result_preserves_list_content(interpreter):
"""Test _create_tool_result preserves list content (for binary files like PNG).

When reading binary files via readFiles, the content may already be a list.
The method should preserve this structure instead of stringifying it.
Fixes: https://github.com/strands-agents/tools/issues/370
"""
# Simulate content that's already a properly structured list (like binary file data)
binary_content = [{"type": "resource", "resource": {"blob": b"\x89PNG\r\n"}}]
response = {"stream": [{"result": {"content": binary_content}}], "isError": False}

result = interpreter._create_tool_result(response)

assert result["status"] == "success"
# Content should be preserved as-is, not wrapped in str()
assert result["content"] == binary_content
assert result["content"][0]["type"] == "resource"


def test_create_tool_result_wraps_string_content(interpreter):
"""Test _create_tool_result wraps non-list content in text format.

For backward compatibility, when content is a string or other non-list type,
it should be wrapped in [{"text": str(content)}].
"""
response = {"stream": [{"result": {"content": "Hello, World!"}}], "isError": False}

result = interpreter._create_tool_result(response)

assert result["status"] == "success"
assert result["content"] == [{"text": "Hello, World!"}]


def test_create_tool_result_preserves_list_on_error(interpreter):
"""Test _create_tool_result preserves list content even on error responses."""
error_content = [{"type": "text", "text": "Error details"}]
response = {"stream": [{"result": {"content": error_content}}], "isError": True}

result = interpreter._create_tool_result(response)

assert result["status"] == "error"
assert result["content"] == error_content
Loading