{"record":{"id":"afbf6802b6e7faa2","repo":"spring-projects/spring-ai","slug":"failed-to-convert-map-to-json","errorCode":null,"errorMessage":"Failed to convert map to JSON","messagePattern":"Failed to convert map to JSON","errorType":"exception","errorClass":"java.lang.RuntimeException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/GoogleGenAiChatModel.java","lineNumber":385,"sourceCode":"\t\t\t}\n\t\t\telse {\n\t\t\t\t// It's a primitive or other type, wrap it\n\t\t\t\tMap<String, Object> wrapper = new HashMap<>();\n\t\t\t\twrapper.put(\"result\", parsed);\n\t\t\t\treturn wrapper;\n\t\t\t}\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new RuntimeException(\"Failed to parse JSON: \" + json, e);\n\t\t}\n\t}\n\n\tprivate String mapToJson(Map<String, Object> map) {\n\t\ttry {\n\t\t\treturn this.jsonMapper.writeValueAsString(map);\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new RuntimeException(\"Failed to convert map to JSON\", e);\n\t\t}\n\t}\n\n\tprivate Schema jsonToSchema(String json) {\n\t\ttry {\n\t\t\treturn this.jsonMapper.readValue(json, Schema.class);\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t}\n\n\t// https://googleapis.github.io/java-genai/javadoc/com/google/genai/types/GenerationConfig.html\n\t@Override\n\tpublic ChatResponse call(Prompt prompt) {\n\t\tvar requestPrompt = this.buildRequestPrompt(prompt);\n\t\treturn this.internalCall(requestPrompt, null);\n\t}","sourceCodeStart":367,"sourceCodeEnd":403,"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#L367-L403","documentation":"mapToJson() serializes a Map<String,Object> to a JSON string using the model's ObjectMapper for Gemini function-call arguments. Serialization failure is unexpected for in-memory maps but possible with unserializable values (e.g. objects with no accessible properties, cyclic references, or incompatible Jackson modules), and is rethrown as a RuntimeException with this message. It is raised while building function call arguments (functionArguments).","triggerScenarios":"A tool function returns or builds arguments whose map contains values Jackson cannot serialize (cyclic object graphs, types without getters, custom classes lacking a serializer) when functionArguments assembles the FunctionCall payload.","commonSituations":"Returning domain objects with circular references from @Tool methods; maps containing exotic types (InputStream, LocalDateTime without JavaTimeModule registered); custom JsonMapper configurations missing required modules.","solutions":["Return simple, JSON-friendly types (Map<String,String/Number>, records, POJOs with getters) from tool functions.","Register the needed Jackson modules (e.g. JavaTimeModule) on the jsonMapper used by the chat model, or configure the model's JsonMapper.","Break cyclic references (use @JsonIgnore or DTOs) before returning data used as function-call arguments."],"exampleFix":"// before\nMap<String, Object> result = Map.of(\"created\", LocalDateTime.now()); // fails without JavaTimeModule\n\n// after\nMap<String, Object> result = Map.of(\"created\", LocalDateTime.now().toString());","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    String json = mapToJson(map);\n}\ncatch (RuntimeException e) {\n    logger.warn(\"Map not serializable: {}\", e.getMessage());\n    // sanitize map values or fall back to a safe DTO\n}","preventionTips":["Return JSON-friendly types (records, simple maps) from tool functions.","Register Jackson modules like JavaTimeModule on the model's JsonMapper.","Eliminate cyclic references in objects used as function-call arguments."],"tags":["chat","json","serialization","function-calling"],"backgroundTag":"json-serialization-failed","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}