Implementation:Openai Openai python Images Generate
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Image_Generation |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for generating images from text prompts with streaming support provided by the OpenAI Python SDK.
Description
The Images resource provides a generate() method that creates images from text descriptions. It supports DALL-E 2, DALL-E 3, and GPT-Image-1 models with configurable sizes, quality, formats, and streaming partial image previews.
Usage
Call client.images.generate() with a prompt and model. Access results via response.data[0].url or response.data[0].b64_json.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/resources/images.py
- Lines: L858-914 (sync impl), L1748-1804 (async impl)
Signature
class Images(SyncAPIResource):
def generate(
self,
*,
prompt: str,
model: Union[str, ImageModel, None] | NotGiven = NOT_GIVEN,
n: Optional[int] | NotGiven = NOT_GIVEN,
quality: Literal["standard", "hd", "low", "medium", "high", "auto"] | NotGiven = NOT_GIVEN,
response_format: Optional[Literal["url", "b64_json"]] | NotGiven = NOT_GIVEN,
size: Optional[Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]] | NotGiven = NOT_GIVEN,
style: Optional[Literal["vivid", "natural"]] | NotGiven = NOT_GIVEN,
background: Optional[Literal["transparent", "opaque", "auto"]] | NotGiven = NOT_GIVEN,
output_format: Optional[Literal["png", "jpeg", "webp"]] | NotGiven = NOT_GIVEN,
output_compression: Optional[int] | NotGiven = NOT_GIVEN,
partial_images: Optional[int] | NotGiven = NOT_GIVEN,
stream: Optional[Literal[False]] | Literal[True] | NotGiven = NOT_GIVEN,
moderation: Optional[Literal["low", "auto"]] | NotGiven = NOT_GIVEN,
user: str | NotGiven = NOT_GIVEN,
) -> ImagesResponse | Stream[ImageGenStreamEvent]:
"""
Creates an image given a prompt.
"""
Import
from openai import OpenAI
# Access via client.images.generate()
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| prompt | str | Yes | Image description |
| model | str | No | dall-e-2, dall-e-3, or gpt-image-1 |
| n | int | No | Number of images (1-10) |
| size | str | No | Dimensions (e.g., "1024x1024") |
| quality | str | No | Quality level |
| response_format | str | No | "url" or "b64_json" |
| stream | bool | No | Enable streaming partial images |
| background | str | No | transparent, opaque, auto |
Outputs
| Name | Type | Description |
|---|---|---|
| response | ImagesResponse | Response with .data list of Image objects |
| stream | Stream[ImageGenStreamEvent] | Streaming partial image events |
Usage Examples
Basic Generation
from openai import OpenAI
client = OpenAI()
response = client.images.generate(
model="dall-e-3",
prompt="A white cat sitting on a windowsill at sunset",
size="1024x1024",
quality="hd",
n=1,
)
image_url = response.data[0].url
print(image_url)
Base64 Response
import base64
response = client.images.generate(
model="gpt-image-1",
prompt="A futuristic city skyline",
response_format="b64_json",
output_format="png",
)
image_bytes = base64.b64decode(response.data[0].b64_json)
with open("city.png", "wb") as f:
f.write(image_bytes)
Streaming with Partial Previews
stream = client.images.generate(
model="gpt-image-1",
prompt="A detailed landscape painting",
stream=True,
partial_images=3,
)
for event in stream:
if hasattr(event, "data") and event.data:
print(f"Partial image received ({len(event.data[0].b64_json)} chars)")
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment