{"record":{"id":"45efdff33d0d62ae","repo":"spring-projects/spring-ai","slug":"failed-to-write-request-body","errorCode":null,"errorMessage":"Failed to write request body","messagePattern":"Failed to write request body","errorType":"exception","errorClass":"AnthropicIoException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/http/okhttp/SpringAiAnthropicHttpClient.java","lineNumber":427,"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 AnthropicIoException(\"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 toAnthropicHeaders(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":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/http/okhttp/SpringAiAnthropicHttpClient.java#L409-L445","documentation":"The custom OkHttp RequestBody created by toHttpRequestBody writes its content to the network sink via source.writeTo(sink). If an IOException occurs while streaming bytes to the output stream, it is wrapped in AnthropicIoException 'Failed to write request body'.","triggerScenarios":"The connection breaks while the request body is being uploaded (large uploads), the source content itself throws while reading, or the socket write times out during writeTo.","commonSituations":"Uploading large payloads over unstable networks; request timeouts set shorter than upload duration; proxy or load balancer cutting the connection mid-upload.","solutions":["Retry the request; transient write failures on uploads are usually recoverable.","Reduce payload size or enable compression before upload.","Increase OkHttp writeTimeout/callTimeout for large request bodies.","Check network stability/proxy configuration and inspect the wrapped cause."],"exampleFix":"// before\nclient.upload(bigFile); // fails on flaky network\n// after\ntry {\n    client.upload(bigFile);\n} catch (AnthropicIoException e) {\n    retryWithBackoff(() -> client.upload(bigFile), 3);\n}","handlingStrategy":"retry","validationCode":"static void assertUploadSizeWithin(long bytes, long maxBytes) {\n    if (bytes > maxBytes) throw new IllegalStateException(\"Upload too large: \" + bytes);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return client.sendRequest(request);\n} catch (AnthropicIoException e) {\n    if (e.getCause() instanceof IOException) return retryWithBackoff(...); // transient write failure\n    throw e;\n}","preventionTips":["Set writeTimeout/callTimeout high enough for the largest expected upload.","Retry idempotent uploads automatically on IO write failures.","Monitor network stability and avoid very large single-shot uploads."],"tags":["network","io","upload"],"backgroundTag":"broken-pipe","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}