Skip to content

Python: Fix: surface error_code for Anthropic text-editor tool errors (empty message) - #9334

Closed
PratikDhanave (PratikDhanave) wants to merge 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix/anthropic-text-editor-error-code
Closed

PratikDhanave (PratikDhanave) wants to merge 1 commit into
microsoft:mainfrom
PratikDhanaveFork:fix/anthropic-text-editor-error-code

Conversation

@PratikDhanave

Copy link
Copy Markdown

Motivation & Context

In the Anthropic chat client, a text_editor_code_execution_tool_result_error produced an empty error message and silently dropped the error_code. This hides the actual failure (e.g. file_not_found, execution_time_exceeded) from anything consuming the tool result.

Description & Review Guide

_chat_client.py built the error content as:

message=content_block.content.error_code
and getattr(content_block.content, "error_message", "")

error_code is a required, always-truthy Literal, so error_code and error_message always evaluates to the right operand error_message — which is Optional[str] and is typically None for these errors. Result: the ErrorContent has no message and the error_code is discarded. The two sibling error branches in the same function use error_code correctly (code_execution_tool_result_error → message=...error_code; bash_code_execution_tool_result_error → stderr=...error_code).

  • Major change: message=content_block.content.error_message or content_block.content.error_code — prefer the human-readable message when present, fall back to the always-present code; never empty.
  • Impact: text-editor tool errors now carry a meaningful message. No API/signature change.
  • Tests: added a regression test (error_code set, error_message=None) asserting the parsed error item's message equals the code. Fails before the fix (None), passes after. Full anthropic text-editor suite green.

Related Issue

N/A - trivial change: a localized, two-token correctness fix in one parser branch, matching the behavior of its sibling branches, covered by a new unit test.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: This change was developed with Claude Code (an AI agent) — it located the bug, wrote the one-line fix and the regression test, and ran verification (pytest). PratikDhanave (@PratikDhanave) to review and tick the AI-assisted attestation above before merge.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible

…message)

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 13, 2026
@github-actions github-actions Bot changed the title Fix: surface error_code for Anthropic text-editor tool errors (empty message) Python: Fix: surface error_code for Anthropic text-editor tool errors (empty message) Oct 13, 2026
@eavanvalkenburg

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

MAF Automated Review — Iteration 1

Result: No findings
Scope: full PR (1 commit(s)): 830cf33dee9b
Model: gpt-5.6-sol

Overview

The change makes Anthropic text-editor execution errors prefer a human-readable message and reliably fall back to the provider's required error code. The supported SDK contract guarantees both attributes, and the regression test covers the previously empty-message path while the shared parser keeps streaming and non-streaming behavior aligned. No Critical, High, or Medium issue remains after reconciliation.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.


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].

@jpalvarezl

Copy link
Copy Markdown
Member

Thank you for the contribution. This pull request has had outstanding feedback about the required items is not None narrowing and its related typing failure for more than three days without recent progress, so we are closing it for now to keep the review queue current. Please reopen it when you are ready to continue and address the outstanding item, and we will be happy to take another look.

This branch was previously deployed

1 inactive deployment
github-app-auth — 830cf33d Deployed Oct 13, 2026 by PratikDhanave via team_check #6696
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants