spring-projects/spring-ai · error · AnthropicIoException
Failed to write request body
Error message
Failed to write request body
What it means
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'.
Source
Thrown at models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/http/okhttp/SpringAiAnthropicHttpClient.java:427
@Override
public long contentLength() {
return length;
}
@Override
public boolean repeatable() {
return !isOneShot;
}
@Override
public void writeTo(OutputStream outputStream) {
BufferedSink sink = Okio.buffer(Okio.sink(outputStream));
try {
source.writeTo(sink);
sink.flush();
}
catch (IOException e) {
throw new AnthropicIoException("Failed to write request body", e);
}
}
@Override
public void close() {
}
};
}
private static Headers toAnthropicHeaders(okhttp3.Headers okHttpHeaders) {
Headers.Builder builder = Headers.builder();
for (int i = 0, n = okHttpHeaders.size(); i < n; i++) {
builder.put(okHttpHeaders.name(i), okHttpHeaders.value(i));
}
return builder.build();
}
/**View on GitHub (pinned to 98a7beda4f)
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.
Example fix
// before
client.upload(bigFile); // fails on flaky network
// after
try {
client.upload(bigFile);
} catch (AnthropicIoException e) {
retryWithBackoff(() -> client.upload(bigFile), 3);
} Defensive patterns
Strategy: retry
Validate before calling
static void assertUploadSizeWithin(long bytes, long maxBytes) {
if (bytes > maxBytes) throw new IllegalStateException("Upload too large: " + bytes);
} Try / catch
try {
return client.sendRequest(request);
} catch (AnthropicIoException e) {
if (e.getCause() instanceof IOException) return retryWithBackoff(...); // transient write failure
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Uploading large payloads over unstable networks; request timeouts set shorter than upload duration; proxy or load balancer cutting the connection mid-upload.
Related errors
- Request failed
- Failed to read audio speech response
- Request failed
- Failed to write request body
- Failed to cache the resource:
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/45efdff33d0d62ae.
Report an issue: GitHub.