From 830cf33dee9b181713c47b09604736b40b4246a8 Mon Sep 17 00:00:00 2001 From: Pratik Dhanave Date: Tue, 13 Oct 2026 18:08:52 +0530 Subject: [PATCH] Fix: surface error_code for Anthropic text-editor tool errors (empty message) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the text_editor_code_execution_tool_result_error branch, the message was `content_block.content.error_code and getattr(..., "error_message", "")`. error_code is a required, always-truthy Literal, so `error_code and error_message` always evaluates to error_message — which is Optional[str] and typically None for these errors — so the ErrorContent ended up with no message at all, and error_code was discarded. The sibling branches (code_execution_tool_result_error, bash_code_execution_tool_result_error) use error_code correctly. Use `error_message or error_code` so the human-readable message is preferred when present and the always-present code is the fallback; the message is never empty. Add a regression test (error_code set, error_message None) asserting the error item carries the code. --- .../agent_framework_anthropic/_chat_client.py | 3 +- .../anthropic/tests/test_anthropic_client.py | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py index 146b9f6a998..551059135f6 100644 --- a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py +++ b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py @@ -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, ) ) diff --git a/python/packages/anthropic/tests/test_anthropic_client.py b/python/packages/anthropic/tests/test_anthropic_client.py index c91a37765ed..6adf976572a 100644 --- a/python/packages/anthropic/tests/test_anthropic_client.py +++ b/python/packages/anthropic/tests/test_anthropic_client.py @@ -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] + 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)