plandex-ai/plandex · error
error creating chat completion stream: %w
Error message
error creating chat completion stream: %w
What it means
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.
Source
Thrown at app/server/model/client_stream.go:107
authVars map[string]string,
settings *shared.PlanSettings,
orgUserConfig *shared.OrgUserConfig,
ctx context.Context,
req types.ExtendedChatCompletionRequest,
onStream OnStreamFn,
reqStarted time.Time,
) (*types.ModelResponse, error) {
streamCtx, cancel := context.WithCancel(ctx)
log.Println("processChatCompletionStream - modelConfig", spew.Sdump(map[string]interface{}{
"model": modelConfig.ModelId,
}))
stream, err := createChatCompletionStreamExtended(modelConfig, client, authVars, settings, orgUserConfig, streamCtx, req)
if err != nil {
cancel()
return nil, fmt.Errorf("error creating chat completion stream: %w", err)
}
defer stream.Close()
defer cancel()
accumulator := types.NewStreamCompletionAccumulator()
// Create a timer that will trigger if no chunk is received within the specified duration
timer := time.NewTimer(ACTIVE_STREAM_CHUNK_TIMEOUT)
defer timer.Stop()
streamFinished := false
receivedFirstChunk := false
// Process stream until EOF or error
for {
select {
case <-streamCtx.Done():
log.Println("Stream canceled")View on GitHub (pinned to e2d772072e)
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
Example fix
// before: opaque failure
stream, err := createChatCompletionStreamExtended(modelConfig, client, authVars, settings, orgUserConfig, streamCtx, req)
if err != nil { return nil, err }
// after: log full cause chain upstream
if err != nil {
log.Printf("stream create failed for model=%s: %v", modelConfig.Name, err)
cancel()
return nil, fmt.Errorf("error creating chat completion stream: %w", err)
} Defensive patterns
Strategy: try-catch
Validate before calling
if apiKey == "" || baseURL == "" { return errors.New("provider credentials/endpoint not configured") }
if _, ok := supportedModels[modelName]; !ok { return fmt.Errorf("unknown model %q", modelName) } Type guard
func isStreamCreateError(err error) bool { return err != nil && strings.Contains(err.Error(), "error creating chat completion stream") } Try / catch
stream, err := client.StreamCompletion(req)
if err != nil {
var apiErr *ProviderAPIError
if errors.As(err, &apiErr) && apiErr.StatusCode == 401 {
// refresh credentials and retry once
}
return fmt.Errorf("chat stream unavailable: %w", err)
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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).
Related errors
- error receiving stream chunk: %w
- Error starting reply stream: %v
- connection to plan stream timed out due to missing heartbeat
- stream timed out due to inactivity. The model is not respond
- model stream ended unexpectedly: %w
AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05).
Data as JSON: /api/errors/5ecfdd5d9c1252d3.
Report an issue: GitHub.