Image Generation
Generate images using AI models through the Agnic AI Gateway
Image Generation
Generate images from text prompts using AI models with image output capabilities. Agnic supports various image generation models that can create high-quality images based on your descriptions.
Overview
Image generation is available through models that have "image" in their output_modalities. These models can create images from text prompts when you specify the appropriate modalities in your request.
Basic Image Generation
Python
from openai import OpenAI
client = OpenAI(
api_key="agnic_tok_YOUR_TOKEN",
base_url="https://api.agnic.ai/v1"
)
response = client.chat.completions.create(
model="google/gemini-2.0-flash",
messages=[
{
"role": "user",
"content": "Generate a beautiful sunset over mountains"
}
],
extra_body={
"modalities": ["image", "text"]
}
)
# The generated image will be in the response
message = response.choices[0].message
if hasattr(message, 'images') and message.images:
for image in message.images:
image_url = image["image_url"]["url"] # Base64 data URL
print(f"Generated image: {image_url[:50]}...")JavaScript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'agnic_tok_YOUR_TOKEN',
baseURL: 'https://api.agnic.ai/v1'
});
const response = await client.chat.completions.create({
model: 'google/gemini-2.0-flash',
messages: [
{
role: 'user',
content: 'Generate a beautiful sunset over mountains'
}
],
modalities: ['image', 'text']
});
// The generated image will be in the response
const message = response.choices[0].message;
if (message.images) {
message.images.forEach((image, index) => {
const imageUrl = image.image_url.url; // Base64 data URL
console.log(`Generated image ${index + 1}: ${imageUrl.substring(0, 50)}...`);
});
}cURL
curl https://api.agnic.ai/v1/chat/completions \
-H "Authorization: Bearer agnic_tok_YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemini-2.0-flash",
"messages": [
{
"role": "user",
"content": "Generate a beautiful sunset over mountains"
}
],
"modalities": ["image", "text"]
}'Image Configuration Options
Some models support additional configuration through the image_config parameter.
Aspect Ratio
Set image_config.aspect_ratio to request specific aspect ratios:
| Ratio | Resolution | Use Case |
|---|---|---|
1:1 | 1024×1024 | Social media, avatars |
16:9 | 1344×768 | Widescreen, presentations |
9:16 | 768×1344 | Mobile, stories |
4:3 | 1184×864 | Standard photos |
3:2 | 1248×832 | Classic photography |
Example with Configuration
response = client.chat.completions.create(
model="google/gemini-2.0-flash",
messages=[
{
"role": "user",
"content": "Create a panoramic landscape of a futuristic city"
}
],
extra_body={
"modalities": ["image", "text"],
"image_config": {
"aspect_ratio": "16:9"
}
}
)Response Format
Generated images are returned in the assistant message:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "I've generated a beautiful sunset image for you.",
"images": [
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
}
]
}
}
]
}Image Format Details
- Format: Images are returned as base64-encoded data URLs
- Type: Typically PNG format (
data:image/png;base64,) - Multiple Images: Some models can generate multiple images
- Size: Dimensions vary by model and configuration
Saving Generated Images
Python
import base64
# Extract base64 data from response
image_data = response.choices[0].message.images[0]["image_url"]["url"]
# Remove the data URL prefix
base64_data = image_data.split(",")[1]
# Decode and save
with open("generated_image.png", "wb") as f:
f.write(base64.b64decode(base64_data))JavaScript
import fs from 'fs';
const imageData = response.choices[0].message.images[0].image_url.url;
const base64Data = imageData.split(',')[1];
fs.writeFileSync('generated_image.png', Buffer.from(base64Data, 'base64'));Compatible Models
Models with image generation capabilities:
| Model | Provider | Features |
|---|---|---|
google/gemini-2.0-flash | Fast generation, configurable |
Check the model's architecture.output_modalities for "image" support. Visit AI Gateway Pricing for the full list.
Best Practices
- Clear prompts - Provide detailed descriptions for better results
- Specify style - Include artistic style, mood, lighting preferences
- Set aspect ratio - Match the ratio to your use case
- Error handling - Check for the
imagesfield before processing
Troubleshooting
No images in response?
- Verify the model supports image generation
- Ensure
modalities: ["image", "text"]is in your request - Check that your prompt requests image generation
Wrong dimensions?
- Use the
image_config.aspect_ratioparameter - Check model-specific documentation for supported ratios