AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

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)

Open shawn8888 opened this issue 2 weeks ago • 1 comments

Environment

  • Version: v0.3.8 (commit d94bf91c, 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)

  1. Commit 58b2e978 removed AbnormalFinishError:
    • In internal/aistudio/generate.go, after retries for abnormalGenerationFinish (malformed_response, malformed_function_call, unexpected_tool_call) are exhausted, only contentPolicyFinish emits EventError.
    • malformed_response falls through to emit EventFinish{FinishReason: "malformed_response"}.
    • openAIFinishReason (internal/api/openai.go:1330) falls through to "stop".
  2. Streaming commits HTTP 200 after 10s before upstream events arrive:
    • In internal/api/generate.go:19, streamHeartbeatInterval = 10 * time.Second. awaitStreamStart stops waiting after 10s, so streamChatCompletion (internal/api/openai.go:1159-1176) writes HTTP 200 headers and {"role":"assistant","content":""}.
    • In both incidents above, first_event_ms was 21.3s and 10.6s, so HTTP 200 was already committed before any error could be returned as HTTP 502.

Expected Behavior

  1. When abnormalGenerationFinish (malformed_response, malformed_function_call, unexpected_tool_call) exhausts retries with 0 output/reasoning/tool-call tokens, emit EventError (HTTP 502 if headers are not yet sent, or an in-stream OpenAI error event without finish_reason: "stop" if streaming has already started) rather than reporting a clean "stop".
  2. Consider whether awaitStreamStart / SSE keepalive comments can avoid committing a role: assistant 200 chunk prematurely, or ensure streamChatCompletion emits an SSE error payload when an upstream attempt fails with 0 output after headers were flushed.

shawn8888 avatar Nov 14 '26 22:11 shawn8888

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.

Mag1cFall avatar Nov 15 '26 00:11 Mag1cFall