chenhg5/cc-connect · error

minimax tts: create request: %w

Error message

minimax tts: create request: %w

What it means

This error wraps a failure from http.NewRequestWithContext when building the POST to MiniMax's /v1/t2a_v2 TTS endpoint inside Synthesize. The constructor only fails if the context is canceled/deadlined, the HTTP method is invalid, or the URL fails to parse. It indicates the request never left the process — the problem is local inputs, not the MiniMax API.

Source

Thrown at core/tts.go:336

		"stream": true,
		"voice_setting": map[string]any{
			"voice_id": voice,
			"speed":    speed,
		},
		"audio_setting": map[string]any{
			"format":      "mp3",
			"sample_rate": 32000,
		},
	}
	jsonData, err := json.Marshal(reqBody)
	if err != nil {
		return nil, "", fmt.Errorf("minimax tts: marshal request: %w", err)
	}

	url := strings.TrimRight(m.BaseURL, "/") + "/v1/t2a_v2"
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData))
	if err != nil {
		return nil, "", fmt.Errorf("minimax tts: create request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+m.APIKey)
	req.Header.Set("Content-Type", "application/json")

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

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

	// Parse SSE stream: each line is "data: {...}" with hex-encoded audio chunks.
	var audioBuf bytes.Buffer
	scanner := bufio.NewScanner(resp.Body)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check whether ctx was already canceled before calling Synthesize: inspect ctx.Err() at the call site.
  2. Validate the configured base_url in config.toml: it must be an absolute URL like https://api.minimax.chat with no spaces.
  3. If the caller applies a deadline, increase the timeout so it has not expired by the time Synthesize runs.
  4. Retry with a fresh context if the cancellation was transient (e.g. per-request deadline).

Example fix

// before
ctx, cancel := context.WithTimeout(parentCtx, 2*time.Second)
// after
ctx, cancel := context.WithTimeout(parentCtx, 30*time.Second) // TTS SSE streams need longer deadlines
Defensive patterns

Strategy: try-catch

Validate before calling

if err := ctx.Err(); err != nil { return fmt.Errorf("tts: context not usable before synthesize: %w", err) }
if u, err := url.Parse(cfg.BaseURL); err != nil || u.Scheme == "" { return fmt.Errorf("tts: invalid base_url %q", cfg.BaseURL) }

Type guard

func validContext(ctx context.Context) bool { return ctx != nil && ctx.Err() == nil }

Try / catch

audio, format, err := tts.Synthesize(ctx, text, voice)
if err != nil {
    if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
        // refresh context / extend deadline and retry once
    }
    return err
}

Prevention

When it happens

Trigger: ctx is already canceled or its deadline has elapsed before Synthesize calls http.NewRequestWithContext; m.BaseURL contains characters that make the joined URL unparseable (e.g. spaces, stray control characters, invalid scheme); an empty/invalid ctx passed by the caller.

Common situations: Caller passes a request context that timed out upstream; a misconfigured BaseURL in config.toml with a typo (missing scheme, embedded space); test harness canceling the context before the call.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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