Image Generation

View as Markdown

Image Generation

1from meshapi import ImageGenerationParams
2
3result = client.images.generate(
4 ImageGenerationParams(
5 model="openai/gpt-image-1",
6 prompt="A watercolor of a fox in a snowy forest",
7 n=1,
8 size="1024x1024",
9 quality="high",
10 output_format="webp",
11 )
12)
13
14print(result.data[0].url)

Where the image lands depends on the model and response_format. With the default (or response_format="url"), some models — including openai/gpt-image-1 — return the image inline as a data: URI in url rather than in b64_json. Use result.data[0].image_bytes() to get raw bytes regardless of shape (it decodes b64_json or a data: URL), or pass response_format="b64_json" for predictable base64. A remote http(s) url raises — fetch it yourself.

1with open("out.png", "wb") as f:
2 f.write(result.data[0].image_bytes())

Streaming

1for chunk in client.images.stream(
2 ImageGenerationParams(
3 model="openai/gpt-image-1",
4 prompt="A watercolor of a fox in a snowy forest",
5 n=1,
6 size="1024x1024",
7 quality="high",
8 output_format="webp",
9 )
10):
11 if chunk.status == "processing":
12 print("Generating...")
13 elif chunk.data:
14 print("Done:", chunk.data[0].url)

Async

1from meshapi import AsyncMeshAPI, ImageGenerationParams
2
3async_client = AsyncMeshAPI()
4
5async for chunk in async_client.images.stream(
6 ImageGenerationParams(
7 model="openai/gpt-image-1",
8 prompt="A watercolor of a fox in a snowy forest",
9 n=1,
10 size="1024x1024",
11 quality="high",
12 output_format="webp",
13 )
14):
15 if chunk.status == "processing":
16 print("Generating...")
17 elif chunk.data:
18 print("Done:", chunk.data[0].url)

Parameters

FieldTypeNotes
promptstrRequired
modelstr | Nonee.g. "openai/gpt-image-1"
nint | NoneNumber of images (1–10)
sizestr | None"auto", "1024x1024", "WIDTHxHEIGHT"
qualitystr | None"auto", "low", "medium", "high", "hd", "standard"
response_format"url" | "b64_json" | None
output_format"png" | "jpeg" | "webp" | NoneOpenAI only
streambool | NoneEnable SSE keep-alive streaming