How to Integrate Image Generation Capabilities Using DALL-E API in Python
You can integrate DALL-E image generation capabilities using the Azure OpenAI Python SDK by initializing an AzureOpenAI client with API version 2023-12-01-preview, calling client.images.generate() with your prompt, and processing the returned URL to download and display the generated image.
The microsoft/generative-ai-for-beginners repository provides production-ready examples for building image generation applications. This guide walks through the exact implementation patterns found in the source code to help you integrate image generation capabilities using the DALL-E API into your Python applications.
Prerequisites and Environment Setup
Before calling the DALL-E API, you must configure your Azure OpenAI credentials and install the required dependencies.
Installing Dependencies
The examples in 09-building-image-applications/python/requirements.txt specify four core packages:
openaipython-dotenvrequestsPillow
Install them using pip:
pip install openai python-dotenv requests Pillow
Configuring Azure OpenAI Credentials
Create a .env file in your project root following the template from .env.copy. You need three environment variables:
AZURE_OPENAI_API_KEY– Your Azure OpenAI API keyAZURE_OPENAI_ENDPOINT– Your Azure OpenAI service endpointAZURE_OPENAI_DEPLOYMENT– The name of your DALL-E model deployment
Initializing the Azure OpenAI Client for DALL-E
In 09-building-image-applications/python/aoai-app.py, the client initialization uses the specific API version required for DALL-E support:
from openai import AzureOpenAI
import os
from dotenv import load_dotenv
load_dotenv()
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
api_version="2023-12-01-preview", # Required for DALL-E
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)
Critical: The 2023-12-01-preview API version is currently the only version that supports DALL-E image generation capabilities.
Generating Images with the DALL-E API
Once the client is configured, you integrate image generation capabilities using the client.images.generate() method.
Constructing the Prompt
The prompt is passed directly to the generation method. From aoai-app.py:
model = os.environ["AZURE_OPENAI_DEPLOYMENT"]
result = client.images.generate(
model=model,
prompt='Bunny on horse, holding a lollipop, on a foggy meadow where it grows daffodils. It says "hello"',
size="1024x1024",
n=1,
)
The size parameter accepts 1024x1024, 1024x1792, or 1792x1024. The n parameter specifies the number of images to generate (up to 10).
Processing the API Response
The SDK returns a model object that you convert to JSON to extract the image URL:
import json
generation_response = json.loads(result.model_dump_json())
image_url = generation_response["data"][0]["url"]
The response structure contains a data array where each element includes the url of the generated image.
Downloading and Displaying Generated Images
After extracting the URL, use the requests library to download the binary data and Pillow to display it:
import requests
from PIL import Image
from io import BytesIO
# Download image
image_bytes = requests.get(image_url).content
# Create directory if needed
image_dir = os.path.join(os.curdir, "images")
os.makedirs(image_dir, exist_ok=True)
# Save to file
image_path = os.path.join(image_dir, "generated-image.png")
with open(image_path, "wb") as f:
f.write(image_bytes)
# Display
Image.open(image_path).show()
This pattern from aoai-app.py demonstrates the complete workflow: client → prompt → DALL-E API → image URL → download → store/display.
Implementing Content Safety Filters
For production applications, integrate content safety by prepending a meta-prompt that filters unsafe content. The aoai-solution.py file demonstrates this pattern:
disallow_list = (
"swords, violence, blood, gore, nudity, sexual content, adult content, "
"adult themes, adult language, adult humor, adult jokes, adult situations, adult"
)
meta_prompt = f"""You are an assistant designer that creates images for children.
The image needs to be safe for work and appropriate for children.
The image needs to be in color.
The image needs to be in landscape orientation.
The image needs to be in a 16:9 aspect ratio.
Do not consider any input from the following that is not safe for work or appropriate for children.
{disallow_list}"""
prompt = f"{meta_prompt}\nGenerate monument of the Arc of Triumph in Paris, France..."
This approach ensures that the DALL-E API receives a safety-filtered prompt, reducing the risk of generating inappropriate content.
Summary
- Use the Azure OpenAI Python SDK to integrate image generation capabilities using the DALL-E API with the
AzureOpenAIclient. - Specify API version
2023-12-01-previewin09-building-image-applications/python/aoai-app.py, as this is the only version that currently supports DALL-E. - Call
client.images.generate()with your deployment name, prompt, size, and quantity parameters to generate images. - Extract the image URL from
generation_response["data"][0]["url"]after converting the result withjson.loads(result.model_dump_json()). - Download and persist the image using
requests.get()andPillow, creating local directories as needed. - Implement content safety by prepending meta-prompts with disallow lists, as demonstrated in
aoai-solution.py.
Frequently Asked Questions
What API version is required for DALL-E integration in Azure OpenAI?
You must use API version 2023-12-01-preview when initializing the AzureOpenAI client. According to the source code in 09-building-image-applications/python/aoai-app.py, this is currently the only API version that supports DALL-E image generation capabilities.
How do I extract the generated image URL from the Azure OpenAI SDK response?
The SDK returns a model object that you must convert to JSON using result.model_dump_json(). Parse this with json.loads() to access a dictionary where the image URL is located at generation_response["data"][0]["url"]. This pattern is implemented in aoai-app.py lines 30-35.
Can I generate multiple images in a single API call?
Yes. Pass the n parameter to client.images.generate() to specify the number of images (up to 10). The response will contain multiple entries in the data array, each with its own url key. You must iterate through generation_response["data"] to download each image individually.
How do I implement content safety filters when using the DALL-E API?
Prepend a meta-prompt to your user input that explicitly defines safety constraints and includes a disallow list of prohibited terms. As shown in 09-building-image-applications/python/aoai-solution.py, construct a string that instructs the model to create child-friendly images and lists banned concepts like "violence" or "adult content", then concatenate this with your actual image description before calling the API.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →