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

Unsupported value type:

Error message

Unsupported value type:

What it means

ConverseApiUtils.convertObjectToDocument() converts a Java object into a software.amazon.awssdk Document for Bedrock request fields. It handles List, Map, String, Boolean, and numeric types; anything else (e.g. arbitrary POJOs, dates, enums) falls through to the else branch and throws IllegalArgumentException naming the offending class.

Source

Thrown at models/spring-ai-bedrock-converse/src/main/java/org/springframework/ai/bedrock/converse/api/ConverseApiUtils.java:77

			return Document.fromNumber(floatValue);
		}
		else if (value instanceof Double doubleValue) {
			return Document.fromNumber(doubleValue);
		}
		else if (value instanceof BigDecimal bigDecimalValue) {
			return Document.fromNumber(bigDecimalValue);
		}
		else if (value instanceof BigInteger bigIntegerValue) {
			return Document.fromNumber(bigIntegerValue);
		}
		else if (value instanceof List listValue) {
			return Document.fromList(listValue.stream().map(v -> convertObjectToDocument(v)).toList());
		}
		else if (value instanceof Map mapValue) {
			return convertMapToDocument(mapValue);
		}
		else {
			throw new IllegalArgumentException("Unsupported value type:" + value.getClass().getSimpleName());
		}
	}

	public static Map<String, String> getRequestMetadata(Map<String, Object> metadata) {

		if (metadata.isEmpty()) {
			return Map.of();
		}

		Map<String, String> result = new HashMap<>();
		for (Map.Entry<String, Object> entry : metadata.entrySet()) {
			String key = entry.getKey();
			Object value = entry.getValue();

			if (key != null && value != null) {
				result.put(key, value.toString());
			}
		}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Convert the value to a supported type (String, Number, Boolean, Map, List) before adding it to the options/metadata.
  2. For POJOs, serialize to Map<String,Object> or a JSON string first.
  3. Use convertMapToDocument-style helpers for nested structures.
  4. Avoid Date/enum objects; use their string representations.

Example fix

// before
options.put("timestamp", new Date());
// after
options.put("timestamp", Instant.now().toString());
Defensive patterns

Strategy: type-guard

Validate before calling

for (Object v : options.values()) {
    if (!(v instanceof String || v instanceof Number || v instanceof Boolean
          || v instanceof Map || v instanceof List)) {
        throw new IllegalArgumentException("Unsupported option value type: " + v.getClass());
    }
}

Type guard

static boolean isDocumentConvertible(Object v) {
    return v instanceof String || v instanceof Boolean || v instanceof Number
        || v instanceof Map || v instanceof List;
}

Try / catch

try {
    doc = ConverseApiUtils.convertObjectToDocument(value);
} catch (IllegalArgumentException e) {
    // fall back to value.toString() or a Map conversion
}

Prevention

When it happens

Trigger: Putting an unsupported object type (e.g. a custom POJO, java.util.Date, BigDecimal in some paths) into a generation option map (BedrockChatOptions) or metadata map that is converted via attr()/convertObjectToDocument().

Common situations: Users putting custom option values into BedrockChatOptions (e.g. providerExtensions with raw POJOs); metadata containing complex objects; version changes adding new option types not covered by the converter.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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