{"record":{"id":"85c7298f7164bd36","repo":"siyuan-note/siyuan","slug":"model-returned-nil-stream","errorCode":null,"errorMessage":"model returned nil stream","messagePattern":"model returned nil stream","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":2561,"sourceCode":"\t\t\tsendEvent(ch, AgentEvent{Type: \"retry\", RetryAttempt: attempt, RetryMax: maxRetries})\n\t\t}\n\n\t\tstreamCtx, streamCancel := context.WithCancel(ctx)\n\t\trequestTimer, requestTimerDone := startCancelTimer(requestTimeout, streamCancel)\n\t\tstream, err := util.CreateOpenAICompletionStream(streamCtx, client, protocol, req, responseInput)\n\t\trequestTimedOut := stopCancelTimer(requestTimer, requestTimerDone)\n\t\tif ctx.Err() != nil {\n\t\t\tif stream != nil {\n\t\t\t\tstream.Close()\n\t\t\t}\n\t\t\tstreamCancel()\n\t\t\treturn nil, openai.ChatCompletionStreamResponse{}, nil, ctx.Err()\n\t\t}\n\t\tif requestTimedOut {\n\t\t\terr = errModelRequestTimeout\n\t\t}\n\t\tif err == nil && stream == nil {\n\t\t\terr = errors.New(\"model returned nil stream\")\n\t\t}\n\t\tif err == nil {\n\t\t\tfor {\n\t\t\t\tfirstResp, firstErr := recvStreamWithIdleTimeout(stream, streamIdleTimeout, streamCancel)\n\t\t\t\tif firstErr != nil || !util.IsOpenAIResponsesProtocol(protocol) || len(firstResp.Choices) > 0 ||\n\t\t\t\t\tfirstResp.Usage != nil {\n\t\t\t\t\tif firstErr == nil || errors.Is(firstErr, io.EOF) {\n\t\t\t\t\t\treturn stream, firstResp, streamCancel, nil\n\t\t\t\t\t}\n\t\t\t\t\terr = firstErr\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif stream != nil {\n\t\t\tstream.Close()\n\t\t}\n\t\tstreamCancel()","sourceCodeStart":2543,"sourceCodeEnd":2579,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/agent.go#L2543-L2579","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: adapter swallows the error and returns nil\nif resp.Err != nil {\n    return nil, openai.ChatCompletionStreamResponse{}, nil, nil // misuse\n}\n// after\nif resp.Err != nil {\n    return nil, openai.ChatCompletionStreamResponse{}, nil, fmt.Errorf(\"stream create failed: %w\", resp.Err)\n}","handlingStrategy":"try-catch","validationCode":"// wrapper safety check around any custom stream factory\nstream, err := createStream(ctx, client, req)\nif err == nil && stream == nil {\n    err = errors.New(\"model returned nil stream\")\n}","typeGuard":"func isUsableStream(s *util.OpenAICompletionStream) bool { return s != nil }","tryCatchPattern":"stream, _, cancel, err := createProtocolStreamWithRetry(...)\nif err != nil {\n    if err.Error() == \"model returned nil stream\" {\n        // report a client/SDK bug; retry once and otherwise disable the adapter\n    }\n}","preventionTips":["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."],"tags":["agent","streaming","null","defensive"],"backgroundTag":"unexpected-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"}