Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai python Image Gen Completed Event

From Leeroopedia
Revision as of 13:38, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Openai_Openai_python_Image_Gen_Completed_Event.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains API_Types, Python
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for representing an image generation completion streaming event provided by the openai-python SDK.

Description

ImageGenCompletedEvent is a Pydantic BaseModel emitted when image generation has completed and the final image is available. It contains b64_json base64-encoded image data along with generation settings: background (transparent, opaque, or auto), output_format (png, webp, or jpeg), quality (low, medium, high, or auto), and size. The type field is always "image_generation.completed". A usage field provides token consumption details via nested Usage and UsageInputTokensDetails models, specifically for GPT image models.

Usage

Import ImageGenCompletedEvent when processing streaming events from the image generation API to detect when the final generated image is ready.

Code Reference

Source Location

Signature

class UsageInputTokensDetails(BaseModel):
    image_tokens: int
    text_tokens: int

class Usage(BaseModel):
    input_tokens: int
    input_tokens_details: UsageInputTokensDetails
    output_tokens: int
    total_tokens: int

class ImageGenCompletedEvent(BaseModel):
    b64_json: str
    background: Literal["transparent", "opaque", "auto"]
    created_at: int
    output_format: Literal["png", "webp", "jpeg"]
    quality: Literal["low", "medium", "high", "auto"]
    size: Literal["1024x1024", "1024x1536", "1536x1024", "auto"]
    type: Literal["image_generation.completed"]
    usage: Usage

Import

from openai.types import ImageGenCompletedEvent

I/O Contract

Fields (ImageGenCompletedEvent)

Name Type Required Description
b64_json str Yes Base64-encoded image data, suitable for rendering as an image.
background Literal["transparent", "opaque", "auto"] Yes The background setting for the generated image.
created_at int Yes Unix timestamp when the event was created.
output_format Literal["png", "webp", "jpeg"] Yes The output format for the generated image.
quality Literal["low", "medium", "high", "auto"] Yes The quality setting for the generated image.
size Literal["1024x1024", "1024x1536", "1536x1024", "auto"] Yes The size of the generated image.
type Literal["image_generation.completed"] Yes The event type. Always "image_generation.completed".
usage Usage Yes Token usage information for the image generation (GPT image models only).

Fields (Usage)

Name Type Required Description
input_tokens int Yes Number of tokens (images and text) in the input prompt.
input_tokens_details UsageInputTokensDetails Yes Detailed breakdown of input tokens.
output_tokens int Yes Number of image tokens in the output image.
total_tokens int Yes Total number of tokens used for the image generation.

Fields (UsageInputTokensDetails)

Name Type Required Description
image_tokens int Yes Number of image tokens in the input prompt.
text_tokens int Yes Number of text tokens in the input prompt.

Usage Examples

from openai.types import ImageGenCompletedEvent
import base64

# When processing streaming events from image generation
def handle_event(event):
    if isinstance(event, ImageGenCompletedEvent):
        # Decode the final generated image
        image_data = base64.b64decode(event.b64_json)
        print(f"Image size: {event.size}")
        print(f"Quality: {event.quality}")
        print(f"Background: {event.background}")
        print(f"Total tokens: {event.usage.total_tokens}")
        with open("generated.png", "wb") as f:
            f.write(image_data)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment