For Gemini-native image generation and editing with Nano Banana models, see the Nano Banana API overview. To compare image models and endpoints, see Choose an image API.
Set up your credentials
Create a model API key in Token management and make it available as theLAOZHANG_API_KEY environment variable. Check that its group has access to gpt-image-2-vip in Models and pricing.
Set the key in your terminal, replacing the placeholder:
- API host:
https://api.laozhang.ai - OpenAI SDK base URL:
https://api.laozhang.ai/v1 - Authentication:
Authorization: Bearer $LAOZHANG_API_KEY - Model:
gpt-image-2-vip
Save images from a response
Save this helper assave_images.py in the directory where you run the examples. It writes every image in data[] to disk, whether the entry holds b64_json or a url:
python save_images.py <response.json> <name> after a cURL request, or import save_images in Python.
Generate an image
Send a JSON body containingmodel and prompt to:
generated.png, generated.jpg, or generated.webp, according to the returned file format.
- cURL
- Python
generation-response.json:.json file contains the API response; changing its extension to .png does not convert it into an image.
Edit an image
Place an image namedinput.png in the directory where you run the command. Send the file and your instructions as multipart form data to:
edited.png, edited.jpg, or edited.webp.
- cURL
- Python
edit-response.json:image[]=@input.png uploads the file contents. A plain path string in JSON does not upload a local file. When using cURL’s -F option, let cURL set the multipart content type and boundary; do not add Content-Type: application/json.
Describe both the change you want and the details to preserve. Image editing is generative, so an instruction to keep the subject unchanged does not guarantee identical pixels outside the edited area.
Request options
The minimal examples use the model’s default output settings. To choose dimensions or quality, use the options listed for the exact model in the GPT Image 2 guide or GPT Image 2.5 guide.
GPT Image and DALL·E use different options. Do not copy DALL·E’s
quality="hd" or style into a GPT Image request. Masks, multiple reference images, transparent backgrounds, and output-format controls also require model-specific instructions; use the basic file upload above when editing a single local image.
Read image results
Each entry indata[] is one output image, returned in one of two forms:
b64_jsonholds the Base64-encoded image bytes. Some responses add adata:image/...;base64,prefix or drop the trailing=padding, so strip the prefix and restore the padding before decoding, assave_images.pydoes.urlis a download link. Fetch it promptly without your LaoZhang API key and keep your own copy; URL results may expire.
data[]. An image-generation task ID or a Gemini candidates response uses a different format and needs the handler for that API.
Errors and retries
- 400: check the request fields and model options. For edits, confirm that the file was uploaded as multipart data.
- 401 or 403: check the API key, token group, and model permissions.
- 404 or an unavailable-model error: check the endpoint and exact model ID in Models and pricing.
- 429: read the error body to distinguish rate or concurrency limits from account balance issues.
- 5xx or timeout: inspect call logs before submitting the image request again.