spring-projects/spring-ai · error · java.lang.IllegalArgumentException
Unknown HarmBlockThreshold:
Error message
Unknown HarmBlockThreshold:
What it means
mapToGenAiHarmBlockThreshold converts GoogleGenAiSafetySetting.HarmBlockThreshold enums to genai SDK thresholds; the default branch throws IllegalArgumentException for any value the switch does not map. It means a threshold value outside the supported set reached the mapper.
Source
Thrown at models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java:849
};
}
private static com.google.genai.types.HarmBlockThreshold mapToGenAiHarmBlockThreshold(
GoogleGenAiSafetySetting.HarmBlockThreshold threshold) {
return switch (threshold) {
case HARM_BLOCK_THRESHOLD_UNSPECIFIED -> new com.google.genai.types.HarmBlockThreshold(
com.google.genai.types.HarmBlockThreshold.Known.HARM_BLOCK_THRESHOLD_UNSPECIFIED);
case BLOCK_LOW_AND_ABOVE -> new com.google.genai.types.HarmBlockThreshold(
com.google.genai.types.HarmBlockThreshold.Known.BLOCK_LOW_AND_ABOVE);
case BLOCK_MEDIUM_AND_ABOVE -> new com.google.genai.types.HarmBlockThreshold(
com.google.genai.types.HarmBlockThreshold.Known.BLOCK_MEDIUM_AND_ABOVE);
case BLOCK_ONLY_HIGH -> new com.google.genai.types.HarmBlockThreshold(
com.google.genai.types.HarmBlockThreshold.Known.BLOCK_ONLY_HIGH);
case BLOCK_NONE -> new com.google.genai.types.HarmBlockThreshold(
com.google.genai.types.HarmBlockThreshold.Known.BLOCK_NONE);
case OFF ->
new com.google.genai.types.HarmBlockThreshold(com.google.genai.types.HarmBlockThreshold.Known.OFF);
default -> throw new IllegalArgumentException("Unknown HarmBlockThreshold: " + threshold);
};
}
private static ThinkingLevel mapToGenAiThinkingLevel(GoogleGenAiThinkingLevel level) {
return switch (level) {
case THINKING_LEVEL_UNSPECIFIED -> new ThinkingLevel(ThinkingLevel.Known.THINKING_LEVEL_UNSPECIFIED);
case MINIMAL -> new ThinkingLevel(ThinkingLevel.Known.MINIMAL);
case LOW -> new ThinkingLevel(ThinkingLevel.Known.LOW);
case MEDIUM -> new ThinkingLevel(ThinkingLevel.Known.MEDIUM);
case HIGH -> new ThinkingLevel(ThinkingLevel.Known.HIGH);
};
}
private static FunctionCallingConfigMode mapToFunctionCallingConfigMode(
GoogleGenAiChatOptions.ToolChoice.Mode mode) {
return switch (mode) {
case AUTO -> new FunctionCallingConfigMode(FunctionCallingConfigMode.Known.AUTO);
case ANY -> new FunctionCallingConfigMode(FunctionCallingConfigMode.Known.ANY);View on GitHub (pinned to 98a7beda4f)
Solutions
- Use only mapped values such as BLOCK_ONLY_HIGH, BLOCK_NONE, OFF and the other cases the switch handles.
- Check the exception message for the offending threshold value and fix your safety configuration.
- Update the library or the mapper if a newly introduced enum constant is missing.
- Parse config-driven thresholds defensively against the supported enum set.
Example fix
// before new GoogleGenAiSafetySetting(category, HarmBlockThreshold.HARM_THRESHOLD_UNSPECIFIED); // after new GoogleGenAiSafetySetting(category, HarmBlockThreshold.BLOCK_ONLY_HIGH);
Defensive patterns
Strategy: type-guard
Validate before calling
Set<HarmBlockThreshold> allowed = Set.of(BLOCK_NONE, BLOCK_ONLY_HIGH, OFF /* plus other mapped cases */);
if (!allowed.contains(threshold)) throw new IllegalArgumentException("unsupported: " + threshold); Type guard
boolean isMapped(HarmBlockThreshold t) { return EnumSet.of(BLOCK_NONE, BLOCK_ONLY_HIGH, OFF).contains(t); } Try / catch
try { applySafety(settings); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith("Unknown HarmBlockThreshold")) fixConfig(); else throw e; } Prevention
- Use only thresholds handled by the mapper
- Avoid config-driven valueOf without validation
- Verify mappings after version upgrades
When it happens
Trigger: Configuring a safety setting with a HarmBlockThreshold constant not handled by the switch (e.g. HARM_THRESHOLD_UNSPECIFIED or a new constant) when constructing Gemini safety settings.
Common situations: Version drift where the enum gained new constants not yet mapped; building thresholds dynamically from config strings; copying thresholds from Vertex/other SDKs with different names.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Unknown HarmCategory:
- Incomplete Google GenAI configuration: Provide 'api-key' for
- Gemini doesn't support message type:
- Failed to generate content
- ThinkingLevel.%s is not supported for model '%s'. This model
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/dffff49c3d24c564.
Report an issue: GitHub.