{"record":{"id":"a76a2414bd7694a3","repo":"siyuan-note/siyuan","slug":"response-stream-ended-before-a-terminal-event","errorCode":null,"errorMessage":"response stream ended before a terminal event","messagePattern":"response stream ended before a terminal event","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai_completion.go","lineNumber":461,"sourceCode":"\t\treturn stream.chat.Recv()\n\t}\n\tif stream.responses == nil {\n\t\treturn openai.ChatCompletionStreamResponse{}, io.EOF\n\t}\n\tif len(stream.pending) > 0 {\n\t\tresponse := stream.pending[0]\n\t\tstream.pending = stream.pending[1:]\n\t\treturn response, nil\n\t}\n\tif stream.responsesDone {\n\t\treturn openai.ChatCompletionStreamResponse{}, io.EOF\n\t}\n\n\tfor {\n\t\tevent, err := stream.responses.Recv()\n\t\tif err != nil {\n\t\t\tif errors.Is(err, io.EOF) {\n\t\t\t\treturn openai.ChatCompletionStreamResponse{}, errors.New(\"response stream ended before a terminal event\")\n\t\t\t}\n\t\t\treturn openai.ChatCompletionStreamResponse{}, err\n\t\t}\n\t\tresponse := openai.ChatCompletionStreamResponse{Object: \"chat.completion.chunk\"}\n\t\tswitch event.Type {\n\t\tcase openai.ResponseStreamEventOutputTextDelta:\n\t\t\tstream.responseContent.WriteString(event.Delta)\n\t\t\tresponse.Choices = []openai.ChatCompletionStreamChoice{{\n\t\t\t\tIndex: 0,\n\t\t\t\tDelta: openai.ChatCompletionStreamChoiceDelta{Content: event.Delta},\n\t\t\t}}\n\t\t\treturn response, nil\n\t\tcase openai.ResponseStreamEventRefusalDelta:\n\t\t\tstream.responseContent.WriteString(event.Delta)\n\t\t\tresponse.Choices = []openai.ChatCompletionStreamChoice{{\n\t\t\t\tIndex: 0,\n\t\t\t\tDelta: openai.ChatCompletionStreamChoiceDelta{Content: event.Delta, Refusal: event.Delta},\n\t\t\t}}","sourceCodeStart":443,"sourceCodeEnd":479,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai_completion.go#L443-L479","documentation":"Recv() translates the OpenAI Responses API stream into Chat Completions chunks. The Responses stream must end with a terminal event (response.completed or response.incomplete); if the underlying stream yields io.EOF first, the library replaces EOF with this error because the converted stream never emitted a finish_reason chunk. It guards callers against treating a truncated stream as a normal end.","triggerScenarios":"stream.responses.Recv() returns io.EOF before any ResponseStreamEventCompleted/Incomplete event was seen, e.g. the HTTP body ended after partial events (text deltas) without the terminal response event.","commonSituations":"Proxy/load-balancer cutting long streaming connections; provider closing the connection early; network drop mid-stream; non-OpenAI gateways that emulate Responses API imperfectly and omit the terminal event; server crash mid-generation.","solutions":["Check network path (proxies, ingress timeouts) so the full SSE body is delivered; increase idle/proxy read timeouts","Retry the request; the stream may have been dropped transiently","Verify the endpoint actually implements the Responses API fully (some OpenAI-compatible gateways skip response.completed)","Reduce max_output_tokens or prompt size so generation finishes before infrastructure timeouts"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"chunk, err := stream.Recv()\nif err != nil {\n    if strings.Contains(err.Error(), \"response stream ended before a terminal event\") {\n        // treat as truncated stream: discard partial answer or retry\n        return retryCompletion(ctx, req)\n    }\n    return err\n}","preventionTips":["Set proxy/ingress idle timeouts well above expected generation time","Add an application-level retry for streaming requests","Prefer providers/gateways with full Responses API support","Monitor finish_reason presence per completed stream"],"tags":["streaming","openai","network","truncated-response"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}