zerogen 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {zerogen-0.2.0 → zerogen-0.3.0}/PKG-INFO +163 -7
  2. zerogen-0.3.0/README.md +366 -0
  3. {zerogen-0.2.0 → zerogen-0.3.0}/pyproject.toml +1 -1
  4. {zerogen-0.2.0 → zerogen-0.3.0}/pyproject.toml.orig +1 -1
  5. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/__init__.py +6 -2
  6. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/_client.py +32 -7
  7. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/_errors.py +11 -0
  8. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/_options.py +1 -1
  9. zerogen-0.3.0/src/zerogen/_version.py +1 -0
  10. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/assets.py +320 -59
  11. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/folders.py +162 -5
  12. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/runs.py +87 -6
  13. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/uploads.py +84 -3
  14. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/types/__init__.py +66 -0
  15. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/types/_requests.py +174 -2
  16. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/types/_responses.py +305 -67
  17. zerogen-0.2.0/README.md +0 -210
  18. zerogen-0.2.0/src/zerogen/_version.py +0 -1
  19. {zerogen-0.2.0 → zerogen-0.3.0}/LICENSE +0 -0
  20. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/_models.py +0 -0
  21. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/_pagination.py +0 -0
  22. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/py.typed +0 -0
  23. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/__init__.py +0 -0
  24. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/labels.py +0 -0
  25. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/models.py +0 -0
  26. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/naming_presets.py +0 -0
  27. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/projects.py +0 -0
  28. {zerogen-0.2.0 → zerogen-0.3.0}/src/zerogen/resources/skills.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zerogen
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: The official Python library for the ZeroGen API
5
5
  Keywords: zerogen,generative-ai,image-generation,video-generation,api
6
6
  Author: ZeroGen LLC
@@ -77,6 +77,18 @@ response is a pydantic model with snake_case attributes, such as
77
77
  completes and checks them. Each model takes its own parameters: read a model's
78
78
  request schema with `zerogen.models.get(id)`.
79
79
 
80
+ To label every output as it lands, add `labelIds`, which also needs the
81
+ `library:organize` scope. To upscale one output of a 480p Draft to its final,
82
+ send `upscaleDraft` with the draft's model ID, and leave out `prompt`, `inputs`,
83
+ `output`, `parameters` and `naming`. The final is a new run, quoted and billed
84
+ like any other:
85
+
86
+ ```python
87
+ final = zerogen.runs.create_and_wait(
88
+ {"model": draft_model, "upscaleDraft": {"runId": draft.id, "outputIndex": 0}}
89
+ )
90
+ ```
91
+
80
92
  ## Async
81
93
 
82
94
  `AsyncZeroGen` has the same methods, to await:
@@ -101,6 +113,35 @@ asyncio.run(main())
101
113
  Cancel the task to stop a `create_and_wait` or `wait_for` from waiting; the run
102
114
  keeps running.
103
115
 
116
+ ## Progress
117
+
118
+ To show a run's progress while you wait, pass `wait_for` an `on_update`
119
+ function, which gets the run after each read, and a shorter `poll`:
120
+
121
+ ```python
122
+ from zerogen.types import Run
123
+
124
+
125
+ def report(run: Run) -> None:
126
+ done = sum(output.status == "succeeded" for output in run.outputs)
127
+ print(f"{run.status}: {done} of {len(run.outputs)} outputs")
128
+
129
+
130
+ run = zerogen.runs.create(request)
131
+ run = zerogen.runs.wait_for(run.id, poll=5, on_update=report)
132
+ ```
133
+
134
+ `poll` is how many seconds each read may wait for the run to end, from 1 to 60,
135
+ and 60 by default. The API answers as soon as the run ends, so a shorter `poll`
136
+ only reports progress more often, at a request each time. With `AsyncZeroGen`,
137
+ `on_update` can also be a coroutine function.
138
+
139
+ To stop waiting from another thread, such as when someone presses Cancel, pass
140
+ `ZeroGen`'s `wait_for` a `threading.Event` as `cancel`. When it's set,
141
+ `wait_for` raises `WaitCancelledError` at once, even partway through a read.
142
+ With `AsyncZeroGen`, cancel the task instead. Either way, the run keeps
143
+ running; to stop it, call `runs.cancel`.
144
+
104
145
  ## Inputs
