spring-projects/spring-ai · error · java.lang.IllegalArgumentException

Invalid JSON format for the response:

Error message

Invalid JSON format for the response: 

What it means

After invoking the Bedrock model, internalInvocation deserializes the HTTP response body into the response class with Jackson. If the body is not valid JSON or does not match the target type, the JacksonException is wrapped in an IllegalArgumentException containing the raw response body, signaling an unexpected or malformed response from Bedrock.

Source

Thrown at models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java:254

		}
		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);
		}
	}

	/**
	 * Internal method to invoke the model and return the response stream.
	 *
	 * @param request Model invocation request.
	 * @param clazz Response class type.
	 * @return The model invocation response stream.
	 */
	protected Flux<SO> internalInvocationStream(I request, Class<SO> clazz) {

		// final Sinks.Many<SO> eventSink = Sinks.many().unicast().onBackpressureError();
		final Sinks.Many<SO> eventSink = Sinks.many().multicast().onBackpressureBuffer();

		SdkBytes body;
		try {
			body = SdkBytes.fromUtf8String(this.jsonMapper.writeValueAsString(request));

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Inspect the response body in the message to identify what Bedrock actually returned (often an AWS error message)
  2. Verify the modelId matches the response class expected by the API client
  3. Check AWS credentials/permissions and retry limits so Bedrock stops returning error payloads
  4. Ensure Jackson can deserialize the target type (modules, constructors, @JsonProperty mappings)
Defensive patterns

Strategy: try-catch

Try / catch

try { return api.internalInvocation(request, clazz); } catch (IllegalArgumentException e) { String body = e.getMessage(); log.error("Unexpected Bedrock response: {}", body); throw new AwsResponseException(body, e); }

Prevention

When it happens

Trigger: Bedrock returns a non-JSON body (error page, throttling/HTML message) or JSON whose shape does not fit the expected response class while calling readValue(responseBody, clazz).

Common situations: AWS throttling/access-denied errors returned in unexpected format; wrong response class for the modelId; Bedrock model output exceeding limits and truncated responses; Jackson module/version mismatches.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/09a555e2914e5acc. Report an issue: GitHub.