Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

OpenAIImageGenerator

Generate images from text prompts using OpenAI image generation models. The component accepts a text prompt and returns generated images along with any revised prompt OpenAI used.

Key Features

  • Uses gpt-image-2 by default.
  • Supports configurable image quality and size.
  • Returns images as base64-encoded JSON strings.
  • Returns the revised prompt OpenAI used for image generation.
  • Compatible with PromptBuilder for dynamic prompt construction.

Configuration

Authentication

To use this component, connect Haystack Platform with OpenAI first. For detailed instructions, see Use OpenAI Models.

  1. Drag the OpenAIImageGenerator component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    1. Select the image generation model. The default is gpt-image-2.
    2. Choose the image quality: auto, high, medium, or low.
    3. Set the image size. The standard options are 1024x1024, 1024x1536, 1536x1024, or auto.
  4. Go to the Advanced tab to configure the OpenAI API key, timeout, maximum retries, API base URL, organization ID, and HTTP client settings.

Connections

OpenAIImageGenerator accepts a prompt string as input. It outputs a list of images (images) and the revised prompt (revised_prompt) OpenAI used for generation.

Typically, you connect a PromptBuilder to the OpenAIImageGenerator's prompt input to build dynamic prompts. The images and revised_prompt outputs connect to an OutputAdapter or AnswerBuilder for further processing.

Source Code

To check this component's source code, open openai_image_generator.py in the Haystack repository.

Usage Examples

Basic Configuration

openai_image_generator:
type: haystack.components.generators.openai_image_generator.OpenAIImageGenerator
init_parameters:
model: gpt-image-2
quality: auto
size: 1024x1024
timeout: 60

Image Generation Pipeline

This pipeline uses OpenAIImageGenerator to generate images based on a query:

# haystack-pipeline
components:
prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: '{{query}}'
openai_image_generator:
type: haystack.components.generators.openai_image_generator.OpenAIImageGenerator
init_parameters:
model: gpt-image-2
quality: auto
size: 1024x1024
timeout: 60
answer_formatter:
type: haystack.components.converters.output_adapter.OutputAdapter
init_parameters:
template: |-
{% set ns = namespace(doc_string='') %}
{% set ns.doc_string = ns.doc_string + '## Query:\n' + query + '\n\n' %}
{% set ns.doc_string = ns.doc_string + '## OpenAIs Revised Prompt:\n' + revised_prompt + '\n\n' %}
{% set ns.doc_string = ns.doc_string + '![](' + images[0] + ')' + '\n\n' %}
{% set answer = [ns.doc_string] %}
{{ answer }}
output_type: List[str]
answer_builder:
type: deepset_cloud_custom_nodes.augmenters.deepset_answer_builder.DeepsetAnswerBuilder
init_parameters: {}

connections:
- sender: prompt_builder.prompt
receiver: openai_image_generator.prompt
- sender: openai_image_generator.revised_prompt
receiver: answer_formatter.revised_prompt
- sender: openai_image_generator.images
receiver: answer_formatter.images
- sender: answer_formatter.output
receiver: answer_builder.replies
- sender: prompt_builder.prompt
receiver: answer_builder.prompt

max_runs_per_component: 100

metadata: {}

inputs:
query:
- prompt_builder.query
- answer_formatter.query
- answer_builder.query

outputs:
answers: answer_builder.answers



Parameters

Inputs

ParameterTypeDefaultDescription
promptstrThe prompt to generate the image.
sizeOptional[Literal['1024x1024', '1024x1536', '1536x1024', 'auto']]NoneIf provided, overrides the size provided during initialization.
qualityOptional[Literal['auto', 'high', 'medium', 'low']]NoneIf provided, overrides the quality provided during initialization.
response_formatOptional[Literal['b64_json']]NoneKept for backward compatibility. The component always returns a base64-encoded image.

Outputs

ParameterTypeDescription
imagesList[str]A list of generated images as base64-encoded JSON strings.
revised_promptstrThe prompt OpenAI used to generate the image. OpenAI may revise the original prompt before generating the image.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
modelstrgpt-image-2The OpenAI image generation model.
qualityLiteral['auto', 'high', 'medium', 'low']autoThe quality of the generated image.
sizeLiteral['1024x1024', '1024x1536', '1536x1024', 'auto']1024x1024The size of the generated image. gpt-image-2 also supports custom sizes.
response_formatLiteral['b64_json']b64_jsonKept for backward compatibility. The component always returns a base64-encoded image.
api_keySecretSecret.from_env_var('OPENAI_API_KEY')The OpenAI API key to connect to OpenAI.
api_base_urlOptional[str]NoneAn optional base URL.
organizationOptional[str]NoneThe Organization ID, defaults to None.
timeoutOptional[float]NoneTimeout for OpenAI Client calls. If not set, it is inferred from the OPENAI_TIMEOUT environment variable or set to 30.
max_retriesOptional[int]NoneMaximum retries to establish contact with OpenAI if it returns an internal error. If not set, it is inferred from the OPENAI_MAX_RETRIES environment variable or set to 5.
http_client_kwargsOptional[Dict[str, Any]]NoneA dictionary of keyword arguments to configure a custom httpx.Clientor httpx.AsyncClient. For more information, see the HTTPX documentation.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
promptstrThe prompt to generate the image.
sizeOptional[Literal['1024x1024', '1024x1536', '1536x1024', 'auto']]NoneIf provided, overrides the size provided during initialization.
qualityOptional[Literal['auto', 'high', 'medium', 'low']]NoneIf provided, overrides the quality provided during initialization.
response_formatOptional[Literal['b64_json']]NoneKept for backward compatibility. The component always returns a base64-encoded image.