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:
- A local image model. Previews come from local models only. See Create your first image.
- A client API key. Make one in API keys.
Ask for previews
Section titled “Ask for previews”Add two fields to the request:
"stream": truesends the answer as events."partial_images": 4asks 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}'What comes back
Section titled “What comes back”Server-sent events, in OpenAI’s image stream shape:
event: image_generation.partial_imagedata: {"type":"image_generation.partial_image","b64_json":"…","partial_image_index":0,"step":2,"steps":8,"size":"512x512","output_format":"png", …}
event: image_generation.completeddata: {"type":"image_generation.completed","b64_json":"…","seed":"1196736256","steps":8,"size":"512x512","output_format":"png", …}- Each
partial_imagehas a small, soft preview inb64_json, and thestepit was taken at out ofsteps. - The
completedevent has the full image and itsseed. - For a change (
/v1/images/edits), the events areimage_edit.partial_imageandimage_edit.completed. - A failure after the stream starts arrives as an
errorevent.
Python
Section titled “Python”import base64, osfrom 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.
Check it worked
Section titled “Check it worked”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 amsty_nexus.image_savedevent.