siyuan-note/siyuan · error
response stream terminal event is missing response
Error message
response stream terminal event is missing response
What it means
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.
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
Defensive patterns
Strategy: try-catch
Try / catch
chunk, err := stream.Recv()
if err != nil {
if strings.Contains(err.Error(), "terminal event is missing response") {
return errMalformedStream // mark provider as unreliable, fall back
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.
Related errors
- response ended with an incomplete function call
- response failed
- response stream ended before a terminal event
- response stream failed
- streamed function arguments do not match the terminal…
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/93ecf1d99a20c7bf.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/openai_completion.go:527
call := stream.responseToolCalls[index]
call.Index = &index
call.Type = openai.ToolTypeFunction
call.Function.Arguments += event.Delta
stream.responseToolCalls[index] = call
response.Choices = []openai.ChatCompletionStreamChoice{{
Index: 0,
Delta: openai.ChatCompletionStreamChoiceDelta{ToolCalls: []openai.ToolCall{{
Index: &index,
Type: openai.ToolTypeFunction,
Function: openai.FunctionCall{
Arguments: event.Delta,
},
}}},
}}
return response, nil
case openai.ResponseStreamEventCompleted, openai.ResponseStreamEventIncomplete:
if event.Response == nil {
return openai.ChatCompletionStreamResponse{}, errors.New("response stream terminal event is missing response")
}
if err = stream.queueResponseTerminal(*event.Response); err != nil {
return openai.ChatCompletionStreamResponse{}, err
}
stream.responsesDone = true
response = stream.pending[0]
stream.pending = stream.pending[1:]
return response, nil
case openai.ResponseStreamEventFailed:
return openai.ChatCompletionStreamResponse{}, responseEventError(event, "response failed")
case openai.ResponseStreamEventError:
return openai.ChatCompletionStreamResponse{}, responseEventError(event, "response stream failed")
}
return response, nil
}
}
func (stream *OpenAICompletionStream) queueResponseTerminal(response openai.CreateResponseResponse) error {View on GitHub (pinned to 9f775e8a12)