{"record":{"id":"93ecf1d99a20c7bf","repo":"siyuan-note/siyuan","slug":"response-stream-terminal-event-is-missing-response","errorCode":null,"errorMessage":"response stream terminal event is missing response","messagePattern":"response stream terminal event is missing response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai_completion.go","lineNumber":527,"sourceCode":"\t\t\tcall := stream.responseToolCalls[index]\n\t\t\tcall.Index = &index\n\t\t\tcall.Type = openai.ToolTypeFunction\n\t\t\tcall.Function.Arguments += event.Delta\n\t\t\tstream.responseToolCalls[index] = call\n\t\t\tresponse.Choices = []openai.ChatCompletionStreamChoice{{\n\t\t\t\tIndex: 0,\n\t\t\t\tDelta: openai.ChatCompletionStreamChoiceDelta{ToolCalls: []openai.ToolCall{{\n\t\t\t\t\tIndex: &index,\n\t\t\t\t\tType:  openai.ToolTypeFunction,\n\t\t\t\t\tFunction: openai.FunctionCall{\n\t\t\t\t\t\tArguments: event.Delta,\n\t\t\t\t\t},\n\t\t\t\t}}},\n\t\t\t}}\n\t\t\treturn response, nil\n\t\tcase openai.ResponseStreamEventCompleted, openai.ResponseStreamEventIncomplete:\n\t\t\tif event.Response == nil {\n\t\t\t\treturn openai.ChatCompletionStreamResponse{}, errors.New(\"response stream terminal event is missing response\")\n\t\t\t}\n\t\t\tif err = stream.queueResponseTerminal(*event.Response); err != nil {\n\t\t\t\treturn openai.ChatCompletionStreamResponse{}, err\n\t\t\t}\n\t\t\tstream.responsesDone = true\n\t\t\tresponse = stream.pending[0]\n\t\t\tstream.pending = stream.pending[1:]\n\t\t\treturn response, nil\n\t\tcase openai.ResponseStreamEventFailed:\n\t\t\treturn openai.ChatCompletionStreamResponse{}, responseEventError(event, \"response failed\")\n\t\tcase openai.ResponseStreamEventError:\n\t\t\treturn openai.ChatCompletionStreamResponse{}, responseEventError(event, \"response stream failed\")\n\t\t}\n\t\treturn response, nil\n\t}\n}\n\nfunc (stream *OpenAICompletionStream) queueResponseTerminal(response openai.CreateResponseResponse) error {","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai_completion.go#L509-L545","documentation":"When Recv() receives the terminal stream event (response.completed/response.incomplete), it reads event.Response to finalize the conversion. This error fires when the terminal event arrives with a nil Response payload, i.e. a malformed event from the provider, so the library cannot build finish-reason/usage chunks.","triggerScenarios":"An event of type ResponseStreamEventCompleted or ResponseStreamEventIncomplete whose event.Response field is nil — the provider sent the terminal event type without embedding the final CreateResponseResponse object.","commonSituations":"Buggy or version-skewed OpenAI-compatible gateway; SDK/json decoding dropping the payload due to schema mismatch; response object stripped by an intermediary that rewrites SSE events.","solutions":["Upgrade the OpenAI Go SDK / gateway so terminal events include the full response object","Inspect the raw SSE stream to confirm whether response.completed carries a response payload; if not, the server side is at fault","Retry the request; if reproducible with a specific provider, bypass or update that gateway","Fall back to the non-streaming CreateOpenAICompletion path if streams from this provider are unreliable"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"chunk, err := stream.Recv()\nif err != nil {\n    if strings.Contains(err.Error(), \"terminal event is missing response\") {\n        return errMalformedStream // mark provider as unreliable, fall back\n    }\n    return err\n}","preventionTips":["Keep the OpenAI SDK and any gateway on compatible versions","Test streaming against the provider with raw SSE capture before rollout","Fall back to non-streaming completion for flaky providers"],"tags":["streaming","openai","malformed-event","protocol"],"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"}