Image Generation with Azure OpenAI
This skill generates images using Azure OpenAI GPT Image models through direct API calls.
When to use
- User asks to generate, create, or produce an image
- User wants to visualize a concept or idea
- User requests artwork, illustrations, or graphics
- User asks to create visual representations of descriptions
Prerequisites
User must have:
- Azure OpenAI resource with deployed GPT Image model (
gpt-image-2orgpt-image-1.5) - Azure authentication configured:
- Recommended: Azure CLI (
az login) with "Cognitive Services OpenAI User" role - Alternative: API key (if enabled on the endpoint)
- Recommended: Azure CLI (
Important: Many Azure OpenAI endpoints disable API key authentication. Always use Azure RBAC when API keys are disabled.
How to use
Using Python
Install required packages:
pip install openai azure-identity
Generate image with Azure RBAC (recommended):
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
api_version="2024-02-01",
azure_endpoint="https://YOUR-RESOURCE.openai.azure.com/",
azure_ad_token_provider=token_provider
)
result = client.images.generate(
model="gpt-image-2",
prompt="A serene mountain landscape at sunset",
size="1536x1024",
quality="medium"
)
print(result.data[0].url)
Parameters
prompt: Description of the image (1-4000 chars, required)model:gpt-image-2orgpt-image-1.5(use deployment name)size: Use model-supported dimensions such as1024x1024,1024x1536, or1536x1024(default:1024x1024)quality:low,medium,high, orautofor GPT Image models (usemediumby default)n: Number of images (model-specific limits apply)style: Optional stylistic control if supported by the deployed model
Prompt crafting tips
- Be specific and descriptive
- Include style, mood, and atmosphere details
- Specify key visual elements and composition
- Use portrait (
1024x1536) for vertical, landscape (1536x1024) for horizontal images - Use
highquality only when the user specifically requests extra detail; uselowfor faster or lower-cost drafts
Output
- The API returns a temporary URL valid for 24 hours
- Download and save the image if persistence is needed
- The response includes a
revised_promptshowing what the model actually used
Error handling
- 401/403: Check Azure authentication and RBAC role assignments
- API Key Disabled: Switch to Azure RBAC with DefaultAzureCredential
- 429: Rate limit reached, implement retry with backoff
- Content filtered: Revise prompt to comply with content policies