chenhg5/cc-connect · error

create form file: %w

Error message

create form file: %w

What it means

OpenAIWhisper.Transcribe builds a multipart/form-data body for the /audio/transcriptions endpoint. CreateFormFile only fails if the multipart writer has already been closed or the writer's underlying buffer errored, which in practice is nearly impossible at this point in the fresh writer's lifecycle. The error is wrapped defensively so any writer corruption surfaces as 'create form file: ...'.

Source

Thrown at core/speech.go:64

		model = "whisper-1"
	}
	return &OpenAIWhisper{
		APIKey:  apiKey,
		BaseURL: strings.TrimRight(baseURL, "/"),
		Model:   model,
		Client:  &http.Client{Timeout: 5 * time.Minute},
	}
}

func (w *OpenAIWhisper) Transcribe(ctx context.Context, audio []byte, format string, lang string) (string, error) {
	ext := formatToExt(format)

	var buf bytes.Buffer
	writer := multipart.NewWriter(&buf)

	part, err := writer.CreateFormFile("file", "audio."+ext)
	if err != nil {
		return "", fmt.Errorf("create form file: %w", err)
	}
	if _, err := part.Write(audio); err != nil {
		return "", fmt.Errorf("write audio: %w", err)
	}
	_ = writer.WriteField("model", w.Model)
	_ = writer.WriteField("response_format", "text")
	if lang != "" {
		_ = writer.WriteField("language", lang)
	}
	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())

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Do not close the multipart writer before all parts are created; keep CreateFormFile/WriteField calls before writer.Close()
  2. Check available memory if buffer allocation failures occur with very large audio payloads
  3. Inspect the wrapped error (%w chain) to identify the underlying writer failure
Defensive patterns

Strategy: try-catch

Try / catch

text, err := stt.Transcribe(ctx, audio, format, lang)
if err != nil {
    if strings.Contains(err.Error(), "create form file") {
        slog.Error("multipart writer corrupted before audio write", "err", err)
    }
    return fmt.Errorf("transcribe: %w", err)
}

Prevention

When it happens

Trigger: Calling Transcribe on an OpenAIWhisper instance where the multipart.Writer reports an error when creating the 'file' form part — e.g. the writer was closed before CreateFormFile or the backing bytes.Buffer write failed.

Common situations: Rarely hit in production; almost always indicates a programming error or memory pressure (buffer allocation failure) rather than user misconfiguration. Developers may see it while testing custom Transcribe implementations or after reusing a closed writer.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


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