{"record":{"id":"7ec5f9dab10c3f42","repo":"spring-projects/spring-ai","slug":"failed-to-write-request-body-7ec5f9","errorCode":null,"errorMessage":"Failed to write request body","messagePattern":"Failed to write request body","errorType":"exception","errorClass":"OpenAIIoException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-openai/src/main/java/org/springframework/ai/openai/http/okhttp/SpringAiOpenAiHttpClient.java","lineNumber":377,"sourceCode":"\t\t\t@Override\n\t\t\tpublic long contentLength() {\n\t\t\t\treturn length;\n\t\t\t}\n\n\t\t\t@Override\n\t\t\tpublic boolean repeatable() {\n\t\t\t\treturn !isOneShot;\n\t\t\t}\n\n\t\t\t@Override\n\t\t\tpublic void writeTo(OutputStream outputStream) {\n\t\t\t\tBufferedSink sink = Okio.buffer(Okio.sink(outputStream));\n\t\t\t\ttry {\n\t\t\t\t\tsource.writeTo(sink);\n\t\t\t\t\tsink.flush();\n\t\t\t\t}\n\t\t\t\tcatch (IOException e) {\n\t\t\t\t\tthrow new OpenAIIoException(\"Failed to write request body\", e);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t@Override\n\t\t\tpublic void close() {\n\t\t\t}\n\t\t};\n\t}\n\n\tprivate static Headers toOpenAiHeaders(okhttp3.Headers okHttpHeaders) {\n\t\tHeaders.Builder builder = Headers.builder();\n\t\tfor (int i = 0, n = okHttpHeaders.size(); i < n; i++) {\n\t\t\tbuilder.put(okHttpHeaders.name(i), okHttpHeaders.value(i));\n\t\t}\n\t\treturn builder.build();\n\t}\n\n\t/**","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/http/okhttp/SpringAiOpenAiHttpClient.java#L359-L395","documentation":"Wraps an IOException thrown while streaming the request body into the OkHttp sink inside SpringAiOpenAiHttpClient's repeatable request-body writer; the outbound HTTP request could not be fully written (broken pipe, connection reset, or sink failure).","triggerScenarios":"OkHttp serializing the request body to the network when the underlying source throws IOException — connection dropped mid-write, source stream read error, or sink closed unexpectedly (SpringAiOpenAiHttpClient.java:377).","commonSituations":"Uploading large payloads (long audio/images) over an unstable connection, a body stream supplied by another thread that gets closed, or read timeouts during upload.","solutions":["Retry the request; a mid-write connection reset is usually transient.","Check that the body's source stream is not shared or concurrently closed.","Increase write/read timeouts for large uploads and inspect the cause chain."],"exampleFix":"int attempts = 0;\nwhile (true) {\n    try { return client.execute(request, options); }\n    catch (OpenAIIoException e) {\n        if (++attempts >= 3 || e.getCause() == null) throw e;\n        Thread.sleep(500L * attempts);\n    }\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    return client.execute(request, options);\n} catch (OpenAIIoException e) {\n    if (e.getMessage().contains(\"write request body\")) {\n        // transient mid-write failure: retry with backoff, else surface\n    }\n    throw e;\n}","preventionTips":["Retry idempotent requests after mid-write IO failures","Avoid sharing the body stream across threads","Increase timeouts for large uploads (images/audio)","Keep the source stream open until the call completes"],"tags":["io","okhttp","request-body","network"],"backgroundTag":"broken-pipe","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}