|
| 1 | +# API reference and file workflows |
| 2 | + |
| 3 | +The synchronous Python client returns dictionaries from the Subfork API. It does |
| 4 | +not print results; the command-line tool's output options do not affect Python. |
| 5 | +Set `SUBFORK_API_KEY`, then use a context manager to close connections reliably. |
| 6 | + |
| 7 | +## Execute and inspect results |
| 8 | + |
| 9 | +```python |
| 10 | +from typing import Any, Dict |
| 11 | +from subfork import Subfork |
| 12 | + |
| 13 | + |
| 14 | +def run_graph(client: Subfork, graph_id: str) -> Dict[str, Any]: |
| 15 | + """Run a graph and return its completed outputs.""" |
| 16 | + submitted = client.graphs.execute(graph_id) |
| 17 | + result = client.executions.wait(submitted["id"], timeout=300) |
| 18 | + if result["status"] != "completed": |
| 19 | + raise RuntimeError(f"Execution {submitted['id']}: {result['status']}") |
| 20 | + return result["outputs"] |
| 21 | + |
| 22 | + |
| 23 | +with Subfork() as client: |
| 24 | + outputs = run_graph(client, "g_YOUR_GRAPH") |
| 25 | +``` |
| 26 | + |
| 27 | +Output names and shapes are defined by each graph. Text and JSON can be used |
| 28 | +immediately. A media output may contain an `artifact` object, a remote URL, or |
| 29 | +inline data. Artifact download helpers accept an artifact ID, not arbitrary URLs. |
| 30 | +Waiting requires `graphs:read`; starting a run requires `graphs:run`. A waiting |
| 31 | +timeout does not cancel the remote execution. |
| 32 | + |
| 33 | +## Download artifacts |
| 34 | + |
| 35 | +For a graph exposing an `audio` media output with an artifact reference: |
| 36 | + |
| 37 | +```python |
| 38 | +with Subfork() as client: |
| 39 | + outputs = run_graph(client, "g_YOUR_GRAPH") |
| 40 | + artifact_id = outputs["audio"]["artifact"]["artifact_id"] |
| 41 | + path = client.artifacts.download(artifact_id, "narration.mp3") |
| 42 | +``` |
| 43 | + |
| 44 | +A direct artifact output instead exposes `outputs["artifact"]["artifact_id"]`. |
| 45 | +The helper works for PDFs, images, MP3, MP4 and other stored file types. It returns |
| 46 | +`pathlib.Path`; it does not decode media or open a viewer. Use your preferred media |
| 47 | +library or player after downloading. |
| 48 | + |
| 49 | +`download(artifact_id, destination, *, overwrite=False, max_bytes=250_000_000)` |
| 50 | +streams to a temporary file, then publishes the complete file at the requested |
| 51 | +path. The parent directory must exist. Existing files are protected unless you |
| 52 | +pass `overwrite=True`; failed downloads preserve them. The default decoded-byte |
| 53 | +limit is 250 MB and can be increased explicitly. |
| 54 | + |
| 55 | +Downloads require `graphs:read` and access to the artifact. Expired or deleted |
| 56 | +artifacts cannot be recovered by the client. One HTTPS storage redirect is |
| 57 | +supported; the Subfork authorization header and cookies are not forwarded. |
| 58 | + |
| 59 | +## Upload graph assets |
| 60 | + |
| 61 | +**Server requirement:** API-key asset upload/list support must be deployed. |
| 62 | +Older servers return HTTP 403 even with an otherwise valid key. |
| 63 | + |
| 64 | +```python |
| 65 | +with Subfork() as client: |
| 66 | + uploaded = client.assets.upload( |
| 67 | + "g_YOUR_GRAPH", "document.pdf", content_type="application/pdf" |
| 68 | + ) |
| 69 | + artifact_id = uploaded["artifact_id"] |
| 70 | + print(artifact_id) |
| 71 | +``` |
| 72 | + |
| 73 | +`upload(graph_id, source, *, content_type=None)` streams a local file as multipart |
| 74 | +form data. The MIME type is inferred from its filename unless supplied; the |
| 75 | +server validates the file and controls persisted metadata. The returned JSON |
| 76 | +includes `artifact_id`, `media_type`, `size_bytes` and other artifact metadata. |
| 77 | + |
| 78 | +Uploads require `graphs:write`, an owned graph, enabled uploads and available |
| 79 | +storage quota. The asset starts private. Uploading does **not** automatically |
| 80 | +bind an Asset node or execute the graph. To select it on an existing Asset node: |
| 81 | + |
| 82 | +```python |
| 83 | +with Subfork() as client: |
| 84 | + graph_id = "g_YOUR_GRAPH" |
| 85 | + graph = client.graphs.get(graph_id) |
| 86 | + definition = graph["definition"] |
| 87 | + source_node = next( |
| 88 | + node for node in definition["nodes"] |
| 89 | + if node["node_instance_id"] == "pdf" and node["node_id"] == "n_asset" |
| 90 | + ) |
| 91 | + uploaded = client.assets.upload(graph_id, "document.pdf") |
| 92 | + source_node["params"]["artifact_id"] = uploaded["artifact_id"] |
| 93 | + client.graphs.update( |
| 94 | + graph_id, name=graph["name"], definition=definition, |
| 95 | + description=graph.get("description") or "", |
| 96 | + ) |
| 97 | +``` |
| 98 | + |
| 99 | +The example changes the draft; existing published versions remain immutable. |
| 100 | +Fetch the latest draft and avoid concurrent edits when updating its definition. |
| 101 | +For a graph exposing an artifact input, you can instead supply the reference in |
| 102 | +`graphs.execute(..., inputs={"pdf": {"artifact_id": artifact_id}})`. The input |
| 103 | +name must match that graph's published or draft interface. |
| 104 | + |
| 105 | +List assets with `client.assets.list(graph_id)`. It returns an `assets` array in a |
| 106 | +JSON object. Use `include_generated=True` to include unexpired execution outputs. |
| 107 | +Listing requires `graphs:read` and ownership of the graph. |
| 108 | + |
| 109 | +The client does not expose secret management, asset deletion, visibility changes |
| 110 | +or retention changes. Configure provider secrets through the Subfork UI. |
| 111 | + |
| 112 | +## Errors and retries |
| 113 | + |
| 114 | +HTTP failures raise `APIError` subclasses, including permission, validation and |
| 115 | +rate-limit errors. Downloads may also raise `FileExistsError`, filesystem errors, |
| 116 | +or `ValueError` for an invalid limit or oversized response. Network failures raise |
| 117 | +`TransportError`. File transfers and graph executions are never automatically |
| 118 | +retried: an upload or run may have succeeded before the connection failed. Inspect |
| 119 | +remote state before repeating a write. |
0 commit comments