Overview

OpenAIImageGenService provides high-quality image generation capabilities using OpenAI’s DALL-E models. It transforms text prompts into images with various size options and model configurations.

Installation

No additional installation is required for the OpenAIImageGenService as it is part of the Pipecat AI package. You’ll also need an OpenAI API key for authentication.

Configuration

Constructor Parameters

api_key
str
required
OpenAI API key for authentication
base_url
str
default:"None"
Optional base URL for OpenAI API requests
aiohttp_session
aiohttp.ClientSession
required
HTTP session for making requests
image_size
str
required
Image dimensions - one of “256x256”, “512x512”, “1024x1024”, “1792x1024”, “1024x1792”
model
str
default:"dall-e-3"
OpenAI model identifier for image generation

Input

The service accepts text prompts through its image generation pipeline.

Output Frames

URLImageRawFrame

url
string
Generated image URL from OpenAI
image
bytes
Raw image data
size
tuple
Image dimensions (width, height)
format
string
Image format (e.g., ‘JPEG’)

ErrorFrame

error
string
Error information if generation fails

Usage Example

import aiohttp
from pipecat.pipeline.pipeline import Pipeline
from pipecat.services.openai.image import OpenAIImageGenService

# Create an aiohttp session
aiohttp_session = aiohttp.ClientSession()

# Configure service
image_gen = OpenAIImageGenService(
    api_key="your-openai-api-key",
    aiohttp_session=aiohttp_session,
    image_size="1024x1024",
    model="dall-e-3"
)

# Use in pipeline
main_pipeline = Pipeline(
    [
        transport.input(),
        context_aggregator.user(),
        llm_service,
        image_gen,
        tts_service,
        transport.output(),
        context_aggregator.assistant(),
    ]
)

Frame Flow

Metrics Support

The service supports metrics collection:
  • Time to First Byte (TTFB)
  • Processing duration
  • API response metrics

Model Support

OpenAI’s image generation service offers different model variants:
Model IDDescription
dall-e-3Latest DALL-E model with higher quality and better prompt following
dall-e-2Previous generation model with good quality and lower cost

Image Size Options

Size OptionAspect RatioDescription
256x2561:1Small square image
512x5121:1Medium square image
1024x10241:1Large square image
1792x102416:9Horizontal/landscape orientation
1024x17929:16Vertical/portrait orientation

Error Handling

try:
    async for frame in image_gen.run_image_gen(prompt):
        if isinstance(frame, ErrorFrame):
            logger.error(f"Image generation error: {frame.error}")
        else:
            # Process successful image generation
            pass
except Exception as e:
    logger.error(f"Unexpected error during image generation: {e}")