{"record":{"id":"e2ccb59a6cfa2a3a","repo":"chenhg5/cc-connect","slug":"create-form-file-w","errorCode":null,"errorMessage":"create form file: %w","messagePattern":"create form file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/speech.go","lineNumber":64,"sourceCode":"\t\tmodel = \"whisper-1\"\n\t}\n\treturn &OpenAIWhisper{\n\t\tAPIKey:  apiKey,\n\t\tBaseURL: strings.TrimRight(baseURL, \"/\"),\n\t\tModel:   model,\n\t\tClient:  &http.Client{Timeout: 5 * time.Minute},\n\t}\n}\n\nfunc (w *OpenAIWhisper) Transcribe(ctx context.Context, audio []byte, format string, lang string) (string, error) {\n\text := formatToExt(format)\n\n\tvar buf bytes.Buffer\n\twriter := multipart.NewWriter(&buf)\n\n\tpart, err := writer.CreateFormFile(\"file\", \"audio.\"+ext)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"create form file: %w\", err)\n\t}\n\tif _, err := part.Write(audio); err != nil {\n\t\treturn \"\", fmt.Errorf(\"write audio: %w\", err)\n\t}\n\t_ = writer.WriteField(\"model\", w.Model)\n\t_ = writer.WriteField(\"response_format\", \"text\")\n\tif lang != \"\" {\n\t\t_ = writer.WriteField(\"language\", lang)\n\t}\n\twriter.Close()\n\n\turl := w.BaseURL + \"/audio/transcriptions\"\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &buf)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"create request: %w\", err)\n\t}\n\treq.Header.Set(\"Authorization\", \"Bearer \"+w.APIKey)\n\treq.Header.Set(\"Content-Type\", writer.FormDataContentType())","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/core/speech.go#L46-L82","documentation":"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: ...'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Do not close the multipart writer before all parts are created; keep CreateFormFile/WriteField calls before writer.Close()","Check available memory if buffer allocation failures occur with very large audio payloads","Inspect the wrapped error (%w chain) to identify the underlying writer failure"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"text, err := stt.Transcribe(ctx, audio, format, lang)\nif err != nil {\n    if strings.Contains(err.Error(), \"create form file\") {\n        slog.Error(\"multipart writer corrupted before audio write\", \"err\", err)\n    }\n    return fmt.Errorf(\"transcribe: %w\", err)\n}","preventionTips":["Never close the multipart writer before all parts are created","Create a fresh multipart.Writer per Transcribe call"],"tags":["multipart","stt","whisper"],"backgroundTag":"api-request-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}