Skip to main content
The source-fidelity code uses OpenAIChat and gpt-image-1 despite its Responses category. Replace them with OpenAIResponses and gpt-image-2 before running the example.
image_generation_agent.py

Run the Example

1

Set up your virtual environment

2

Install dependencies

3

Export your OpenAI API key

4

Use the Responses API

Replace the OpenAIChat import and constructor with OpenAIResponses(id="gpt-5.2").
5

Encode the image bytes

Add import base64. Encode response.images[0].content with base64.b64encode(...).decode("utf-8"), then pass that string to save_base64_data.
6

Update the image model

When saving the code, replace gpt-image-1 with gpt-image-2.
7

Run the example

Save the code above as image_generation_agent.py, then run:
Full source: cookbook/90_models/openai/responses/image_generation_agent.py