v0.3.8: exhausted malformed_response retries emit HTTP 200 with finish_reason="stop" and 0 output tokens instead of an error (regression after 58b2e978)
Environment
- Version:
v0.3.8(commitd94bf91c,UPSTREAM_CHANNELS=playground,WAA_BACKEND=spidermonkey) - Endpoint:
POST /v1/chat/completions(streaming) - Model:
gemini-4-hydrogen(~63k input tokens + 1 image attachment) - Downstream routers/plugins affected: yetone/magpie#6712, BUKOWSKIREAL/magpie-gemini-web#61
Problem
When upstream Google AI Studio ends all 4 attempts (initial + 3 regenerations) with malformed_response and 0 output tokens, AIStudio2API logs state=blocked / finish_reason=malformed_response internally, yet returns HTTP 200 + finish_reason: "stop" + 0 reply/reasoning/tool-call tokens to OpenAI clients:
# backend.log / requests.db (both requests ran 4 upstream GenerateContent attempts)
level=WARN msg="Upstream stopped generation" id=chatcmpl_1794688838638417321_17 state=blocked status=200 finish_reason=malformed_response upstream_bytes=5516 first_event_ms=21313.68 duration_ms=57960.43 reply_tokens=0 reasoning_tokens=0 tool_calls=0
level=WARN msg="Upstream stopped generation" id=chatcmpl_1794688972985290176_21 state=blocked status=200 finish_reason=malformed_response upstream_bytes=6684 first_event_ms=10571 duration_ms=43577 reply_tokens=0 reasoning_tokens=0 tool_calls=0
Because the response looks like a normal completed turn (HTTP 200, finish_reason: "stop", [DONE]), downstream AI routers and coding agents treat the turn as finished instead of retrying, switching accounts, or falling back to another provider—resulting in a silent empty reply to the user.
Root Cause (verified in source)
- Commit
58b2e978removedAbnormalFinishError:- In
internal/aistudio/generate.go, after retries forabnormalGenerationFinish(malformed_response,malformed_function_call,unexpected_tool_call) are exhausted, onlycontentPolicyFinishemitsEventError. malformed_responsefalls through to emitEventFinish{FinishReason: "malformed_response"}.openAIFinishReason(internal/api/openai.go:1330) falls through to"stop".
- In
- Streaming commits HTTP
200after10sbefore upstream events arrive:- In
internal/api/generate.go:19,streamHeartbeatInterval = 10 * time.Second.awaitStreamStartstops waiting after 10s, sostreamChatCompletion(internal/api/openai.go:1159-1176) writes HTTP200headers and{"role":"assistant","content":""}. - In both incidents above,
first_event_mswas21.3sand10.6s, so HTTP200was already committed before any error could be returned as HTTP502.
- In
Expected Behavior
- When
abnormalGenerationFinish(malformed_response,malformed_function_call,unexpected_tool_call) exhausts retries with 0 output/reasoning/tool-call tokens, emitEventError(HTTP502if headers are not yet sent, or an in-stream OpenAIerrorevent withoutfinish_reason: "stop"if streaming has already started) rather than reporting a clean"stop". - Consider whether
awaitStreamStart/ SSE keepalive comments can avoid committing arole: assistant200chunk prematurely, or ensurestreamChatCompletionemits an SSE error payload when an upstream attempt fails with 0 output after headers were flushed.
Fixed in v0.3.9: when regeneration is exhausted and the client received nothing, Chat, Responses and Anthropic now return a 502 (or an in-stream error once streaming started) instead of an empty stop, and the Gemini endpoint keeps the official MALFORMED_RESPONSE.