Working Python snippets for GPT Image 2.6: single calls, polling, batch with retry.
👉 Try GPT Image 2.6 on APIMart
https://gola.apimart.ai/k-QUaoXJ
pip install openaiNo APIMart-specific SDK is required — the endpoint is OpenAI-compatible.
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.apimart.ai/v1",
api_key=os.environ["APIMART_API_KEY"])
r = client.images.generate(model="gpt-image-2.6-flare",
prompt="minimal product shot of a ceramic mug on oak",
n=1)
print(r.data[0].url)import time, requests
def wait(task_id, key, timeout=300):
url = f"https://api.apimart.ai/v1/tasks/{task_id}"
for _ in range(timeout // 5):
d = requests.get(url, headers={"Authorization": f"Bearer {key}"}).json()["data"]
if d["status"] in ("completed", "succeeded"):
return d["result"]["images"][0]["url"], d.get("cost")
if d["status"] in ("failed", "error"):
raise RuntimeError(d.get("error"))
time.sleep(5)
raise TimeoutError(task_id)from tenacity import retry, wait_exponential, stop_after_attempt
@retry(wait=wait_exponential(min=2, max=30), stop=stop_after_attempt(4))
def generate(prompt):
return client.images.generate(model="gpt-image-2.6-sunburst",
prompt=prompt, n=1).data[0].urldata.cost on a finished task is the final charge — log it per request
instead of estimating from the price table.
- Prompt library: gpt-image-2-6-prompts
- More image models: awesome prompt collections
CC BY 4.0
