Skip to main content

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, offering both artistic and photorealistic image creation capabilities.

Installation

To use OpenAI image generation services, install the required dependencies:
pip install "pipecat-ai[openai]"

Prerequisites

OpenAI Account Setup

Before using OpenAI image generation services, you need:
  1. OpenAI Account: Sign up at OpenAI Platform
  2. API Key: Generate an OpenAI API key from your account dashboard
  3. Model Access: Ensure access to DALL-E models
  4. HTTP Session: Configure aiohttp session for image downloading

Required Environment Variables

  • OPENAI_API_KEY: Your OpenAI API key for authentication

Configuration

api_key
str
required
OpenAI API key for authentication.
aiohttp_session
aiohttp.ClientSession
required
HTTP session for downloading generated images. You must create and manage this yourself.
image_size
Literal['256x256', '512x512', '1024x1024', '1792x1024', '1024x1792']
required
Target size for generated images.
base_url
str
default:"None"
Custom base URL for OpenAI API. If None, uses the default OpenAI endpoint.
model
str
default:"dall-e-3"
DALL-E model to use for image generation.

Usage

Basic Setup

import aiohttp
from pipecat.services.openai import OpenAIImageGenService

async with aiohttp.ClientSession() as session:
    image_gen = OpenAIImageGenService(
        api_key=os.getenv("OPENAI_API_KEY"),
        aiohttp_session=session,
        image_size="1024x1024",
    )

With Custom Model

image_gen = OpenAIImageGenService(
    api_key=os.getenv("OPENAI_API_KEY"),
    aiohttp_session=session,
    image_size="1792x1024",
    model="dall-e-3",
)

Notes

  • HTTP session required: You must provide an aiohttp.ClientSession for downloading the generated images from OpenAI’s URLs.
  • Image sizes vary by model: DALL-E 3 supports 1024x1024, 1792x1024, and 1024x1792. DALL-E 2 supports 256x256, 512x512, and 1024x1024.