> ## Documentation Index
> Fetch the complete documentation index at: https://docs.f13.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming

> How the generation stream is framed and how to consume it

`POST /v1/generations` responds with a server-sent event stream
(`Content-Type: text/event-stream`). The SVG is streamed as it is generated,
followed by a frame with the stored asset, and a terminal frame that closes
the stream.

## Wire format

Each frame is a single `data:` line carrying one JSON object, terminated by a
blank line:

```text theme={null}
data: {"type":"delta","text":"<svg xmlns=…"}

data: {"type":"done"}
```

Frames are distinguished by the `type` field inside the JSON - the stream does
not use named SSE `event:` lines. Because the endpoint is a `POST` with a
body, the browser `EventSource` API cannot consume it; read the response body
and split on blank lines, as in the [quickstart](https://docs.f13.com/quickstart.md#3-consume-the-stream-from-code).

## Frame types

| `type` | Payload | Meaning |
| - | - | - |
| `delta` | `text` (string) | The next chunk of SVG markup. Concatenate all `text` values in order. |
| `diagram` | `diagram` (object) | Sent once, after the last `delta`, by models that describe their drawing: the [diagram object](https://docs.f13.com/api-reference/generations.md#the-diagram-object). For a request with `options` it holds the views, the parts and the answer key. Ignore the fields you do not know. |
| `saved` | `asset` (object) | The generation was stored to your workspace library. Carries the full [asset object](https://docs.f13.com/api-reference/generations.md#the-asset-object) - id, hosted URLs, cost, latency. |
| `save_error` | `message` (string) | The SVG was generated (and billed) but could not be stored. The message is `"Could not save to your workspace"`. You still have the markup from the deltas. |
| `photo` | `image` (string) | Zero data retention workspaces only: the PNG copy of the image, when the model returned one, as a `data:image/png;base64,` URL (at most 8 MiB decoded). Sent once, before `done`; it is not stored anywhere. |
| `done` | `retention` (string, optional), `note` (string, optional) | Terminal event. Everything you need arrived before it. `retention: "none"` says the workspace keeps nothing and no `saved` event came; `note: "not_retained"` marks a replay under that mode (see [Zero data retention](#zero-data-retention)). |
| `error` | `code` (string), `message` (string) | Terminal frame. The generation failed and its reservation was refunded. `code` is one of the stable values listed under [Failure modes](#failure-modes-to-handle); `message` is fixed wording for display, never raw upstream text, except for `unsupported_input` and `unsupported_request`, where it is the model's own explanation of what it does not take. |

## Ordering

A successful stream is:

```text theme={null}
delta* → diagram? → saved → done
```

If storage fails, `save_error` replaces `saved`. `done` never arrives without
one of the two: when the model returns nothing usable the stream ends in
`error` (`no_svg`) instead. A failed stream is:

```text theme={null}
delta* → error
```

After `done` or `error` the connection closes; no further frames follow. A
replay of a completed request (same `Idempotency-Key`) has the same shape,
with the whole SVG in a single `delta` and the same `diagram` frame.

## Zero data retention

A workspace can be set by f13 staff to keep nothing its generations produce:
no stored asset, no hosted SVG or PNG file, no stored result for retries, no
product analytics about the generation. Billing is the same: each completed
generation is charged at the workspace's price and counted on its usage. In
that mode a successful stream is:

```text theme={null}
delta* → photo? → diagram? → done
```

```text theme={null}
data: {"type":"delta","text":"<svg xmlns=…"}

data: {"type":"photo","image":"data:image/png;base64,iVBORw0KGgo…"}

data: {"type":"done","retention":"none"}
```

There is no `saved` or `save_error` event and no asset id, so a follow-up
cannot pass `parentAssetId` (it is refused with `403`,
`"code": "not_entitled"`). `photo` appears only when the model returned a PNG
copy. Keep the markup (and the photo and the `diagram` frame) you receive:
**the platform cannot give it back later.** A retry with the same `Idempotency-Key` is not billed again,
but it replays only the end of the stream:

```text theme={null}
data: {"type":"done","retention":"none","note":"not_retained"}
```

The asset, share and library routes answer `403` with
`"code": "not_entitled"` for such a workspace.

## Failure modes to handle

* **HTTP errors happen before the stream starts.** Validation problems, auth
  failures, `402`, `409`, `422`, `429` and `503` are ordinary JSON responses
  with a non-200 status - check `res.ok` before reading the stream. See
  [Errors](https://docs.f13.com/errors.md).
* **Mid-stream failures arrive inside a 200.** Once streaming has begun the
  status is already sent, so failures surface as an `error` frame. Nothing is
  charged for a stream that ends this way. Branch on its `code` (table
  below), not on `message`.
* **`save_error` is not a failed generation.** The markup you assembled from
  the deltas is complete and the generation was billed; only the library copy
  is missing. Retry with a new request if you need the hosted URL.
* **A dropped connection ends the stream silently.** Retry with the same
  [`Idempotency-Key`](https://docs.f13.com/api-reference/introduction#idempotency) to recover the
  result without being billed twice. Under
  [zero data retention](#zero-data-retention) the retry is not billed but
  cannot return the image.

### Error frame codes

| `code` | Meaning |
| - | - |
| `timeout` | The generation did not finish within 30 seconds and was cancelled. `message` is `"generation timed out"`. |
| `no_svg` | The model finished without producing a complete SVG (a refusal, prose, or a truncated document). |
| `upstream_error` | The model service failed. Retrying may succeed. |
| `unsupported_request` | The model does not draw this request at all, for example a request that is not a diagram or chart, or `output: "image"` for a drawing that has no photo. This is not a failure: retrying the same request is refused the same way, so send it to another model or service instead. `message` is the model's own explanation when it gave one. |
| `unsupported_input` | The model does not take a value the request named, for example a `language` it does not draw in or a value inside `options`. `message` says what it takes instead. Retrying the same request fails the same way. |
| `generation_failed` | Anything else. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.