Image Generation Agent

Code

1"""🔧 Example: Using the OpenAITools Toolkit for Image Generation
2
3This script demonstrates how to use the `OpenAITools` toolkit, which includes a tool for generating images using OpenAI's DALL-E within an Kern Agent.
4
5Example prompts to try:
6- "Create a surreal painting of a floating city in the clouds at sunset"
7- "Generate a photorealistic image of a cozy coffee shop interior"
8- "Design a cute cartoon mascot for a tech startup"
9- "Create an artistic portrait of a cyberpunk samurai"
10
11Run `uv pip install openai kern-ai` to install the necessary dependencies.
12"""
13
14from kern.agent import Agent
15from kern.models.openai import OpenAIResponses
16from kern.tools.openai import OpenAITools
17from kern.utils.media import save_base64_data
18
19agent = Agent(
20 model=OpenAIResponses(id="gpt-5.2"),
21 tools=[OpenAITools(image_model="gpt-image-1")],
22 markdown=True,
23)
24
25response = agent.run(
26 "Generate a photorealistic image of a cozy coffee shop interior",
27)
28
29if response.images and response.images[0].content:
30 save_base64_data(str(response.images[0].content), "tmp/coffee_shop.png")

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export OPENAI_API_KEY=xxx

Install dependencies

1uv pip install -U openai kern-ai

Run Agent

1python cookbook/11_models/openai/responses/image_generation_agent.py