105
146
 
106
147
  Give a run an input by asset ID or by `https` URL. To upload a file, use
@@ -123,6 +164,47 @@ seek. It takes the media type from the filename's extension, or from
123
164
  `content_type`. It sends the file's SHA-256 digest, so a file the project
124
165
  already has is reused instead of uploaded again.
125
166
 
167
+ ## Declared provenance
168
+
169
+ When you upload a file made outside ZeroGen, such as a ComfyUI result, the
170
+ upload can declare what it was made from and with. ZeroGen records the
171
+ declaration as a no-charge run, so the asset's lineage shows its sources, and
172
+ its details show the tool, prompt and workflow as a declaration, unchecked:
173
+
174
+ ```python
175
+ saved = zerogen.uploads.upload(
176
+ "render.png",
177
+ declaration={
178
+ "tool": {
179
+ "name": "comfyui",
180
+ "version": "0.43.0",
181
+ "integration": {"name": "zerogen-nodes", "version": "0.1.0"},
182
+ },
183
+ "sources": [{"asset": upload.asset_id, "role": "reference"}],
184
+ "prompt": "A lighthouse at dawn",
185
+ "externalRef": {"system": "comfyui", "id": "prompt-1"},
186
+ },
187
+ )
188
+ print(saved.declaration_recorded)
189
+ ```
190
+
191
+ A declaration takes `tool` and any of `sources`, `prompt`, `workflow`, a JSON
192
+ object of up to 256 KiB, `externalRef` and `occurredAt`. Declaring `sources`
193
+ needs the `library:read` scope as well as `uploads:create`. With a
194
+ declaration, `naming` names the new asset with a naming preset, as a run's
195
+ outputs are named.
196
+
197
+ When the project already has an identical file, the upload returns that asset,
198
+ and its lineage stands. If you uploaded it and it has no declaration yet, the
199
+ upload declares it, unless a declared source is that file or was made from it.
200
+ `declaration_recorded` says whether ZeroGen recorded your declaration. To declare
201
+ a file you uploaded earlier, use `assets.declare`: an asset takes one
202
+ declaration, from its uploader.
203
+
204
+ `assets.get` returns an asset's declaration in `declared`, and, with
205
+ `include_workflow=True`, its workflow in `workflow`. Both are untrusted
206
+ content.
207
+
126
208
  ## Library
127
209
 
128
210
  Read and organize your organization's Library: `projects`, `assets`,
