siyuan-note/siyuan · error

model returned nil stream

Error message

model returned nil stream

What it means

A defensive invariant check in createStreamWithRetry: if the transport call returned neither an error nor a stream, the agent fabricates this error rather than proceeding with a nil stream. It signals an unexpected/undefined state in the model client response handling.

Source

Thrown at kernel/agent/agent.go:2491

			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 8641553a1f)

Solutions

  1. Retry the request; a nil stream is often transient client behavior
  2. Check the openai client library version for known nil-stream bugs and upgrade
  3. Verify base URL/protocol configuration points at a real streaming endpoint
  4. Enable transport-level logging to see the raw HTTP response
  5. If reproducible, report the (nil, nil) return to the client library maintainers
Defensive patterns

Strategy: retry

Try / catch

if err != nil && err.Error() == "model returned nil stream" {
    // retry once; if persistent, log client/version details for diagnosis
}

Prevention

When it happens

Trigger: After the request completes without requestTimedOut and with err == nil, the returned stream is nil (client library returned nothing), so `if err == nil && stream == nil` raises errors.New("model returned nil stream").

Common situations: OpenAI client library returning (nil, nil) on unusual transport states; misconfigured protocol/base-URL causing the client to skip stream creation; client library version regressions.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/85c7298f7164bd36. Report an issue: GitHub.