Skip to main content

Ollama Image Generation

warning

Ollama image generation is experimental. The Ollama API and LangChain4j integration may change in future versions.

Maven Dependency

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-ollama</artifactId>
<version>${latest version here}</version>
</dependency>

API

  • OllamaImageModel

OllamaImageModel implements ImageModel and supports text-to-image generation through Ollama's standard /api/generate endpoint. Image editing and generating multiple images from the same prompt are not supported.

This requires an Ollama version and model that support the experimental image generation capability. Regular text models return text instead of an image and will fail with OllamaImageGenerationException.

Usage

ImageModel imageModel = OllamaImageModel.builder()
.baseUrl("http://localhost:11434")
.modelName("x/z-image-turbo")
.width(1024)
.height(768)
.steps(20)
.seed(42)
.build();

Response<Image> response = imageModel.generate("a sunset over mountains");
Image image = response.content();
byte[] imageBytes = Base64.getDecoder().decode(image.base64Data());
Files.write(Path.of("ollama-image.png"), imageBytes);

Parameters

ParameterDescriptionType
httpClientBuilderSee Customizable HTTP ClientHttpClientBuilder
baseUrlThe base URL of the Ollama server.String
modelNameThe image generation model to use from the Ollama server.String
widthWidth of the generated image in pixels. Use 0 or leave unset to use the Ollama/model default. Must be between 0 and 4096 when set.Integer
heightHeight of the generated image in pixels. Use 0 or leave unset to use the Ollama/model default. Must be between 0 and 4096 when set.Integer
stepsNumber of diffusion steps. Use 0 or leave unset to use the Ollama/model default. Must not be negative.Integer
seedRandom seed sent through Ollama options.seed.Integer
timeoutThe maximum time allowed for the API call to complete.Duration
customHeadersCustom HTTP headers.Map<String, String>
logRequestsWhether to log requests.Boolean
logResponsesWhether to log responses.Boolean
maxRetriesThe maximum number of retries in case of API call failure.Integer

There is currently no Spring Boot starter configuration for OllamaImageModel.

The generated image is returned as Base64-encoded PNG data. URL output, token usage, and Ollama streaming progress fields such as completed and total are not exposed.