spring-projects/spring-ai · error · java.lang.IllegalArgumentException
Invalid JSON format for the input request:
Error message
Invalid JSON format for the input request:
What it means
internalInvocation serializes the request object to JSON with the configured Jackson ObjectMapper before sending it to Bedrock's InvokeModel API. If Jackson cannot serialize the request (JacksonException), the library wraps it in an IllegalArgumentException, indicating the request object is not JSON-mappable — usually a configuration/programming error in the request object.
Source
Thrown at models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java:238
/**
* Internal method to invoke the model and return the response.
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html
* https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
* https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrockruntime/BedrockRuntimeClient.html#invokeModel
*
* @param request Model invocation request.
* @param clazz The response class type
* @return The model invocation response.
*
*/
protected O internalInvocation(I request, Class<O> clazz) {
SdkBytes body;
try {
body = SdkBytes.fromUtf8String(this.jsonMapper.writeValueAsString(request));
}
catch (JacksonException e) {
throw new IllegalArgumentException("Invalid JSON format for the input request: " + request, e);
}
InvokeModelRequest invokeRequest = InvokeModelRequest.builder()
.modelId(this.modelId)
.body(body)
.build();
InvokeModelResponse response = this.client.invokeModel(invokeRequest);
String responseBody = response.body().asString(StandardCharsets.UTF_8);
try {
return this.jsonMapper.readValue(responseBody, clazz);
}
catch (JacksonException e) {
throw new IllegalArgumentException("Invalid JSON format for the response: " + responseBody, e);
}
}View on GitHub (pinned to 98a7beda4f)
Solutions
- Inspect the wrapped cause (e) for the exact Jackson serialization failure and fix the offending field/getter
- Ensure the request object matches the API class's expected request type and has Jackson-visible accessors
- Align Jackson versions/modules on the classpath with those used by spring-ai-bedrock
Example fix
// before
record MyRequest(SomeUnserializable obj) {} // fails serialization
// after
record MyRequest(@JsonProperty("input") String input) {} // simple JSON-friendly types Defensive patterns
Strategy: try-catch
Validate before calling
try { new ObjectMapper().writeValueAsString(request); } catch (JacksonException e) { throw new IllegalStateException("Request not JSON-serializable: " + e.getMessage()); } Try / catch
try { api.internalInvocation(request, responseType); } catch (IllegalArgumentException e) { if (e.getCause() instanceof JacksonException je) { log.error("Serialize failed: {}", je.getMessage()); } throw e; } Prevention
- Keep request DTOs to JSON-friendly types with proper accessors
- Ensure required Jackson modules (records/Java8) are on the classpath
- Align Jackson versions with those used by spring-ai-bedrock
When it happens
Trigger: Invoking a Bedrock model via AbstractBedrockApi when the request object contains unserializable types, mismatched Jackson modules, or fails getter access, causing writeValueAsString to throw.
Common situations: Custom request DTOs without Jackson-compatible getters; missing jackson modules (e.g. Java 8/records support); passing objects of the wrong generic type I; classpath Jackson version conflicts.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Tool execution results are not supported for Bedrock models
- Invalid JSON format for the response:
- Region is empty and cannot be loaded from DefaultAwsRegionPr
- Error serializing documentMap to JSON.
- Failed to serialize the Document metadata:
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/cd4269ab8b58062d.
Report an issue: GitHub.