DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio
Documentation

DALLEImageGenerator

Generate images using OpenAI's DALL-E model.

Most common position in a pipelineAfter a PromptBuilder, flexible
Mandatory init variables"api_key": An OpenAI API key. Can be set with OPENAI_API_KEY env var.
Mandatory run variables“prompt”: A string containing the prompt for the model
Output variables“images”: A list of generated images

”revised_prompt”: A string containing the prompt that was used to generate the image, if there was any revision to the prompt made by OpenAI
API referenceGenerators
GitHub linkhttps://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/openai_dalle.py

Overview

The DALLEImageGenerator component generates images using OpenAI's DALL-E model.

By default, the component uses dall-e-3 model, standard picture quality, and 1024x1024 resolution. You can change these parameters using model (during component initialization), quality, and size (during component initialization or run) parameters.

DALLEImageGenerator needs an OpenAI key to work. It uses an OPENAI_API_KEY environment variable by default. Otherwise, you can pass an API key at initialization with api_key:

image_generator = DALLEImageGenerator(api_key=Secret.from_token("<your-api-key>"))

Check our API reference for the detailed component parameters description, or the OpenAI documentation for the details on OpenAI API parameters.

Usage

On its own

from haystack.components.generators import DALLEImageGenerator

image_generator = DALLEImageGenerator()
response = image_generator.run("Show me a picture of a black cat.")

print(response)

In a pipeline

In the following pipeline, we first set up a PromptBuilder that will structure the image description with a detailed template describing various artistic elements. The pipeline then passes this structured prompt into a DALLEImageGenerator to generate the image based on this detailed description.

from haystack import Pipeline
from haystack.components.generators import DALLEImageGenerator
from haystack.components.builders import PromptBuilder

prompt_builder = PromptBuilder(
    template="""Create a {style} image with the following details:

                Main subject: {prompt}
                Artistic style: {art_style}
                Lighting: {lighting}
                Color palette: {colors}
                Composition: {composition}
                Additional details: {details}"""
)

image_generator = DALLEImageGenerator()

pipeline = Pipeline()
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("image_generator", image_generator)

pipeline.connect("prompt_builder.prompt", "image_generator.prompt")

results = pipeline.run(
    {
        "prompt": "a mystical treehouse library",
        "style": "photorealistic",
        "art_style": "fantasy concept art with intricate details",
        "lighting": "dusk with warm lantern light glowing from within",
        "colors": "rich earth tones, deep greens, and golden accents",
        "composition": "wide angle view showing the entire structure nestled in an ancient oak tree",
        "details": "spiral staircases wrapping around branches, stained glass windows, floating books, and magical fireflies providing ambient illumination"
    }
)

generated_images = results["image_generator"]["images"]
revised_prompt = results["image_generator"]["revised_prompt"]

print(f"Generated image URL: {generated_images[0]}")
print(f"Revised prompt: {revised_prompt}")