Generate Images

Code

1from kern.agent import Agent
2from kern.models.openai import OpenAIResponses
3from kern.tools.dalle import DalleTools
4
5image_agent = Agent(
6 model=OpenAIResponses(id="gpt-5.2"),
7 tools=[DalleTools()],
8 description="You are an AI agent that can generate images using DALL-E.",
9 instructions="When the user asks you to create an image, use the `create_image` tool to create the image.",
10 markdown=True,
11)
12
13image_agent.print_response("Generate an image of a white siamese cat")
14
15images = image_agent.get_images()
16if images and isinstance(images, list):
17 for image_response in images:
18 image_url = image_response.url
19 print(image_url)

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/chat/generate_images.py