Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -1546,8 +1546,7 @@ def _parse_contents_from_anthropic(
case "text_editor_code_execution_tool_result_error":
text_editor_outputs.append(
Content.from_error(
message=content_block.content.error_code
and getattr(content_block.content, "error_message", ""),
message=content_block.content.error_message or content_block.content.error_code,
raw_representation=content_block.content,
)
)
Expand Down
30 changes: 30 additions & 0 deletions python/packages/anthropic/tests/test_anthropic_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3965,6 +3965,36 @@ def test_parse_text_editor_result_error(mock_anthropic_client: MagicMock) -> Non
assert result[0].type == "function_result"


def test_parse_text_editor_result_error_preserves_error_code(mock_anthropic_client: MagicMock) -> None:
"""A text-editor error with no error_message must still surface the error_code.

error_code is a required, always-truthy Literal and error_message is Optional
(usually None), so the old `error_code and error_message` always discarded the
code and produced an empty message.
"""
from types import SimpleNamespace

client = create_test_anthropic_client(mock_anthropic_client)

mock_content = SimpleNamespace(
type="text_editor_code_execution_tool_result_error",
error_code="file_not_found",
error_message=None,
)
mock_block = MagicMock()
mock_block.type = "text_editor_code_execution_tool_result"
mock_block.tool_use_id = "call_editor_err"
mock_block.content = mock_content

result = client._parse_contents_from_anthropic([mock_block])

assert len(result) == 1
assert result[0].type == "function_result"
error_out = result[0].items[0]

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.

PratikDhanave (@PratikDhanave) result[0].items is typed as Sequence[Content] | None, but the new test indexes it without first narrowing away None. The current head reports this exact line in pyrefly, ty, zuban, pyright, and mypy. Add a guard such as assert result[0].items is not None before indexing, or otherwise narrow the value before reading items[0].

assert error_out.type == "error"
assert error_out.message == "file_not_found"


def test_parse_text_editor_result_view(mock_anthropic_client: MagicMock) -> None:
"""Test parsing text editor view result."""
client = create_test_anthropic_client(mock_anthropic_client)
Expand Down
Loading