fix: surface result text when error result has empty errors[]#1056
Open
archievi wants to merge 1 commit into
Open
fix: surface result text when error result has empty errors[]#1056archievi wants to merge 1 commit into
archievi wants to merge 1 commit into
Conversation
When the CLI emits a result message with is_error=true but an empty errors[] list (e.g. a model_not_found 404 where the protocol-level subtype is "success"), the SDK constructed the exception message from the subtype, producing the contradictory "Claude Code returned an error result: success". The human-readable error in the result field was discarded. Prefer the joined errors[] list, then fall back to the result field, and only then to the subtype. Add a regression test covering the empty-errors / subtype=success case. Fixes anthropics#1031
itxaiohanglover
approved these changes
Jun 22, 2026
itxaiohanglover
left a comment
There was a problem hiding this comment.
This is the cleanest fix among the PRs addressing #1031. The fallback chain errors → result → subtype is exactly right — it surfaces the human-readable error text from the result field before falling back to the protocol-level subtype. The regression test covers the exact scenario from the issue (model_not_found 404 with empty errors[] and subtype="success").
One minor note: the test could also verify that the result text is not truncated or modified (e.g. check str(exception) contains the full model name), but the current assertion is sufficient for the regression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the CLI emits a
resultmessage withis_error=truebut an emptyerrors[]list, the SDK builds a contradictory exception message:This happens for some API-level failures (e.g. a
model_not_found404), where the trailing JSON looks like:{ "type": "result", "subtype": "success", "is_error": true, "errors": [], "result": "There's an issue with the selected model (nonexistent-model). It may not exist or you may not have access to it.", "api_error_status": 404 }In
query.py, the error text was built as:"; ".join([])is""(falsy), so it falls through tosubtype. Heresubtypeis"success"(a clean protocol-level exit), producing the misleading"...returned an error result: success". The human-readable error in theresultfield is never consulted.Fix
Prefer the joined
errors[]list, then fall back to theresultfield, and only then to thesubtype:Minimal, idiomatic change that preserves all existing fallback behavior (the
errors[]-join path and thesubtypefallback for older/minimal payloads are unchanged).Verification
Dev environment set up with
pip install -e ".[dev]". Commands match CI (lint.yml/test.yml):ruff check src/ tests/— All checks passedruff format --check src/ tests/— 55 files already formattedmypy src/— Success: no issues found in 24 source filespython -m pytest tests/test_query.py— 38 passedAdded a regression test (
TestProcessExitAfterErrorResult::test_process_error_empty_errors_uses_result_field_not_subtype) covering the empty-errors[]/subtype="success"case.Confirmed the test fails without the fix: reverting only the source change and running the new test produces:
Restoring the fix, the test passes.
Fixes #1031