Edit images
Edit an image from a prompt through Stav with the OpenAI SDK — change the base URL and the key. One input image, an optional mask, always base64, nothing stored, billed only where the whole edit is priced.
Stav edits images on POST /v1/images/edits, in the same shape as OpenAI's
image edits API. You send one image and a prompt that says what to change.
Code that calls images.edit() today changes two things: the base URL and the
key.
Before you start
- A Stav API key with the
inference:imagesscope, or a key with no scopes. It is the same scope as image generation. Create one under API keys. - A model that edits images. From code, it lists
image_editsinstav_surfacesonGET /v1/models.
Edit an image
Python, OpenAI SDK
import base64, os
from openai import OpenAI
client = OpenAI(base_url="https://api.stav.ai/v1", api_key=os.environ["STAV_API_KEY"])
with open("photo.png", "rb") as image:
img = client.images.edit(
model="openai/gpt-image-1-mini",
image=image,
prompt="Put the bottle on a white marble table",
size="1024x1024",
quality="low",
)
open("edited.png", "wb").write(base64.b64decode(img.data[0].b64_json))
curl
curl https://api.stav.ai/v1/images/edits \
-H "Authorization: Bearer $STAV_API_KEY" \
-F model=openai/gpt-image-1-mini \
-F image=@photo.png \
--form-string prompt="Put the bottle on a white marble table" \
-F size=1024x1024 \
-F quality=low \
| jq -r '.data[0].b64_json' | base64 --decode > edited.png
Send the prompt with --form-string, not -F. -F reads a value that starts
with @ or < as a file name, so a prompt like that would upload a file
instead.
The request is multipart/form-data. A JSON body is refused with
400 invalid_value.
One input image
Send one image: a PNG, JPEG or WebP file. Stav reads the file itself to
check the type, not the file name. A side may be at most 16,384 pixels. A
file that is not one of the three types, or is too large on a side, is refused
with 400 invalid_image. A second image is refused with 400 invalid_value.
Masks
A mask marks which part of the image to change. It is a PNG with an alpha
channel, the same width and height as the image. Transparent pixels are
edited; opaque pixels are kept. A mask of another type or size is refused with
400 invalid_mask.
Only some models take a mask. Check stav_image_options.edits.mask on
GET /v1/models/{model_id}: true means a mask is accepted. Gemini image
models edit by prompt only and take no mask.
A mask sent to a model that takes none is refused with
400 mask_not_supported, before anything is sent or charged. Stav never drops
the mask and edits without it, because that would give you a different
picture than you asked for. Send the request without mask and say in the
prompt what to change.
Upload limits
- Each file (
imageormask) may be at most 50 MB by default. - Some models take less. Gemini image models take at most 14 MB.
stav_image_options.edits.max_upload_mbonGET /v1/models/{model_id}is the largest file a model takes.
A file over the limit is refused with 413 upload_too_large, and the message
names the limit. A body over 64 MB is stopped before it reaches Stav, with a
plain 413 page instead of a JSON error.
The answer is always base64
Stav answers with the edited image itself in data[].b64_json. When a
provider answers with a link, Stav fetches the image and puts the bytes in the
answer. response_format: "url" is refused with
400 unsupported_parameter.
usage.input_images is always 1. usage.images and usage.megapixels
describe the images that came back.
Size, quality and more than one image
size (WIDTHxHEIGHT or auto) and quality work as on
generation. The sizes and qualities a model's
edits are priced for are in stav_image_options.edits (sizes,
qualities, prices). These are edit prices: an edit is never billed at a
generation price.
Send n for several images from one edit. A model that makes one image per
edit refuses n above 1 with 400 n_not_supported.
stav_image_options.edits.max_n says which in advance.
What it costs
An edit sends an image in as well as making one, and providers price that input in different ways. Stav serves an edit only where the whole edit is priced, so you are never billed an estimate.
| Unit | What you pay |
|---|---|
| Per image | The provider's edit price, flat or by size and quality. Known before the image is made: a balance below it is refused with 402 and nothing is sent. A size or quality with no edit price is refused with 400 image_size_not_priced. |
| Tokens | The text input, image input and image output tokens the provider reports, each at its catalogue price. |
A model whose only price is per megapixel or per request, or per image with no edit price, cannot edit images on Stav yet.
usage.cost in the answer, and the X-Stav-Cost header, are what your wallet
is charged, in your workspace's currency. A refused or failed edit is not
charged, and neither is a content refusal. See
Credits and billing.
Where it runs
The same policy as chat applies: the countries your workspace allows, zero
data retention, no training, the sovereignty floor and the providers you
block. The image goes only to the Provider Endpoint that serves the request.
To narrow one request further, send the provider field as JSON in one form
field, for example provider={"runs_in":"eu"}, or the X-Stav-Runs-In
header.
Your images
Stav never stores the input image, the mask or the result. The request log records how many images went in and came out, their size in pixels, the quality and whether a mask was sent. It never records an image or the prompt. Inside a capture window you opened, the form fields are kept, and each image is replaced by its size and format.
Content
Stav adds no content filter of its own. When a provider refuses an edit under
its content policy, you get 400 content_policy_violation with the provider's
reason, and you are not charged.
Errors
| Error | What to do |
|---|---|
400 invalid_image | Send one PNG, JPEG or WebP file as image. Request validation |
400 invalid_mask | Send a PNG with transparency, the same size as the image. Request validation |
400 mask_not_supported | Send the request without mask. Request validation |
400 image_size_not_priced | Send a size and quality the message names. Request validation |
400 n_not_supported | Send n=1 and repeat the call. Request validation |
402 | Buy credits, or raise the spend limit. Billing and spend |
404 model_not_found | The model does not edit images on Stav. Pick one that lists image_edits. Request validation |
413 upload_too_large | Send a smaller file. Request validation |
501 image_edits_not_supported | The model's Inference Provider has no edits call. Pick another model. Request validation |
502 upstream_invalid_answer | No Provider Endpoint gave a whole answer. Try once more. Request validation |
Not in this version
- More than one input image.
- An image link as input: upload the file.
- Image variations.
autoand Routers: name the model.- Streaming.
The full reference, with every field and error: Images API.