siyuan-note/siyuan · error
model returned nil stream
Error message
model returned nil stream
What it means
A defensive error raised in createProtocolStreamWithRetry when the provider's SDK returns err == nil but a nil stream — an impossible outcome per the API contract. Rather than nil-pointer-panicking on stream usage, the agent converts it into the explicit error "model returned nil stream" so it can be classified and retried like other stream-creation failures. (The DECLARED AS/USED AT references to app/electron/accessibility.js are unrelated indexing noise; the authoritative throw site is agent.go:2561.)
Solutions
- Treat it as a provider/client bug: verify the openai client and any protocol adapter return a real stream or a non-nil error.
- Check whether a custom wrapper around client.CreateChatCompletionStream swallows errors and returns nil — fix it to propagate the error.
- Upgrade or pin the OpenAI Go SDK to a version without the nil-stream regression.
- Retry the request; the agent classifies this error so an automatic retry may succeed.
Example fix
// before: adapter swallows the error and returns nil
if resp.Err != nil {
return nil, openai.ChatCompletionStreamResponse{}, nil, nil // misuse
}
// after
if resp.Err != nil {
return nil, openai.ChatCompletionStreamResponse{}, nil, fmt.Errorf("stream create failed: %w", resp.Err)
} Defensive patterns
Strategy: try-catch
Validate before calling
// wrapper safety check around any custom stream factory
stream, err := createStream(ctx, client, req)
if err == nil && stream == nil {
err = errors.New("model returned nil stream")
} Type guard
func isUsableStream(s *util.OpenAICompletionStream) bool { return s != nil } Try / catch
stream, _, cancel, err := createProtocolStreamWithRetry(...)
if err != nil {
if err.Error() == "model returned nil stream" {
// report a client/SDK bug; retry once and otherwise disable the adapter
}
} Prevention
- Never return (nil, nil) from stream-creating wrappers; always propagate an error.
- Pin/upgrade the OpenAI Go SDK to a version without nil-stream regressions.
- Unit-test protocol adapters for the nil-stream path.
- Keep the nil-stream guard so failures surface as errors instead of panics.
When it happens
Trigger: In agent.go (~line 2561): after the request timer fires without timeout (requestTimedOut false) and the client's CreateChatCompletionStream (or protocol equivalent) returns a nil stream with a nil error — a bug or misuse in the client/SDK layer, e.g. a custom protocol adapter returning (nil, nil) on a degraded path.
Common situations: A patched or wrapper-wrapped OpenAI client returns nil on a non-error path; protocol adapters (OpenAI Responses vs Chat Completions) mishandling empty provider responses; SDK version regressions in stream creation.
Related errors
- agent compaction summary is empty
- agent context cannot be compacted enough
- agent context cannot be compacted enough: build runtime…
- agent context cannot be compacted enough: build summary…
- agent context cannot be compacted enough: compacted context…
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/85c7298f7164bd36.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/agent/agent.go:2561
sendEvent(ch, AgentEvent{Type: "retry", RetryAttempt: attempt, RetryMax: maxRetries})
}
streamCtx, streamCancel := context.WithCancel(ctx)
requestTimer, requestTimerDone := startCancelTimer(requestTimeout, streamCancel)
stream, err := util.CreateOpenAICompletionStream(streamCtx, client, protocol, req, responseInput)
requestTimedOut := stopCancelTimer(requestTimer, requestTimerDone)
if ctx.Err() != nil {
if stream != nil {
stream.Close()
}
streamCancel()
return nil, openai.ChatCompletionStreamResponse{}, nil, ctx.Err()
}
if requestTimedOut {
err = errModelRequestTimeout
}
if err == nil && stream == nil {
err = errors.New("model returned nil stream")
}
if err == nil {
for {
firstResp, firstErr := recvStreamWithIdleTimeout(stream, streamIdleTimeout, streamCancel)
if firstErr != nil || !util.IsOpenAIResponsesProtocol(protocol) || len(firstResp.Choices) > 0 ||
firstResp.Usage != nil {
if firstErr == nil || errors.Is(firstErr, io.EOF) {
return stream, firstResp, streamCancel, nil
}
err = firstErr
break
}
}
}
if stream != nil {
stream.Close()
}
streamCancel()View on GitHub (pinned to 9f775e8a12)