chenhg5/cc-connect · error

read response: %w

Error message

read response: %w

What it means

io.ReadAll failed while draining the Whisper API response body. This happens if the connection is reset mid-response, the server closes the connection prematurely, or the context is cancelled/timed out during body read.

Source

Thrown at core/speech.go:92

	writer.Close()

	url := w.BaseURL + "/audio/transcriptions"
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &buf)
	if err != nil {
		return "", fmt.Errorf("create request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+w.APIKey)
	req.Header.Set("Content-Type", writer.FormDataContentType())

	resp, err := w.Client.Do(req)
	if err != nil {
		return "", fmt.Errorf("whisper request: %w", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("read response: %w", err)
	}

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("whisper API %d: %s", resp.StatusCode, string(body))
	}

	// response_format=text returns plain text; try to handle JSON fallback
	text := strings.TrimSpace(string(body))
	if strings.HasPrefix(text, "{") {
		var jr struct {
			Text string `json:"text"`
		}
		if json.Unmarshal(body, &jr) == nil {
			text = jr.Text
		}
	}
	return text, nil
}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Retry the transcription — transient connection resets usually succeed on retry
  2. Check intermediary (proxy/LB) timeouts and raise them for long ASR responses
  3. Inspect the wrapped error for 'connection reset by peer' vs 'context canceled' to distinguish network vs cancellation
Defensive patterns

Strategy: retry

Try / catch

text, err := stt.Transcribe(ctx, audio, format, lang)
if err != nil && strings.Contains(err.Error(), "read response") {
    if !errors.Is(err, context.Canceled) {
        text, err = stt.Transcribe(ctx, audio, format, lang) // one retry
    }
}

Prevention

When it happens

Trigger: Transcribe receives a response but the TCP connection breaks before the full body is read — server restart, load-balancer timeout, context cancellation, or network interruption during read.

Common situations: Unstable network or VPN drops mid-upload/response; ASR proxy with aggressive idle timeouts; large response truncated by an intermediary.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/5bfa3e027e4c68b6. Report an issue: GitHub.