chenhg5/cc-connect · error
write audio: %w
Error message
write audio: %w
What it means
After the multipart part for the audio file is created, the raw audio bytes are written into it via part.Write. If that write fails, the bytes.Buffer backing the writer returned an error (typically allocation/OOM for very large audio payloads). Transcribe wraps it as 'write audio: %w' and aborts the transcription.
Source
Thrown at core/speech.go:67
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())
resp, err := w.Client.Do(req)
if err != nil {View on GitHub (pinned to 4000b2338a)
Solutions
- Increase available memory / container memory limits for large audio files
- Truncate or chunk very long audio before transcription
- Check the wrapped error for out-of-memory indications
Defensive patterns
Strategy: validation
Validate before calling
if len(audio) == 0 {
return "", fmt.Errorf("empty audio payload")
}
if maxAudioBytes > 0 && len(audio) > maxAudioBytes {
return "", fmt.Errorf("audio too large: %d bytes", len(audio))
} Try / catch
if err := transcribe(ctx, audio); err != nil {
var wrapped interface{ Unwrap() error }
if errors.As(err, new(*errors.Error)) || strings.Contains(err.Error(), "write audio") {
slog.Warn("audio write failed, likely memory pressure", "size", len(audio))
}
} Prevention
- Cap audio duration/size before transcription on memory-constrained hosts
- Set container memory limits generously relative to max expected audio size
- Monitor Go heap usage when processing many concurrent voice messages
When it happens
Trigger: Transcribe called with an audio []byte large enough that the in-memory bytes.Buffer fails to grow, or any underlying write error from the multipart part writer.
Common situations: Transcribing long voice messages (multi-MB audio) on memory-constrained hosts or containers with low memory limits; OOM-adjacent failures.
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- create form file: %w
- marshal request: %w
- create form file: %w
- write media data: %w
- close multipart writer: %w
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/5c904b88f26a80ad.
Report an issue: GitHub.