Spring / Spring AI interview questions
What is image generation in Spring AI and how do you use ImageModel?
Spring AI provides an ImageModel abstraction for generating images from text descriptions (text-to-image). Providers that support it include OpenAI (DALL-E 2, DALL-E 3), Azure OpenAI, and Google Vertex AI (Imagen). The interface is separate from ChatModel because image generation has a fundamentally different request/response shape.
@Service public class ImageService { private final ImageModel imageModel; public ImageService(ImageModel imageModel) { this.imageModel = imageModel; } public String generateImageUrl(String description) { ImageResponse response = imageModel.call( new ImagePrompt(description, OpenAiImageOptions.builder() .withQuality("hd") .withN(1) .withWidth(1024) .withHeight(1024) .build()) ); return response.getResult().getOutput().getUrl(); } }
The ImagePrompt wraps the textual description and optional ImageOptions that control quality, size, number of images, and style. The ImageResponse contains a list of ImageGeneration objects, each of which holds either a URL to the generated image (which expires after a provider-defined TTL) or a base64-encoded data URI, depending on the options you specify.
For DALL-E 3 you can also specify a revised_prompt response field — the model rewrites your prompt internally and returns both the original and the revised version it actually used.
More Related questions...