chenhg5/cc-connect · error

mimo tts: create request: %w

Error message

mimo tts: create request: %w

What it means

This error wraps http.NewRequestWithContext failing while building the POST to MiMo's /chat/completions TTS endpoint. As with the MiniMax equivalent, it fires only when the context is already canceled/expired, the URL is unparseable, or the method is invalid — the request never reached the network.

Source

Thrown at core/tts.go:476

		"model": m.Model,
		"messages": []map[string]any{
			{"role": "user", "content": ""},
			{"role": "assistant", "content": text},
		},
		"audio": map[string]any{
			"format": "wav",
			"voice":  voice,
		},
	}
	jsonData, err := json.Marshal(reqBody)
	if err != nil {
		return nil, "", fmt.Errorf("mimo tts: marshal request: %w", err)
	}

	url := strings.TrimRight(m.BaseURL, "/") + "/chat/completions"
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(jsonData))
	if err != nil {
		return nil, "", fmt.Errorf("mimo tts: create request: %w", err)
	}
	// MiMo authenticates via "api-key" header, not "Authorization: Bearer".
	req.Header.Set("api-key", m.APIKey)
	req.Header.Set("Content-Type", "application/json")

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

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

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check ctx.Err() at the call site to confirm context cancellation, and use a fresh/longer-lived context.
  2. Validate base_url in config.toml: absolute URL, no spaces, correct scheme, quotes trimming fine.
  3. Add a deadline margin so the TTS call is not entered with <1s remaining on a parent deadline.
  4. Retry with a new context if the cancellation was transient.

Example fix

// before
base_url = "https ://api.mimo.example.com"   # stray space → invalid URL
// after
base_url = "https://api.mimo.example.com"
Defensive patterns

Strategy: try-catch

Validate before calling

if err := ctx.Err(); err != nil { return err }
if u, err := url.Parse(cfg.BaseURL); err != nil || u.Scheme == "" || strings.ContainsAny(cfg.BaseURL, " \t") { return fmt.Errorf("invalid mimo 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.DeadlineExceeded) || errors.Is(err, context.Canceled) {
        // use a fresh, longer-lived context and retry once
    }
    return err
}

Prevention

When it happens

Trigger: ctx canceled or deadline elapsed before the call; base_url malformed (invalid scheme, spaces, control chars) making TrimRight(base)+"/chat/completions" unparseable.

Common situations: Wrong mimo base_url in config.toml (typo, missing https://, stray whitespace in the TOML string); caller passing an already-expired context; upstream timeout exhausted before TTS was invoked.

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/8f76809541cd3588. Report an issue: GitHub.