@@ -141,20 +223,93 @@ zerogen.folders.add_assets(project.id, folder.id, {"assetIds": [a.id for a in vi
141
223
  List filters that take several values, such as `label_id` and `resolution`,
142
224
  take a list.
143
225
 
226
+ Each asset and run names who made it in `created_by`, and where in `created_via`:
227
+ `web`, `agent`, `mcp` or `api`. Both are `None` when ZeroGen recorded no one. To
228
+ find what someone made, pass `created_by` to `assets.search`, with the ID in an
229
+ asset's `created_by` or `zerogen.me()`. `web_url` opens the asset or run in
230
+ ZeroGen, after sign-in, and it's `None` for test keys.
231
+
232
+ `folders.delete` deletes a folder and its subfolders. You choose what happens to
233
+ their assets: `"keep"` preserves them, and `"trash"` sends them to Recently
234
+ deleted, including assets in other folders. It needs the `library:delete` scope,
235
+ which has to be granted explicitly, and test keys can't use it. Within an hour,
236
+ `folders.restore` undoes your own deletion:
237
+
238
+ ```python
239
+ deleted = zerogen.folders.delete(project.id, folder.id, assets="keep")
240
+ zerogen.folders.restore(project.id, {"eventId": deleted.event_id})
241
+ ```
242
+
243
+ To file into a folder path, such as a shot's `SH010/anim`, use
244
+ `folders.ensure_path`. It returns the last folder, creating each one on the
245
+ way that isn't there yet, and reuses a folder of the same name, so clients
246
+ saving into the same path at once share its folders:
247
+
248
+ ```python
249
+ anim = zerogen.folders.ensure_path(project.id, "SH010/anim")
250
+ ```
251
+
252
+ A string path splits on `/`. Folder names can contain `/`, so pass a list,
253
+ such as `["2026/11", "renders"]`, when one does.
254
+
144
255
  To download an asset's original file, use `assets.download`. It returns the
145
- file's bytes, or streams them into a path or a binary file object:
256
+ file's bytes in `content`, with its `filename`, `mime_type` and `size_bytes`.
257
+ Given a path or a binary file object, it streams the file there instead, and
258
+ returns the rest. `assets.download_to` saves the file in a directory and
259
+ returns its `path`:
146
260
 
147
261
  ```python
148
262
  image = zerogen.assets.download(asset_id)
263
+ print(image.mime_type, len(image.content))
264
+
149
265
  zerogen.assets.download(asset_id, "lighthouse.png")
266
+
267
+ saved = zerogen.assets.download_to(asset_id, "downloads")
268
+ print(saved.path)
150
269
  ```
151
270
 
271
+ The `filename` is untrusted content, so don't use it as a path yourself.
272
+ `download_to` makes a name from it with no path separators or characters that
273
+ Windows refuses. When a file in the directory already has that name, it adds a
274
+ number, as in `lighthouse (1).png`, rather than overwrite the file.
275
+
152
276
  Each call reads a new signed link and sends it to storage without your API key.
153
277
  When storage refuses a link as expired, it reads a new one and tries once more.
154
278
  To hand the link to something else, read it with `assets.get_download`: it's
155
279
  valid for 5 minutes, and anyone holding it can download the file, so keep it
156
280
  private.
157
281
 
282
+ ## Previews
283
+
284
+ To show assets without downloading their originals, use their `preview`. Each
285
+ asset from `assets.search` and `assets.get` has one, and so does each run
286
+ output's `asset`.
287
+
288
+ ```python
289
+ page = zerogen.assets.search(project_id=project.id, limit=50)
290
+ tiles = [
291
+ {
292
+ "id": asset.id,
293
+ "image": asset.preview.image512_url if asset.preview else None,
294
+ "clip": asset.preview.video_url if asset.preview else None,
295
+ }
296
+ for asset in page.data
297
+ ]
298
+ ```
299
+
300
+ `image96_url` and `image512_url` are WebP images up to 96 and 512 pixels wide.
301
+ `video_url` is a silent WebM clip of a video's first six seconds; it's `None`
302
+ for other assets and until the clip is made. `preview` itself is `None` until
303
+ the images are made, which can take up to about 15 minutes after a run, and for
304
+ assets with none, such as audio.
305
+
306
+ The URLs are signed storage URLs, so load them without your API key: a browser
307
+ can load one as an `<img>` element's `src`. They work until `expires_at`, 5
308
+ minutes later, and anyone holding them can load the previews until then, so
309
+ keep them out of logs. After `expires_at`, read the page again for new ones:
310
+ one `assets.search` call refreshes every tile on it, where `assets.get` would
311
+ take a request for each. For a single asset or a run, read it again.
312
+
158
313
  ## Lists
159
314
 
160
315
  List methods return one page, and iterating the page reads every item on every
@@ -195,9 +350,9 @@ The client retries connection errors and errors the API marks `retryable`,
195
350
  twice by default, with backoff. For `429` responses, it waits for
196
351
  `Retry-After`, unless that's more than a minute.
197
352
 
198
- Every `POST` sends an `Idempotency-Key`, and a call's retries reuse it, so a
199
- retried create never starts a second run. To retry safely across calls or
200
- processes, pass your own key:
353
+ Every `POST` and `DELETE` sends an `Idempotency-Key`, and a call's retries reuse
354
+ it, so a retried create never starts a second run. To retry safely across calls
355
+ or processes, pass your own key:
201
356
 
202
357
  ```python
203
358
  zerogen.runs.create(request, idempotency_key=job.id)
@@ -222,11 +377,12 @@ zerogen = ZeroGen(
222
377
  | `max_retries` | `2` | Retries for each request. |
223
378
  | `timeout` | `120` | Seconds httpx waits on each step of an attempt. A request that waits on a run gets its wait added. |
224
379
  | `default_headers` | None | Headers to send with every request. |
380
+ | `user_agent` | None | Your app's name and version, such as `zerogen-nodes/0.1`, which the `User-Agent` header names before this library's. |
225
381
  | `http_client` | A new `httpx.Client` or `httpx.AsyncClient` | The httpx client to send requests with, such as one with your proxy settings. |
226
382
 
227
383
  Every method also takes request options as keyword arguments: `timeout`,
228
- `max_retries`, `headers`, and `idempotency_key` for a `POST`. Close a client
229
- you're done with, or use it as a context manager.
384
+ `max_retries`, `headers`, and `idempotency_key` for a `POST` or `DELETE`. Close
385
+ a client you're done with, or use it as a context manager.
230
386
 
231
387
  ## Test keys
232
388
 
@@ -0,0 +1,366 @@
1
+ # ZeroGen Python library
2
+
3
+ The official Python client for the [ZeroGen API](https://docs.zerogen.com). It
4
+ runs on Python 3.10 or later, has a synchronous and an asynchronous client, and
5
+ is built on [httpx](https://www.python-httpx.org) and
6
+ [pydantic](https://docs.pydantic.dev).
7
+
8
+ ```bash
9
+ pip install zerogen
10
+ ```
11
+
12
+ The ZeroGen API refuses requests from browsers, so keep your API key on your
13
+ server.
14
+
15
+ ## Usage
16
+
17
+ Create an API key in ZeroGen under **Settings → API keys**, and set it as
18
+ `ZEROGEN_API_KEY`.
19
+
20
+ ```python
21
+ from zerogen import ZeroGen
22
+
23
+ zerogen = ZeroGen() # Reads ZEROGEN_API_KEY.
24
+
25
+ run = zerogen.runs.create_and_wait(
26
+ {"model": "openai/gpt-image-2/generate", "prompt": "A lighthouse at dawn"}
27
+ )
28
+
29
+ for output in run.outputs:
30
+ if output.asset:
31
+ print(output.asset.id, output.asset.mime_type)
32
+ ```
33
+
34
+ `create_and_wait` starts a run and returns it when it ends. A run that fails
35
+ returns too, so check its `status`, and each output's. Each output's asset is in
36
+ your organization's Library, in the run's project: your team can open it in
37
+ ZeroGen, you can [download it](#library), and you can send its ID as an input to
38
+ a later run. To start a run without waiting, use `create`, then `get` or
39
+ `wait_for` with its `id`.
40
+
41
+ A request body is a dict with the API's own field names, such as `projectId`
42
+ and `maxCostMicroUsd`, exactly as the
43
+ [API reference](https://docs.zerogen.com/api-reference/overview) shows it. Each
44
+ response is a pydantic model with snake_case attributes, such as
45
+ `run.project_id`. Both are typed from the API's
46
+ [OpenAPI document](https://api.zerogen.com/v1/openapi.json), so your editor
47
+ completes and checks them. Each model takes its own parameters: read a model's
48
+ request schema with `zerogen.models.get(id)`.
49
+
50
+ To label every output as it lands, add `labelIds`, which also needs the
51
+ `library:organize` scope. To upscale one output of a 480p Draft to its final,
52
+ send `upscaleDraft` with the draft's model ID, and leave out `prompt`, `inputs`,
53
+ `output`, `parameters` and `naming`. The final is a new run, quoted and billed
54
+ like any other:
55
+
56
+ ```python
57
+ final = zerogen.runs.create_and_wait(
58
+ {"model": draft_model, "upscaleDraft": {"runId": draft.id, "outputIndex": 0}}
59
+ )
60
+ ```
61
+
62
+ ## Async
63
+
64
+ `AsyncZeroGen` has the same methods, to await:
65
+
66
+ ```python
67
+ import asyncio
68
+
69
+ from zerogen import AsyncZeroGen
70
+
71
+
72
+ async def main() -> None:
73
+ async with AsyncZeroGen() as zerogen:
74
+ run = await zerogen.runs.create_and_wait(
75
+ {"model": "openai/gpt-image-2/generate", "prompt": "A lighthouse at dawn"}
76
+ )
77
+ print(run.status)
78
+
79
+
80
+ asyncio.run(main())
81
+ ```
82
+
83
+ Cancel the task to stop a `create_and_wait` or `wait_for` from waiting; the run
84
+ keeps running.
85
+
86
+ ## Progress
87
+
88
+ To show a run's progress while you wait, pass `wait_for` an `on_update`
89
+ function, which gets the run after each read, and a shorter `poll`:
90
+
91
+ ```python
92
+ from zerogen.types import Run
93
+
94
+
95
+ def report(run: Run) -> None:
96
+ done = sum(output.status == "succeeded" for output in run.outputs)
97
+ print(f"{run.status}: {done} of {len(run.outputs)} outputs")
98
+
99
+
100
+ run = zerogen.runs.create(request)
101
+ run = zerogen.runs.wait_for(run.id, poll=5, on_update=report)
102
+ ```
103
+
104
+ `poll` is how many seconds each read may wait for the run to end, from 1 to 60,
105
+ and 60 by default. The API answers as soon as the run ends, so a shorter `poll`
106
+ only reports progress more often, at a request each time. With `AsyncZeroGen`,
107
+ `on_update` can also be a coroutine function.
108
+
109
+ To stop waiting from another thread, such as when someone presses Cancel, pass
110
+ `ZeroGen`'s `wait_for` a `threading.Event` as `cancel`. When it's set,
111
+ `wait_for` raises `WaitCancelledError` at once, even partway through a read.
112
+ With `AsyncZeroGen`, cancel the task instead. Either way, the run keeps
113
+ running; to stop it, call `runs.cancel`.
114
+
115
+ ## Inputs
116
+
117
+ Give a run an input by asset ID or by `https` URL. To upload a file, use
118
+ `uploads.upload`, which returns when the asset is ready:
119
+
120
+ ```python
121
+ upload = zerogen.uploads.upload("storyboard.png")
122
+
123
+ run = zerogen.runs.create_and_wait(
124
+ {
125
+ "model": "openai/gpt-image-2/edit",
126
+ "prompt": "Turn the sky into a sunset",
127
+ "inputs": [{"role": "source", "asset": upload.asset_id}],
128
+ }
129
+ )
130
+ ```
131
+
132
+ `upload` takes a path, the file's `bytes`, or a binary file object that can
133
+ seek. It takes the media type from the filename's extension, or from
134
+ `content_type`. It sends the file's SHA-256 digest, so a file the project
135
+ already has is reused instead of uploaded again.
136
+
137
+ ## Declared provenance
138
+
139
+ When you upload a file made outside ZeroGen, such as a ComfyUI result, the
140
+ upload can declare what it was made from and with. ZeroGen records the
141
+ declaration as a no-charge run, so the asset's lineage shows its sources, and
142
+ its details show the tool, prompt and workflow as a declaration, unchecked:
143
+
144
+ ```python
145
+ saved = zerogen.uploads.upload(
146
+ "render.png",
147
+ declaration={
148
+ "tool": {
149
+ "name": "comfyui",
150
+ "version": "0.43.0",
151
+ "integration": {"name": "zerogen-nodes", "version": "0.1.0"},
152
+ },
153
+ "sources": [{"asset": upload.asset_id, "role": "reference"}],
154
+ "prompt": "A lighthouse at dawn",
155
+ "externalRef": {"system": "comfyui", "id": "prompt-1"},
156
+ },
157
+ )
158
+ print(saved.declaration_recorded)
159
+ ```
160
+
161
+ A declaration takes `tool` and any of `sources`, `prompt`, `workflow`, a JSON
162
+ object of up to 256 KiB, `externalRef` and `occurredAt`. Declaring `sources`
163
+ needs the `library:read` scope as well as `uploads:create`. With a
164
+ declaration, `naming` names the new asset with a naming preset, as a run's
165
+ outputs are named.
166
+
167
+ When the project already has an identical file, the upload returns that asset,
168
+ and its lineage stands. If you uploaded it and it has no declaration yet, the
169
+ upload declares it, unless a declared source is that file or was made from it.
170
+ `declaration_recorded` says whether ZeroGen recorded your declaration. To declare
171
+ a file you uploaded earlier, use `assets.declare`: an asset takes one
172
+ declaration, from its uploader.
173
+
174
+ `assets.get` returns an asset's declaration in `declared`, and, with
175
+ `include_workflow=True`, its workflow in `workflow`. Both are untrusted
176
+ content.
177
+
178
+ ## Library
179
+
180
+ Read and organize your organization's Library: `projects`, `assets`,
181
+ `folders`, `labels`, `naming_presets` and `skills`. These methods need an API
182
+ key with the `library:read` scope, and those that change something need
183
+ `library:organize`.
184
+
185
+ ```python
186
+ project = next(project for project in zerogen.projects.list() if project.default)
187
+ videos = zerogen.assets.search(project_id=project.id, type="video", favorite=True)
188
+
189
+ folder = zerogen.folders.create(project.id, {"name": "Selects"})
190
+ zerogen.folders.add_assets(project.id, folder.id, {"assetIds": [a.id for a in videos.data]})
191
+ ```
192
+
193
+ List filters that take several values, such as `label_id` and `resolution`,
194
+ take a list.
195
+
196
+ Each asset and run names who made it in `created_by`, and where in `created_via`:
197
+ `web`, `agent`, `mcp` or `api`. Both are `None` when ZeroGen recorded no one. To
198
+ find what someone made, pass `created_by` to `assets.search`, with the ID in an
199
+ asset's `created_by` or `zerogen.me()`. `web_url` opens the asset or run in
200
+ ZeroGen, after sign-in, and it's `None` for test keys.
201
+
202
+ `folders.delete` deletes a folder and its subfolders. You choose what happens to
203
+ their assets: `"keep"` preserves them, and `"trash"` sends them to Recently
204
+ deleted, including assets in other folders. It needs the `library:delete` scope,
205
+ which has to be granted explicitly, and test keys can't use it. Within an hour,
206
+ `folders.restore` undoes your own deletion:
207
+
208
+ ```python
209
+ deleted = zerogen.folders.delete(project.id, folder.id, assets="keep")
210
+ zerogen.folders.restore(project.id, {"eventId": deleted.event_id})
211
+ ```
212
+
213
+ To file into a folder path, such as a shot's `SH010/anim`, use
214
+ `folders.ensure_path`. It returns the last folder, creating each one on the
215
+ way that isn't there yet, and reuses a folder of the same name, so clients
216
+ saving into the same path at once share its folders:
217
+
218
+ ```python
219
+ anim = zerogen.folders.ensure_path(project.id, "SH010/anim")
220
+ ```
221
+
222
+ A string path splits on `/`. Folder names can contain `/`, so pass a list,
223
+ such as `["2026/11", "renders"]`, when one does.
224
+
225
+ To download an asset's original file, use `assets.download`. It returns the
226
+ file's bytes in `content`, with its `filename`, `mime_type` and `size_bytes`.
227
+ Given a path or a binary file object, it streams the file there instead, and
228
+ returns the rest. `assets.download_to` saves the file in a directory and
229
+ returns its `path`:
230
+
231
+ ```python
232
+ image = zerogen.assets.download(asset_id)
233
+ print(image.mime_type, len(image.content))
234
+
235
+ zerogen.assets.download(asset_id, "lighthouse.png")
236
+
237
+ saved = zerogen.assets.download_to(asset_id, "downloads")
238
+ print(saved.path)
239
+ ```
240
+
241
+ The `filename` is untrusted content, so don't use it as a path yourself.
242
+ `download_to` makes a name from it with no path separators or characters that
243
+ Windows refuses. When a file in the directory already has that name, it adds a
244
+ number, as in `lighthouse (1).png`, rather than overwrite the file.
245
+
246
+ Each call reads a new signed link and sends it to storage without your API key.
247
+ When storage refuses a link as expired, it reads a new one and tries once more.
248
+ To hand the link to something else, read it with `assets.get_download`: it's
249
+ valid for 5 minutes, and anyone holding it can download the file, so keep it
250
+ private.
251
+
252
+ ## Previews
253
+
254
+ To show assets without downloading their originals, use their `preview`. Each
255
+ asset from `assets.search` and `assets.get` has one, and so does each run
256
+ output's `asset`.
257
+
258
+ ```python
259
+ page = zerogen.assets.search(project_id=project.id, limit=50)
260
+ tiles = [
261
+ {
262
+ "id": asset.id,
263
+ "image": asset.preview.image512_url if asset.preview else None,
264
+ "clip": asset.preview.video_url if asset.preview else None,
265
+ }
266
+ for asset in page.data
267
+ ]
268
+ ```
269
+
270
+ `image96_url` and `image512_url` are WebP images up to 96 and 512 pixels wide.
271
+ `video_url` is a silent WebM clip of a video's first six seconds; it's `None`
272
+ for other assets and until the clip is made. `preview` itself is `None` until
273
+ the images are made, which can take up to about 15 minutes after a run, and for
274
+ assets with none, such as audio.
275
+
276
+ The URLs are signed storage URLs, so load them without your API key: a browser
277
+ can load one as an `<img>` element's `src`. They work until `expires_at`, 5
278
+ minutes later, and anyone holding them can load the previews until then, so
279
+ keep them out of logs. After `expires_at`, read the page again for new ones:
280
+ one `assets.search` call refreshes every tile on it, where `assets.get` would
281
+ take a request for each. For a single asset or a run, read it again.
282
+
283
+ ## Lists
284
+
285
+ List methods return one page, and iterating the page reads every item on every
286
+ page after it:
287
+
288
+ ```python
289
+ for model in zerogen.models.list(capability="video"):
290
+ print(model.id)
291
+
292
+ page = zerogen.runs.list(limit=20)
293
+ print(page.data, page.next_cursor)
294
+ ```
295
+
296
+ With `AsyncZeroGen`, iterate a list with `async for`, or await it for one page.
297
+
298
+ ## Errors
299
+
300
+ A request the API refuses raises a `ZeroGenError`. Branch on its `code`, which
301
+ is stable; its `message` is for people.
302
+
303
+ ```python
304
+ from zerogen import ZeroGenError
305
+
306
+ try:
307
+ zerogen.runs.create({"model": "openai/gpt-image-2/generate", "prompt": ""})
308
+ except ZeroGenError as error:
309
+ print(error.status, error.code, error.param, error.request_id)
310
+ raise
311
+ ```
312
+
313
+ A request that gets no response, because the connection failed or the request
314
+ timed out, raises a `ZeroGenConnectionError`. For every code, see
315
+ [Errors](https://docs.zerogen.com/errors).
316
+
317
+ ## Retries and idempotency
318
+
319
+ The client retries connection errors and errors the API marks `retryable`,
320
+ twice by default, with backoff. For `429` responses, it waits for
321
+ `Retry-After`, unless that's more than a minute.
322
+
323
+ Every `POST` and `DELETE` sends an `Idempotency-Key`, and a call's retries reuse
324
+ it, so a retried create never starts a second run. To retry safely across calls
325
+ or processes, pass your own key:
326
+
327
+ ```python
328
+ zerogen.runs.create(request, idempotency_key=job.id)
329
+ ```
330
+
331
+ ## Options
332
+
333
+ ```python
334
+ import os
335
+
336
+ zerogen = ZeroGen(
337
+ api_key=os.environ["ZEROGEN_API_KEY"],
338
+ max_retries=2,
339
+ timeout=120, # Seconds per attempt. Waits on a run add their wait.
340
+ )
341
+ ```
342
+
343
+ | Option | Default | Description |
344
+ | --- | --- | --- |
345
+ | `api_key` | `ZEROGEN_API_KEY` | The API key. |
346
+ | `base_url` | `ZEROGEN_BASE_URL`, then `https://api.zerogen.com/v1` | The API's base URL. |
347
+ | `max_retries` | `2` | Retries for each request. |
348
+ | `timeout` | `120` | Seconds httpx waits on each step of an attempt. A request that waits on a run gets its wait added. |
349
+ | `default_headers` | None | Headers to send with every request. |
350
+ | `user_agent` | None | Your app's name and version, such as `zerogen-nodes/0.1`, which the `User-Agent` header names before this library's. |
351
+ | `http_client` | A new `httpx.Client` or `httpx.AsyncClient` | The httpx client to send requests with, such as one with your proxy settings. |
352
+
353
+ Every method also takes request options as keyword arguments: `timeout`,
354
+ `max_retries`, `headers`, and `idempotency_key` for a `POST` or `DELETE`. Close
355
+ a client you're done with, or use it as a context manager.
356
+
357
+ ## Test keys
358
+
359
+ A test key runs every request on placeholder providers and charges nothing. Its
360
+ runs can rehearse failures with the `test` field, and it reaches only the API
361
+ sandbox project. For details, see
362
+ [Test keys](https://docs.zerogen.com/test-keys).
363
+
364
+ ## License
365
+
366
+ MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "zerogen"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "The official Python library for the ZeroGen API"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "zerogen"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "The official Python library for the ZeroGen API"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -2,11 +2,11 @@
2
2
 
3
3
  from . import types
4
4
  from ._client import AsyncZeroGen, ZeroGen
5
- from ._errors import ZeroGenConnectionError, ZeroGenError
5
+ from ._errors import WaitCancelledError, ZeroGenConnectionError, ZeroGenError
6
6
  from ._options import PostRequestOptions, RequestOptions
7
7
  from ._pagination import AsyncPage, AsyncPaginator, Page
8
8
  from ._version import __version__
9
- from .resources.assets import DownloadDestination
9
+ from .resources.assets import Download, DownloadDestination, DownloadInfo, SavedDownload
10
10
  from .resources.runs import has_ended
11
11
  from .resources.uploads import ReadyUpload, UploadSource
12
12
 
@@ -14,12 +14,16 @@ __all__ = [
14
14
  "AsyncPage",
15
15
  "AsyncPaginator",
16
16
  "AsyncZeroGen",
17
+ "Download",
17
18
  "DownloadDestination",
19
+ "DownloadInfo",
18
20
  "Page",
19
21
  "PostRequestOptions",
20
22
  "ReadyUpload",
21
23
  "RequestOptions",
24
+ "SavedDownload",
22
25
  "UploadSource",
26
+ "WaitCancelledError",
23
27
  "ZeroGen",
24
28
  "ZeroGenConnectionError",
25
29
  "ZeroGenError",