@zerogen/sdk, and for Python,
zerogen. Their types come from the
API’s OpenAPI document, and they
follow the conventions in these docs for you:
- They send an
Idempotency-Keywith everyPOST, and reuse it when they retry. - They retry connection failures and errors whose
retryableistrue, and wait forRetry-Afteron a429. - They wait for a run to end with
Prefer: wait. - They page through lists, and upload a file in one call.
In other languages, call the REST API directly. The
API reference and the OpenAPI document describe
every endpoint.
Install
Create a run
Set your API key asZEROGEN_API_KEY, then create a run and wait for it to
end:
createAndWait, or create_and_wait in Python, creates the run with
Prefer: wait=60, then reads it with the same wait until it ends. It returns
for every final status, including failed, so check status and each
output’s status. To return without waiting, call runs.create, then
runs.get or runs.waitFor (wait_for in Python) with the run’s ID. To stop
waiting, pass an AbortSignal as signal in TypeScript, or cancel the task in
async Python; the run keeps going.
In Python, a request body is a dict with the API’s own field names, such as
projectId, as the API reference shows them. Each response is a pydantic
model with snake_case attributes, such as run.project_id. The
asynchronous client, AsyncZeroGen, has the same methods to await.
Each model takes its own inputs and parameters. Read a model’s request schema
with zerogen.models.get("openai/gpt-image-2/generate"). For details, see
Models in the API.
Upload an input
uploads.upload runs the three requests in
Upload a file and returns the ready asset’s ID:
Blob, File, ArrayBuffer, or Uint8Array, and
reads the media type from the file or from the filename’s extension. For files
up to 64 MiB, it sends the file’s SHA-256 digest. In Python, it takes a path,
bytes, or a binary file object that can seek, reads the media type from the
filename’s extension, and always sends the digest. With the digest, a file the
project already has is reused instead of uploaded again.
Download outputs
The libraries cover models, runs, and uploads. To download an output’s file, or to browse your Library, call the REST API: readGET /v1/assets/{assetId}/download with the output’s asset.id, using a key
with the library:read scope. For details, see
Download your outputs.
List
Iterate a list to read every item on every page:data and
nextCursor. In Python, a list method returns the first page, with data and
next_cursor. With AsyncZeroGen, iterate a list with async for, or await
it for one page.
Handle errors
A request that ZeroGen refuses fails with aZeroGenError, which carries the
error envelope: status, code, param, retryable, details,
and the response’s request ID, as requestId in TypeScript and request_id
in Python. Branch on code:
ZeroGenConnectionError.
Retries and idempotency
The libraries retry a request twice by default. They retry only connection failures and errors whoseretryable is true, and they don’t wait for a
Retry-After longer than one minute. Each POST call gets a new
Idempotency-Key, which that call’s retries reuse.
To retry safely across calls or processes, such as from a job queue, pass your
own key:
Configure the client
Every method also takes request options. In TypeScript, they’re its last
argument:
signal, timeout, maxRetries, idempotencyKey for a POST,
and headers. In Python, they’re keyword arguments: timeout, max_retries,
idempotency_key for a POST, and headers.