{"record":{"id":"5ecfdd5d9c1252d3","repo":"plandex-ai/plandex","slug":"error-creating-chat-completion-stream-w","errorCode":null,"errorMessage":"error creating chat completion stream: %w","messagePattern":"error creating chat completion stream: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/model/client_stream.go","lineNumber":107,"sourceCode":"\tauthVars map[string]string,\n\tsettings *shared.PlanSettings,\n\torgUserConfig *shared.OrgUserConfig,\n\tctx context.Context,\n\treq types.ExtendedChatCompletionRequest,\n\tonStream OnStreamFn,\n\treqStarted time.Time,\n) (*types.ModelResponse, error) {\n\tstreamCtx, cancel := context.WithCancel(ctx)\n\n\tlog.Println(\"processChatCompletionStream - modelConfig\", spew.Sdump(map[string]interface{}{\n\t\t\"model\": modelConfig.ModelId,\n\t}))\n\n\tstream, err := createChatCompletionStreamExtended(modelConfig, client, authVars, settings, orgUserConfig, streamCtx, req)\n\n\tif err != nil {\n\t\tcancel()\n\t\treturn nil, fmt.Errorf(\"error creating chat completion stream: %w\", err)\n\t}\n\n\tdefer stream.Close()\n\tdefer cancel()\n\n\taccumulator := types.NewStreamCompletionAccumulator()\n\t// Create a timer that will trigger if no chunk is received within the specified duration\n\ttimer := time.NewTimer(ACTIVE_STREAM_CHUNK_TIMEOUT)\n\tdefer timer.Stop()\n\tstreamFinished := false\n\n\treceivedFirstChunk := false\n\n\t// Process stream until EOF or error\n\tfor {\n\t\tselect {\n\t\tcase <-streamCtx.Done():\n\t\t\tlog.Println(\"Stream canceled\")","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/model/client_stream.go#L89-L125","documentation":"This wraps any error returned when the Go client attempts to open a streaming chat completion connection with the model provider. The library throws it in processChatCompletionStream when createChatCompletionStreamExtended fails before any chunks are received. The underlying cause (auth, config, network, model name) is preserved via %w wrapping. The pending context cancellation is also run before returning.","triggerScenarios":"createChatCompletionStreamExtended returns an error: invalid API key, unreachable provider endpoint, malformed request payload, unsupported model, or failure to build the extended stream config (authVars/settings/orgUserConfig misconfiguration).","commonSituations":"Expired or missing provider API keys in authVars; wrong base URL or proxy endpoint in settings; model name not available to the configured provider; network/firewall blocking the provider; request body rejected by the provider (bad parameters like temperature/max_tokens).","solutions":["Check the wrapped cause (%w chain) in logs to identify the provider-level failure","Verify provider API keys and authVars are set and not expired","Confirm the model name and base URL in modelConfig/settings are valid for the provider","Test network reachability to the provider endpoint (curl the base URL)","Validate request parameters (temperature, max_tokens, messages format) against the provider API"],"exampleFix":"// before: opaque failure\nstream, err := createChatCompletionStreamExtended(modelConfig, client, authVars, settings, orgUserConfig, streamCtx, req)\nif err != nil { return nil, err }\n// after: log full cause chain upstream\nif err != nil {\n    log.Printf(\"stream create failed for model=%s: %v\", modelConfig.Name, err)\n    cancel()\n    return nil, fmt.Errorf(\"error creating chat completion stream: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if apiKey == \"\" || baseURL == \"\" { return errors.New(\"provider credentials/endpoint not configured\") }\nif _, ok := supportedModels[modelName]; !ok { return fmt.Errorf(\"unknown model %q\", modelName) }","typeGuard":"func isStreamCreateError(err error) bool { return err != nil && strings.Contains(err.Error(), \"error creating chat completion stream\") }","tryCatchPattern":"stream, err := client.StreamCompletion(req)\nif err != nil {\n    var apiErr *ProviderAPIError\n    if errors.As(err, &apiErr) && apiErr.StatusCode == 401 {\n        // refresh credentials and retry once\n    }\n    return fmt.Errorf(\"chat stream unavailable: %w\", err)\n}","preventionTips":["Validate API keys, base URL and model name before every request","Keep authVars/settings in validated config structs loaded at startup","Monitor the wrapped cause chain (%w) in your logging","Add a preflight health check against the provider endpoint"],"tags":["streaming","llm","network","initialization"],"backgroundTag":"stream-connection-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}