Replies: 3 comments
|
Hi, For this use case, With Vertex AI Gemini, image output normally comes through
"responseModalities": ["TEXT", "IMAGE"]
Example structure: for (Part part : response.getCandidates(0).getContent().getPartsList()) {
if (part.hasText()) {
String text = part.getText();
}
if (part.hasInlineData()) {
String mimeType = part.getInlineData().getMimeType();
String base64 = Base64.getEncoder().encodeToString(
part.getInlineData().getData().toByteArray()
);
String imageUrl = "data:" + mimeType + ";base64," + base64;
}
}Then the frontend can render it like this: <img src="data:image/png;base64,BASE64_IMAGE_HERE" />or dynamically: img.src = `data:${mimeType};base64,${base64}`;So the issue is probably not that the image “does not render” directly. The real issue is that the Spring AI If Spring AI’s |
|
Have you looked at what the response payload is actually returning? If the Gemini API is sending back image data correctly, the problem might lie in how Spring AI handles the multimodal response, not in Vertex AI itself. I recommend checking the raw response first to see if the image parts are present as base64 data. If they are, you might need to process the individual content parts instead of depending on the default text response handling from ChatClient. Also, which version of Spring AI are you using? It would be helpful to see a small code example of how you're calling Gemini and how you're trying to display the image content. |
|
The issue is that Working codeimport org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatOptions;
import org.springframework.util.MimeType;
import java.util.Base64;
@Service
public class GeminiMultimodalService {
private final ChatClient chatClient;
public GeminiMultimodalService(ChatClient.Builder builder) {
this.chatClient = builder
.defaultOptions(VertexAiGeminiChatOptions.builder()
.model("gemini-2.0-flash-001")
// Tell Gemini to return both text and images
.responseModalities(List.of("TEXT", "IMAGE"))
.build())
.build();
}
public record MultimodalResult(String text, List<String> imageDataUrls) {}
public MultimodalResult generate(String prompt) {
ChatResponse response = chatClient.prompt()
.user(prompt)
.call()
.chatResponse();
List<String> images = new ArrayList<>();
StringBuilder text = new StringBuilder();
for (var generation : response.getResults()) {
AssistantMessage msg = generation.getOutput();
// Text part
if (msg.getText() != null) {
text.append(msg.getText());
}
// Image parts — each media item has a MimeType and byte[] data
for (AssistantMessage.Media media : msg.getMedia()) {
MimeType mimeType = media.getMimeType();
if (mimeType.getType().equals("image")) {
byte[] imageBytes = (byte[]) media.getData();
String base64 = Base64.getEncoder().encodeToString(imageBytes);
// Ready to embed in an <img src="..."> tag
images.add("data:" + mimeType + ";base64," + base64);
}
}
}
return new MultimodalResult(text.toString(), images);
}
}REST endpoint example@RestController
@RequestMapping("/api/generate")
public class GenerateController {
@Autowired
private GeminiMultimodalService service;
@PostMapping
public ResponseEntity<Map<String, Object>> generate(@RequestBody Map<String, String> body) {
var result = service.generate(body.get("prompt"));
return ResponseEntity.ok(Map.of(
"text", result.text(),
"images", result.imageDataUrls() // send data URLs to frontend
));
}
}Frontend rendering (React / plain HTML)// React
{result.images.map((dataUrl, i) => (
<img key={i} src={dataUrl} alt={`Generated image ${i + 1}`} />
))}Key points
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello,
We are using Spring AI with Vertex AI API to call Gemini. Now for some responses, we expect both text as well as Images (base64). I have tried using ChatClient to retrieve the whole response containing interleaving text and images(base64), but couldn't do it so far, the image does not render. Can anyone guide me how to do this using Spring AI?
Thank you in advance.
All reactions