{"record":{"id":"6d575d7e98da9c63","repo":"spring-projects/spring-ai","slug":"failed-to-generate-content","errorCode":null,"errorMessage":"Failed to generate content","messagePattern":"Failed to generate content","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":515,"sourceCode":"\t\t\t\t\t\t\ttoChatResponseMetadata(cumulativeUsage, response.modelVersion().get()));\n\t\t\t\t\treturn Flux.just(chatResponse);\n\t\t\t\t});\n\n\t\t\t\tAtomicReference<ChatResponse> aggregatedResponseRef = new AtomicReference<>();\n\n\t\t\t\tFlux<ChatResponse> aggregatedFlux = new MessageAggregator().aggregate(chatResponseFlux,\n\t\t\t\t\t\taggregatedResponse -> {\n\t\t\t\t\t\t\taggregatedResponseRef.set(aggregatedResponse);\n\t\t\t\t\t\t\tobservationContext.setResponse(aggregatedResponse);\n\t\t\t\t\t\t});\n\n\t\t\t\treturn aggregatedFlux.doOnError(observation::error)\n\t\t\t\t\t.doFinally(s -> observation.stop())\n\t\t\t\t\t.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation));\n\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new RuntimeException(\"Failed to generate content\", e);\n\t\t\t}\n\n\t\t});\n\t}\n\n\tprotected List<Generation> responseCandidateToGeneration(Candidate candidate) {\n\n\t\t// TODO - The candidateIndex (e.g. choice must be assigned to the generation).\n\t\tint candidateIndex = candidate.index().orElse(0);\n\t\tFinishReason candidateFinishReason = candidate.finishReason().orElse(new FinishReason(FinishReason.Known.STOP));\n\n\t\tMap<String, Object> messageMetadata = new HashMap<>();\n\t\tmessageMetadata.put(\"candidateIndex\", candidateIndex);\n\t\tmessageMetadata.put(\"finishReason\", candidateFinishReason);\n\n\t\t// Extract thought signatures from response parts if present\n\t\tif (candidate.content().isPresent() && candidate.content().get().parts().isPresent()) {\n\t\t\tList<Part> parts = candidate.content().get().parts().get();","sourceCodeStart":497,"sourceCodeEnd":533,"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#L497-L533","documentation":"internalStream wraps any exception occurring while setting up or starting the streaming generateContent call into RuntimeException(\"Failed to generate content\", e). It signals that the streaming request to the Gemini API could not be created or initiated.","triggerScenarios":"Calling ChatModel.stream(prompt) when the underlying client.createContent / generateContentStream setup throws: invalid request configuration, client initialization failure, missing model, or errors from the genai SDK during streaming setup.","commonSituations":"Invalid model name in options; malformed contents/config built from the prompt; the genai client failing to authenticate or build the streaming call; SDK version mismatches.","solutions":["Read the cause (getCause()) for the underlying genai SDK error.","Verify the model name and request options (temperature, safety settings, thinking config) are valid for that model.","Check API key/credentials and region configuration.","Ensure prompt media parts reference readable resources with detectable MIME types."],"exampleFix":"// before\nflux = chatModel.stream(new Prompt(\"hello\")); // throws \"Failed to generate content\"\n// after\ntry {\n    flux = chatModel.stream(new Prompt(\"hello\"));\n} catch (RuntimeException e) {\n    logger.error(\"stream failed\", e.getCause());\n}","handlingStrategy":"try-catch","validationCode":"// before streaming, validate options\nAssert.hasText(options.getModel(), \"model must be set\");","typeGuard":null,"tryCatchPattern":"try { chatModel.stream(prompt).subscribe(...) } catch (RuntimeException e) { handleCause(e.getCause()); }","preventionTips":["Validate model names and options before streaming","Log getCause() to surface the real SDK error","Keep the genai SDK version aligned with the Spring AI module"],"tags":["streaming","gemini","api-request"],"backgroundTag":"api-request-failed","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"}