chenhg5/cc-connect · error

openai tts: read audio: %w

Error message

openai tts: read audio: %w

What it means

This error is returned when io.ReadAll fails while streaming the successful (200) TTS response body into memory as MP3 bytes. It indicates the connection was interrupted or reset mid-download of the audio payload, not an API rejection.

Source

Thrown at core/tts.go:268

		return nil, "", fmt.Errorf("openai tts: create request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+o.APIKey)
	req.Header.Set("Content-Type", "application/json")

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

	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(resp.Body)
		return nil, "", fmt.Errorf("openai tts API %d: %s", resp.StatusCode, body)
	}

	mp3Data, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, "", fmt.Errorf("openai tts: read audio: %w", err)
	}
	return mp3Data, "mp3", nil
}

// ──────────────────────────────────────────────────────────────
// MiniMaxTTS — MiniMax T2A v2 TTS implementation
// ──────────────────────────────────────────────────────────────

// MiniMaxTTS implements TextToSpeech using the MiniMax T2A v2 API.
type MiniMaxTTS struct {
	APIKey  string
	BaseURL string
	Model   string
	Client  *http.Client
}

// NewMiniMaxTTS creates a new MiniMaxTTS instance.
func NewMiniMaxTTS(apiKey, baseURL, model string, client *http.Client) *MiniMaxTTS {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Retry the Synthesize call — transient truncation usually succeeds on retry
  2. Check for proxies/load balancers with short response timeouts between client and endpoint
  3. Add retry-with-backoff around Synthesize in the caller
  4. Ensure the http.Client has sane timeouts that don't cut off slow audio transfers

Example fix

// before
mp3, format, err := tts.Synthesize(ctx, text)
if err != nil { return err } // one shot, no retry
// after
mp3, format, err := synthesizeWithRetry(ctx, tts, text, 3) // backoff retries
if err != nil { return fmt.Errorf("tts synthesize: %w", err) }
Defensive patterns

Strategy: retry

Validate before calling

null

Type guard

null

Try / catch

var mp3 []byte
var err error
for attempt := 0; attempt < 3; attempt++ {
    mp3, _, err = tts.Synthesize(ctx, text)
    if err == nil || !strings.Contains(err.Error(), "read audio") { break }
    time.Sleep(time.Duration(attempt+1) * time.Second)
}

Prevention

When it happens

Trigger: Server or proxy closes the connection partway through the audio body, TLS truncation, network drop during transfer, or an intermediary killing long-lived responses.

Common situations: Flaky mobile/VPN connections, aggressive load balancers with short idle/response timeouts, large speech outputs over unstable links.

Related errors


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