Skip to content

Show live previews in your own app

Pro

You’ll get: the same live preview Image Lab shows, in your own app. A local image takes 25 to 100 seconds. With previews, people watch it form step by step instead of staring at a spinner.

You need:

Add two fields to the request:

  • "stream": true sends the answer as events.
  • "partial_images": 4 asks for 4 previews. Nexus spreads them over the steps.
curl -N http://127.0.0.1:3939/v1/images/generations \
-H "Authorization: Bearer $NEXUS_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "images/flux-3-image-klein-q4", "prompt": "A small cactus in a clay pot", "size": "512x512", "stream": true, "partial_images": 4}'

Server-sent events, in OpenAI’s image stream shape:

event: image_generation.partial_image
data: {"type":"image_generation.partial_image","b64_json":"…","partial_image_index":0,"step":2,"steps":8,"size":"512x512","output_format":"png", …}
event: image_generation.completed
data: {"type":"image_generation.completed","b64_json":"…","seed":"1196736256","steps":8,"size":"512x512","output_format":"png", …}
  • Each partial_image has a small, soft preview in b64_json, and the step it was taken at out of steps.
  • The completed event has the full image and its seed.
  • For a change (/v1/images/edits), the events are image_edit.partial_image and image_edit.completed.
  • A failure after the stream starts arrives as an error event.
import base64, os
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:3939/v1", api_key=os.environ["NEXUS_API_KEY"])
stream = client.images.generate(
model="images/flux-3-image-klein-q4",
prompt="A small cactus in a clay pot",
size="512x512",
stream=True,
partial_images=4,
)
for event in stream:
if event.type == "image_generation.partial_image":
print(f"Step {event.step} of {event.steps}")
with open(f"preview-{event.partial_image_index}.png", "wb") as file:
file.write(base64.b64decode(event.b64_json))
elif event.type == "image_generation.completed":
with open("image.png", "wb") as file:
file.write(base64.b64decode(event.b64_json))

In your app, draw each preview over the last one, and show “Step 2 of 8” from step and steps.

The script prints Step 2 of 8, Step 4 of 8, Step 6 of 8, Step 8 of 8, and writes four previews and image.png.

  • Previews are small and blurry on purpose. Blur them a little more in your app and they look like Image Lab’s.
  • Ask for up to one preview per step. Image Lab asks for one at every step.
  • To also save the result in the Gallery, send X-Msty-Nexus-Image-Gallery: save. The stream then ends with a msty_nexus.image_saved event.