{"record":{"id":"6a16a0fbd5d89f7f","repo":"spring-projects/spring-ai","slug":"unknown-harmcategory","errorCode":null,"errorMessage":"Unknown HarmCategory: ","messagePattern":"Unknown HarmCategory: ","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java","lineNumber":830,"sourceCode":"\t\t\t\tprompt.getInstructions().stream().filter(m -> m.getMessageType() != MessageType.SYSTEM).toList()),\n\t\t\t\tmodelName, config);\n\t}\n\n\t// Helper methods for mapping safety settings enums\n\tprivate static com.google.genai.types.HarmCategory mapToGenAiHarmCategory(\n\t\t\tGoogleGenAiSafetySetting.HarmCategory category) {\n\t\treturn switch (category) {\n\t\t\tcase HARM_CATEGORY_UNSPECIFIED -> new com.google.genai.types.HarmCategory(\n\t\t\t\t\tcom.google.genai.types.HarmCategory.Known.HARM_CATEGORY_UNSPECIFIED);\n\t\t\tcase HARM_CATEGORY_HATE_SPEECH -> new com.google.genai.types.HarmCategory(\n\t\t\t\t\tcom.google.genai.types.HarmCategory.Known.HARM_CATEGORY_HATE_SPEECH);\n\t\t\tcase HARM_CATEGORY_DANGEROUS_CONTENT -> new com.google.genai.types.HarmCategory(\n\t\t\t\t\tcom.google.genai.types.HarmCategory.Known.HARM_CATEGORY_DANGEROUS_CONTENT);\n\t\t\tcase HARM_CATEGORY_HARASSMENT -> new com.google.genai.types.HarmCategory(\n\t\t\t\t\tcom.google.genai.types.HarmCategory.Known.HARM_CATEGORY_HARASSMENT);\n\t\t\tcase HARM_CATEGORY_SEXUALLY_EXPLICIT -> new com.google.genai.types.HarmCategory(\n\t\t\t\t\tcom.google.genai.types.HarmCategory.Known.HARM_CATEGORY_SEXUALLY_EXPLICIT);\n\t\t\tdefault -> throw new IllegalArgumentException(\"Unknown HarmCategory: \" + category);\n\t\t};\n\t}\n\n\tprivate static com.google.genai.types.HarmBlockThreshold mapToGenAiHarmBlockThreshold(\n\t\t\tGoogleGenAiSafetySetting.HarmBlockThreshold threshold) {\n\t\treturn switch (threshold) {\n\t\t\tcase HARM_BLOCK_THRESHOLD_UNSPECIFIED -> new com.google.genai.types.HarmBlockThreshold(\n\t\t\t\t\tcom.google.genai.types.HarmBlockThreshold.Known.HARM_BLOCK_THRESHOLD_UNSPECIFIED);\n\t\t\tcase BLOCK_LOW_AND_ABOVE -> new com.google.genai.types.HarmBlockThreshold(\n\t\t\t\t\tcom.google.genai.types.HarmBlockThreshold.Known.BLOCK_LOW_AND_ABOVE);\n\t\t\tcase BLOCK_MEDIUM_AND_ABOVE -> new com.google.genai.types.HarmBlockThreshold(\n\t\t\t\t\tcom.google.genai.types.HarmBlockThreshold.Known.BLOCK_MEDIUM_AND_ABOVE);\n\t\t\tcase BLOCK_ONLY_HIGH -> new com.google.genai.types.HarmBlockThreshold(\n\t\t\t\t\tcom.google.genai.types.HarmBlockThreshold.Known.BLOCK_ONLY_HIGH);\n\t\t\tcase BLOCK_NONE -> new com.google.genai.types.HarmBlockThreshold(\n\t\t\t\t\tcom.google.genai.types.HarmBlockThreshold.Known.BLOCK_NONE);\n\t\t\tcase OFF ->\n\t\t\t\tnew com.google.genai.types.HarmBlockThreshold(com.google.genai.types.HarmBlockThreshold.Known.OFF);","sourceCodeStart":812,"sourceCodeEnd":848,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java#L812-L848","documentation":"mapToGenAiHarmCategory converts Spring AI GoogleGenAiSafetySetting.HarmCategory enums to genai SDK HarmCategory values; an unknown/unmapped category throws IllegalArgumentException. This indicates the safety category enum was extended without updating the mapper, or an unexpected value reached the switch.","triggerScenarios":"Passing a HarmCategory value to GoogleGenAiSafetySetting that the switch statement does not handle (e.g. HARM_CATEGORY_UNSPECIFIED, HARM_CATEGORY_HATE_SPEECH if unmapped, or a newly added enum constant).","commonSituations":"Upgrading spring-ai-google-genai where new enum constants appear; copying safety settings from another provider with different category names; typos when constructing categories dynamically from strings.","solutions":["Use only the mapped categories: HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_SEXUALLY_EXPLICIT (and any others the current source maps).","Print the offending category value from the exception message and align your safety settings configuration.","Upgrade or patch the module if a newly added enum constant is missing from the mapper.","Avoid building categories from arbitrary strings via valueOf."],"exampleFix":"// before\nnew GoogleGenAiSafetySetting(HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold); // if unmapped\n// after\nnew GoogleGenAiSafetySetting(HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold);","handlingStrategy":"type-guard","validationCode":"private static final Set<HarmCategory> SUPPORTED = Set.of(HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_SEXUALLY_EXPLICIT);\nif (!SUPPORTED.contains(category)) throw new IllegalArgumentException(\"unsupported: \" + category);","typeGuard":"boolean isMapped(HarmCategory c) { return EnumSet.of(HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_SEXUALLY_EXPLICIT).contains(c); }","tryCatchPattern":"try { applySafety(settings); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Unknown HarmCategory\")) fixConfig(); else throw e; }","preventionTips":["Only use enum constants documented as mapped","Don't build enums from raw strings via valueOf","Re-check safety settings after library upgrades"],"tags":["enum","safety-settings","gemini"],"backgroundTag":"invalid-enum-value","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}