Google Gemini Image Generation
Official Documentation
Introduction
Given a text prompt, the model will generate new images. Google Gemini provides powerful image generation models that can create images based on natural language descriptions.
Supported Models
Currently supported models include:
| Model | Description |
|---|---|
| gemini-2.5-flash-image | Google Gemini image generation model that supports generating high-quality images based on text prompts |
| gemini-3.1-flash-image-preview | Google Gemini fast image generation model (preview) |
| gemini-3-pro-image-preview | Google Gemini image generation and editing model that supports generating images based on text prompts, as well as editing images based on input images and text prompts |
| imagen-4.0-fast-generate-001 | Google Imagen 4.0 Fast image generation model |
Request Examples
# Basic image generation
curl "https://www.unodetech.xyz/v1beta/models/gemini-2.5-flash-image:generateContent?key=$API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts": [{"text": "Give me an image of a cat"}]
}]
}'Response Example:
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Here is an image for you: "
},
{
"inlineData": {
"mimeType": "image/png",
"data": "..."
}
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
],
"usageMetadata": {
"promptTokenCount": 9,
"candidatesTokenCount": 1298,
"totalTokenCount": 1307,
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 9
}
],
"candidatesTokensDetails": [
{
"modality": "IMAGE",
"tokenCount": 1290
}
]
},
"modelVersion": "gemini-2.5-flash-image",
"responseId": "..."
}# Generate an image and return a temporary URL
curl "https://www.unodetech.xyz/v1beta/models/gemini-2.5-flash-image:generateContent?key=$API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts": [{"text": "Give me an image of a cat"}]
}],
"generationConfig": {
"responseFormat": {
"image": {"delivery": "URI"}
}
}
}'Response Example:
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Here is an image for you: "
},
{
"fileData": {
"mimeType": "image/png",
"fileUri": "https://cdn.example.com/generated-images/...?..."
}
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
]
}# Image editing
curl "https://www.unodetech.xyz/v1beta/models/gemini-3-pro-image-preview:generateContent?key=$API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts": [
{"text": "put the chips in the given image on a beach."},
{
"inline_data": {
"mime_type": "image/jpeg",
"data": "$IMG_BASE64"
}
}
]
}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": {
"aspectRatio": "1:1",
"imageSize": "2K"
}
}
}'Response Example:
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Here is the edited image for you: "
},
{
"inlineData": {
"mimeType": "image/png",
"data": "..."
}
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
],
"usageMetadata": {
"promptTokenCount": 15,
"candidatesTokenCount": 1350,
"totalTokenCount": 1365,
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 15
},
{
"modality": "IMAGE",
"tokenCount": 1200
}
],
"candidatesTokensDetails": [
{
"modality": "IMAGE",
"tokenCount": 1350
}
]
},
"modelVersion": "gemini-3-pro-image-preview",
"responseId": "..."
}Request
Endpoint
Create Image
POST /v1beta/models/gemini-2.5-flash-image:generateContentCreate an image based on a text prompt.
Edit Image
POST /v1beta/models/gemini-3-pro-image-preview:generateContentEdit or generate new images based on input images and text prompts. Supports gemini-2.5-flash-image and gemini-3-pro-image-preview models.
Authentication Method
Include the API key in the request URL parameters:
?key=$API_KEYWhere $API_KEY is your API key.
Request Body Parameters
Response
Success Response
Image Object Example
With "delivery": "URI":
{
"fileData": {
"mimeType": "image/png",
"fileUri": "https://cdn.example.com/generated-images/...?..."
}
}Without URL delivery, the image may be returned as Base64:
{
"inlineData": {
"mimeType": "image/png",
"data": "..."
}
}Image URL Delivery
URL delivery applies to non-streaming generateContent requests. Depending on the model, image data will either be returned in candidates.content.parts in an "inlineData" object or directly as Markdown in a "text" object.
Temporary URLs expire. Download or transfer the image soon after receiving it.
URL Delivery Errors
| HTTP status | Error code | Recommended action |
|---|---|---|
400 | Invalid request | Use a non-streaming generateContent request and valid responseFormat JSON. Only uppercase "URI" activates URL delivery. |
502 | image_url_delivery_failed | Storage or URL signing failed in strict mode; retry later. |
502 | image_response_too_large | Reduce the image count or dimensions. |
503 | image_url_delivery_unavailable | URL delivery for this API key or traffic. |
503 | Other image_delivery_* errors | Retry with backoff when capacity or runtime state recovers. |
Best Practices
Prompt Writing Suggestions
- Use clear and specific descriptions
- Specify important visual details
- Describe the desired artistic style and atmosphere
- Pay attention to composition and perspective descriptions
- Can include details such as color, lighting, mood, etc.
Parameter Selection Suggestions
-
Model Selection
- gemini-2.5-flash-image: Suitable for quickly generating high-quality images
- gemini-3-pro-image-preview: Supports image generation and editing, suitable for scenarios requiring editing based on existing images
-
Prompt Optimization
- Use detailed and descriptive text
- Include specific visual elements and style requirements
- Avoid vague or overly brief descriptions
- When editing images, clearly describe how you want to modify the input image (add, remove, replace elements, etc.)
-
Safety Settings
- Adjust safety thresholds according to application scenarios
Common Issues
-
Image Generation Failure
- Check if the prompt complies with content policies
- Verify API key permissions
- Confirm the request format is correct
-
Results Don't Match Expectations
- Optimize prompt descriptions to be more specific and detailed
- Add more visual details and style descriptions
- Try different description approaches
-
Safety Filter Issues
- Modify the prompt to avoid triggering safety filters
Last updated on
