OpenAI-compatible images and speech
POST https://api.unitool.ai/v1/images/generations
POST https://api.unitool.ai/v1/images/edits
POST https://api.unitool.ai/v1/audio/speechThese paths mirror OpenAI's, so the OpenAI SDK works with https://api.unitool.ai/v1. The key needs the generations scope. Every call is an ordinary generation: the same price as POST /v1/generations, listed in GET /v1/generations, Idempotency-Key honoured, webhooks sent. The generation id is in the x-unitool-generation-id header and in unitool.generation_id of the answer (with unitool.charged_tokens).
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.unitool.ai/v1", api_key=os.environ["UNITOOL_API_KEY"])
image = client.images.generate(model="nanobanana", prompt="A coffee cup on a wooden table", size="1024x1024")
print(image.data[0].url)
edited = client.images.edit(model="nanobanana", prompt="Make the background blue", image=open("cup.png", "rb"))
speech = client.audio.speech.create(model="<speech model id>", voice="<voice id>", input="Hello!")
speech.write_to_file("hello.mp3")import OpenAI from "openai";
import fs from "node:fs";
const client = new OpenAI({ baseURL: "https://api.unitool.ai/v1", apiKey: process.env.UNITOOL_API_KEY });
const image = await client.images.generate({ model: "nanobanana", prompt: "A coffee cup on a wooden table", size: "1024x1024" });
const edited = await client.images.edit({ model: "nanobanana", prompt: "Make the background blue", image: fs.createReadStream("cup.png") });Which model answers
modelis a media model id from the catalog. A fitting mode is picked for you: for/images/generationsan image mode that needs no file, for/images/editsa mode with an image field, for/audio/speecha speech mode with a voice field.- You can pick the mode yourself:
model: "nanobanana:<mode>". It still has to fit the path. - A model with no fitting mode:
400 model_not_supported. Video models do not work here. voiceis a voice id from the options of the voice field inGET /v1/models/{id}, not an OpenAI voice name.
Parameters
| OpenAI parameter | Where it goes |
|---|---|
prompt (speech: input) | The prompt |
n | The mode's count field. Without n, one image, even if the model's own default is several. n > 1 on a mode without such a field is a 400 |
size | The mode's size field; otherwise WxH becomes aspect_ratio plus resolution (1K / 2K / 4K by pixel count). An unsupported ratio is a 400 listing the supported ones |
quality, background, output_format | Fields of the same name. auto means the model's default |
voice, speed | The voice and speed fields |
response_format | Images: url (default) or b64_json. Speech: the audio format |
user | Accepted and not used |
A parameter the mode does not have, and any other field (style, moderation, stream, instructions...), is 400 unsupported_parameter naming it: such fields are never dropped silently. A value outside the options is 400 invalid_parameter with the list. Nothing is created on a refusal. Model-specific parameters go through POST /v1/generations.
/images/edits takes multipart/form-data: image or image[], mask, text fields. The body may be up to 32 MiB. A file's type is read from its content, not from what the part declares.
Waiting and retries
The answer waits for the generation up to 120 seconds. Slower: 504 generation_timeout with the id in error.unitool.generation_id. The generation keeps running and is charged when it finishes; read it with GET /v1/generations/{id}. Repeating the call with the same Idempotency-Key waits for the same generation; without one it starts a new paid generation.
The official SDKs retry by themselves on a broken connection. Such a retry returns the generation of the first attempt and is not charged again. For your own retries send Idempotency-Key. Generation failures carry x-should-retry: false, so the SDKs do not repeat a paid call on their own.
With response_format: "b64_json" the answer carries the image bytes, 32 MiB in total. Larger: 400 response_too_large with error.unitool.generation_id. The generation is ready and paid: read it by id and use url.
Generation failures
| Answer | When |
|---|---|
400 content_policy_violation | Moderation rejected the request or the result |
400 invalid_parameter | The model did not accept the parameters |
402 insufficient_balance | The balance was short |
409 generation_canceled | The generation was canceled before it started; nothing charged |
500 internal_error, 502 upstream_error, 504 timeout | Our failure or the model's; tokens are returned |
Each of these answers has error.unitool.generation_id.