chenhg5/cc-connect · error

openai tts API %d: %s

Error message

openai tts API %d: %s

What it means

This error is returned when the TTS endpoint responds with a non-200 HTTP status. The library reads the response body and embeds both the status code and body text so the upstream API's own error message (auth failure, invalid model, quota, bad parameter) is visible to the caller.

Source

Thrown at core/tts.go:263

	}

	url := strings.TrimRight(o.BaseURL, "/") + "/audio/speech"
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData))
	if err != nil {
		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

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Read the status code and body in the wrapped message to identify the upstream cause
  2. Verify the API key is valid and has TTS/quota access for the endpoint
  3. Confirm the BaseURL points to a provider that implements /audio/speech (many proxies only proxy /chat/completions)
  4. Back off and retry on 429/5xx; fix credentials/parameters on 401/403/400

Example fix

// before
o.BaseURL = "https://my-proxy.example.com" // no /v1 audio support
// after
o.BaseURL = "https://api.openai.com/v1" // provider that serves /audio/speech
Defensive patterns

Strategy: try-catch

Validate before calling

if cfg.TTSAPIKey == "" { return errors.New("tts api key not configured") }

Type guard

null

Try / catch

mp3, format, err := tts.Synthesize(ctx, text)
if err != nil {
    var apiErr string
    if strings.Contains(err.Error(), "tts API ") {
        apiErr = err.Error() // includes status code and upstream body
        // 401/403 -> fix key; 429/5xx -> retry with backoff
    }
    return fmt.Errorf("tts failed: %s", apiErr)
}

Prevention

When it happens

Trigger: Any non-200 response from POST {BaseURL}/audio/speech: 401 invalid API key, 403 forbidden, 404 wrong path/model, 429 rate limit, 5xx upstream outage.

Common situations: Expired or wrong OPENAI_API_KEY for the configured BaseURL, a third-party OpenAI-compatible proxy that doesn't implement /audio/speech, unsupported model/tts-1 voice parameters, exhausted quota.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


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