Anthropic clients never see a failed turn: anthropic_stop_reason maps finish=="error" to end_turn

Aperta
#1,164 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c
Ambito
api, backend

Direzione di ricerca

Start in ds4_server.c at anthropic_stop_reason, then trace anthropic_final_response and anthropic_sse_finish_live, including sse_error_event and send_prefill_failure_response. Review the existing OpenAI-compatible and Responses handling, then run ds4_test --server. Done requires an agreed failure-surfacing behavior and coverage for both Anthropic response paths.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

anthropic_stop_reason maps every finish state except tool_calls and length to end_turn — including "error". A turn that failed server-side is therefore indistinguishable from a normal completion on the Anthropic API, and the error text is not surfaced anywhere in the response body or stream.

Current upstream code (ds4_server.c @ 0aaea5a):

static const char *anthropic_stop_reason(const char *finish) {   // :9038
    if (finish && !strcmp(finish, "tool_calls")) return "tool_use";
    if (finish && !strcmp(finish, "length")) return "max_tokens";
    return "end_turn";
}

Both Anthropic surfaces route through it: the non-streaming final message (anthropic_final_response, :9113, stop_reason at :9123) and the streaming terminator (anthropic_sse_finish_live → message_delta's stop_reason at :9828).

The other two APIs are honest about the same event:

  • OpenAI-compatible: finish_reason: "error" on the 200 body (the value is passed through verbatim).
  • Responses: finish == "error" → status: "failed" with error.code = "server_error" (:8636, :8676).

And the SSE error event the server already has (sse_error_event, :7005, which emits a proper type: "error" / api_error) is called from exactly one place — send_prefill_failure_response (:12759). A failure raised after decoding (engine fault during generation, failed session append, unterminated tool call, stream-write failure) never reaches it on the Anthropic path.

Observed in production

While investigating the case that became #1163: the model opened a tool envelope and never closed it, the generator set finish = "error", and the Anthropic client received the raw truncated markup as ordinary assistant text with stop_reason: "end_turn". Nothing in the response indicated anything had gone wrong; the only trace is in the server log. (#1163 removes that specific trigger by deferring to the parse-failure recovery, but the collapse itself stays for every other error class.)

Why this is not a one-line mapping fix

Anthropic's stop_reason vocabulary (end_turn | max_tokens | stop_sequence | tool_use | pause_turn) has no error member, so the choices are shaped by what clients tolerate:

  1. Out-of-band surfacing (probably the smallest honest change): keep stop_reason: "end_turn", but deliver the failure alongside it — e.g. an error field on the message object, or a message_delta-adjacent error event reusing the existing sse_error_event machinery. Clients that ignore unknown fields keep working; clients that look can tell.
  2. HTTP-level errors for genuine server faults (5xx, or a terminal error event mid-stream), while model-output shapes (unclosed envelope, parse failures) stay completions — which is the same distinction #1163 draws one layer down.
  3. Document the collapse so an operator debugging a silent Anthropic failure knows to check the server log rather than the response.

Option 2 changes failure semantics clients can actually branch on, but risks breaking agent loops that today silently receive end_turn and treat the truncated markup as a real answer (a working failure mode, in a sense). I don't have a strong claim on which the project prefers — hence the issue rather than a PR; happy to implement whichever shape you pick, with the Anthropic unit cases pinned against the current anthropic_final_response/anthropic_sse_finish_live pair.

Reproduction notes (per CONTRIBUTING.md)

Machine/backend/model: Apple M5 Max 128 GB, Metal, Qwen3.8 Flash Next Q4_K + vision, production server (--thinking enabled, Anthropic /v1/messages via REQ_ANTHROPIC). The error-finish trigger (unclosed envelope) is model-output-shaped and rare by construction (1 of 149 tool turns in a multi-day soak), so a deterministic local repro is easier through the parse layer than the model: generate/hold a result with finish == "error" and observe anthropic_final_response emits stop_reason: "end_turn" with the content verbatim.

ds4_test --server is green on current main at these line references; this issue reports behavior, it is not a crash.

Lingua principale
C
Stelle
24.4k
Fork
2.4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di antirez/ds4

Tutte le issue di antirez/ds4

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.