Stability AI Image Generation

Spring AI supports Stability AI’s text to image generation model.

Prerequisites

You will need to create an API key with Stability AI to access their AI models, follow their Getting Started documentation.

The Spring AI project defines a configuration property named spring.ai.stabilityai.api-key that you should set to the value of the API Key obtained from Stability AI. Exporting an environment variable in one way to set that configuration property.

export SPRING_AI_STABILITYAI_API_KEY=<INSERT KEY HERE>

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the Stability AI Image Generation Client. To enable it add the following dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-stability-ai-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-stability-ai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Image Generation Properties

The prefix spring.ai.stabilityai is used as the property prefix that lets you connect to Stability AI.

Property Description Default

spring.ai.stabilityai.base-url

The URL to connect to

api.stability.ai/v1

spring.ai.stabilityai.api-key

The API Key

-

The prefix spring.ai.stabilityai.image is the property prefix that lets you configure the ImageClient implementation for Stability AI.

Property Description Default

spring.ai.stabilityai.image.enabled

Enable Stability AI image client.

true

spring.ai.stabilityai.image.base-url

Optional overrides the spring.ai.openai.base-url to provide a specific url

api.stability.ai/v1

spring.ai.stabilityai.image.api-key

Optional overrides the spring.ai.openai.api-key to provide a specific api-key

-

spring.ai.stabilityai.image.option.n

The number of images to be generated. Must be between 1 and 10.

1

spring.ai.stabilityai.image.option.model

The engine/model to use in Stability AI. The model is passed in the URL as a path parameter.

stable-diffusion-v1-6

spring.ai.stabilityai.image.option.width

Width of the image to generate, in pixels, in an increment divisible by 64. Engine-specific dimension validation applies.

512

spring.ai.stabilityai.image.option.height

Height of the image to generate, in pixels, in an increment divisible by 64. Engine-specific dimension validation applies.

512

spring.ai.stabilityai.image.option.responseFormat

The format in which the generated images are returned. Must be "application/json" or "image/png".

-

spring.ai.stabilityai.image.option.cfg_scale

The strictness level of the diffusion process adherence to the prompt text. Range: 0 to 35.

7

spring.ai.stabilityai.image.option.clip_guidance_preset

Pass in a style preset to guide the image model towards a particular style. This list of style presets is subject to change.

NONE

spring.ai.stabilityai.image.option.sampler

Which sampler to use for the diffusion process. If this value is omitted, an appropriate sampler will be automatically selected.

-

spring.ai.stabilityai.image.option.seed

Random noise seed (omit this option or use 0 for a random seed). Valid range: 0 to 4294967295.

0

spring.ai.stabilityai.image.option.steps

Number of diffusion steps to run. Valid range: 10 to 50.

30

spring.ai.stabilityai.image.option.style_preset

Pass in a style preset to guide the image model towards a particular style. This list of style presets is subject to change.

-

Image Options

The StabilityAiImageOptions.java provides model configurations, such as the model to use, the style, the size, etc.

On start-up, the default options can be configured with the StabilityAiImageClient(StabilityAiApi stabilityAiApi, StabilityAiImageOptions options) constructor. Alternatively, use the spring.ai.openai.image.options.* properties described previously.

At runtime, you can override the default options by adding new, request specific, options to the ImagePrompt call. For example to override the Stability AI specific options such as quality and the number of images to create, use the following code example:

ImageResponse response = openaiImageClient.call(
        new ImagePrompt("A light cream colored mini golden doodle",
        StabilityAiImageOptions.builder()
                .withStylePreset("cinematic")
                .withN(4)
                .withHeight(1024)
                .withWidth(1024).build())

);
In addition to the model specific StabilityAiImageOptions you can use a portable ImageOptions instance, created with the ImageOptionsBuilder#builder().