Image Generation
What You’ll Build
Section titled “What You’ll Build”A .prompty file that generates images from text descriptions using
DALL-E. Set apiType: image and the runtime calls the Images API instead
of Chat Completions, returning image URLs.
Step 1: Create an Image Prompt
Section titled “Step 1: Create an Image Prompt”Set apiType: image and use a DALL-E model (dall-e-3 or dall-e-2):
---name: image-generatordescription: Generate images from text descriptionsmodel: id: dall-e-3 provider: openai apiType: image connection: kind: key apiKey: ${env:OPENAI_API_KEY} options: additionalProperties: size: "1024x1024" quality: standardinputSchema: properties: - name: description kind: string default: A friendly robot painting a landscapetemplate: format: kind: jinja2 parser: kind: prompty---{{description}}Step 2: Generate an Image
Section titled “Step 2: Generate an Image”from prompty import execute
result = execute( "generate-image.prompty", inputs={"description": "A cat astronaut floating in space, digital art"},)
# result is the image URL (or list of URLs)print(result) # "https://oaidalleapiprodscus.blob.core.windows.net/..."Async variant:
from prompty import execute_async
result = await execute_async( "generate-image.prompty", inputs={"description": "A sunset over mountains, watercolor style"},)print(result)import { execute } from "prompty";
const result = await execute("generate-image.prompty", { description: "A cat astronaut floating in space, digital art",});
console.log(result); // "https://oaidalleapiprodscus.blob.core.windows.net/..."Model Options
Section titled “Model Options”Control image output through options.additionalProperties:
---name: custom-imagemodel: id: dall-e-3 provider: openai apiType: image connection: kind: key apiKey: ${env:OPENAI_API_KEY} options: additionalProperties: size: "1792x1024" quality: hd style: naturalinputSchema: properties: - name: description kind: stringtemplate: format: kind: jinja2 parser: kind: prompty---{{description}}Available Options
Section titled “Available Options”| Option | Values | Default | Notes |
|---|---|---|---|
size | 1024x1024, 1792x1024, 1024x1792 | 1024x1024 | DALL-E 3 sizes |
quality | standard, hd | standard | hd costs more tokens |
style | vivid, natural | vivid | DALL-E 3 only |
n | 1 | 1 | DALL-E 3 only supports n=1 |
DALL-E 2 Options
Section titled “DALL-E 2 Options”DALL-E 2 supports different sizes and multiple images per request:
model: id: dall-e-2 provider: openai apiType: image connection: kind: key apiKey: ${env:OPENAI_API_KEY} options: additionalProperties: size: "256x256" n: 3| Option | Values | Notes |
|---|---|---|
size | 256x256, 512x512, 1024x1024 | Smaller = cheaper |
n | 1–10 | Number of images to generate |
Downloading the Image
Section titled “Downloading the Image”The result is a URL that expires after a period. To save the image locally:
import urllib.requestfrom prompty import execute
url = execute( "generate-image.prompty", inputs={"description": "A cozy cabin in the woods"},)
# Download and saveurllib.request.urlretrieve(url, "cabin.png")print("Saved to cabin.png")import { execute } from "prompty";import { writeFile } from "fs/promises";
const url = await execute("generate-image.prompty", { description: "A cozy cabin in the woods",});
// Download and saveconst response = await fetch(url as string);const buffer = Buffer.from(await response.arrayBuffer());await writeFile("cabin.png", buffer);console.log("Saved to cabin.png");Provider Support
Section titled “Provider Support”Microsoft Foundry (if available)
Section titled “Microsoft Foundry (if available)”If your Foundry resource has a DALL-E deployment, use the Foundry provider:
model: id: ${env:AZURE_OPENAI_IMAGE_DEPLOYMENT} provider: foundry apiType: image connection: kind: key endpoint: ${env:AZURE_AI_PROJECT_ENDPOINT} apiKey: ${env:AZURE_AI_PROJECT_KEY}Complete Example: Image Gallery Generator
Section titled “Complete Example: Image Gallery Generator”---name: gallerydescription: Generate themed images for a gallerymodel: id: dall-e-3 provider: openai apiType: image connection: kind: key apiKey: ${env:OPENAI_API_KEY} options: additionalProperties: size: "1024x1024" quality: standardinputSchema: properties: - name: subject kind: string - name: style kind: string default: digital arttemplate: format: kind: jinja2 parser: kind: prompty---{{subject}}, {{style}}, high detail, professional qualityfrom prompty import executeimport urllib.request
subjects = [ "A mountain lake at sunrise", "A bustling Tokyo street at night", "An ancient library filled with magical books",]
for i, subject in enumerate(subjects): url = execute("gallery.prompty", inputs={"subject": subject, "style": "oil painting"}) urllib.request.urlretrieve(url, f"gallery_{i}.png") print(f"Generated: gallery_{i}.png")Further Reading
Section titled “Further Reading”- File format reference — full
.promptyfrontmatter syntax - Connections — configuring provider connections