Chat Image Generation
Some models (such as gpt-4o, gpt-4o-image) support generating images directly within chat conversations. The model automatically determines whether to generate an image based on the conversation context and returns the image in its response.
Chat image generation is different from the standalone Images API (/v1/images/generations). Here, the model generates images through chat conversation and can return both text and images simultaneously.
Basic Usage
curl https://crazyrouter.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Draw a cat running under moonlight in cyberpunk style"
}
],
"max_tokens": 4096
}'
When the model generates an image, the content field returns multipart content containing the image:
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1709123456,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": [
{
"type": "text",
"text": "Here is the cyberpunk-style moonlight cat I created for you:"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..."
}
}
]
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 1024,
"total_tokens": 1044
}
}
Image Editing
You can also send an image and ask the model to modify it:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Convert this image to watercolor painting style"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/photo.jpg"}
}
]
}
],
max_tokens=4096
)
Image generation consumes a significant number of tokens. Generated images are returned in Base64 format, so be aware that response sizes can be large.