Skip to content

Stream a model's output

Models whose predict() returns an iterator produce their output in pieces: tokens, frames, partial results from a long job. Turion relays the pieces as they are produced, so you can show them before the run finishes.

Terminal window
turion run acme/storyteller -i prompt="a haiku about GPUs" --stream
# Silicon rivers… <- printed as the model produces it

A version streams when its Cog output type is Iterator[…] or ConcatenateIterator[…]:

from cog import BasePredictor, ConcatenateIterator
class Predictor(BasePredictor):
def predict(self, prompt: str) -> ConcatenateIterator[str]:
for token in self.model.generate(prompt):
yield token

A plain list[str] output is not an iterator and does not stream. Nothing changes about publishing: turion cog push reads the output type from the image’s Cog schema.

Everything below also works against a non-streaming model: the stream carries no chunks and delivers the whole output in its final event.

Terminal window
turion run acme/storyteller -i prompt="…" --stream

--stream prints string chunks as they arrive (no newline between them, one at the end) and prints other chunks as JSON, one per line. It does not print status progress, and it cannot be combined with --sync or --output-dir.

import turion
client = turion.Client()
for chunk in client.run("acme/storyteller", {"prompt": "…"}, stream=True):
print(chunk, end="", flush=True)

run(..., stream=True) uploads file inputs and creates the prediction when you call it, then returns an iterator over the output chunks. A model that does not stream yields one item, its final output. Because it never polls, stream=True ignores on_status, timeout and poll_interval.

For the raw events, including the final one, subscribe to an existing prediction:

pred = client.predictions.create("acme/storyteller", {"prompt": "…"})
for event in client.predictions.stream_output(pred.id):
if "seq" in event:
print(event["output"], end="") # a chunk
else:
print(event["status"], event["error"]) # the final event
Terminal window
curl -N -H "Authorization: Bearer tk_…" \
https://api.turion.ai/v1/predictions/<prediction-id>/output/stream
data: {"seq": 0, "output": "Silicon "}
data: {"seq": 1, "output": "rivers"}
data: {"status": "succeeded", "output": "Silicon rivers", "error": null}
data: [DONE]

Each data: line is one JSON event. Chunk events carry seq (from 0, with no gaps) and output (what the model yielded). The final event carries status, the assembled output and error. The stream then ends with data: [DONE].

Sync mode can return the same stream on the submit call: POST /v1/run/{owner}/{model} with Accept: text/event-stream.

Every subscriber starts from seq 0. You get the chunks produced so far, then the live tail, so subscribing late or reconnecting never loses the beginning. Deduplicate or resume on seq.

Chunks are kept for one hour after the prediction finishes. The assembled output is not affected: GET /v1/predictions/{id} keeps returning it. Subscribing after the chunks are gone yields only the final event and [DONE].

Do not print the final output twice. The final event repeats the assembled output for clients that did not read the chunks. client.run(stream=True) drops it when it has already yielded chunks, with one exception: if the relay broke mid-run and the chunks seen are a strict prefix of a final string output, it yields the missing suffix so the answer is not truncated. The final event is always the source of truth.

A stream lasts at most 10 minutes. If it ends without a final event, because of that limit or a dropped connection, the SDK raises TurionError("stream_incomplete", …) instead of returning a partial answer. The prediction keeps running; read it with GET /v1/predictions/{id} or subscribe again.

Failures arrive in the stream. A failed or canceled prediction still sends a final event with error; the SDK raises prediction_failed or prediction_canceled after the chunks that did arrive.

File chunks carry the final URLs. A file the model yields is uploaded before its chunk is relayed, so the chunk holds the same https:// URL as the final event and GET /v1/predictions/{id}. See Files & outputs.

Item boundaries are kept, except for ConcatenateIterator. For an Iterator[…] model, one yielded item is one chunk event. An item larger than 64 KiB cannot be relayed and ends the stream early; the final event still carries the full output. A ConcatenateIterator[str] item larger than 64 KiB is split across consecutive seqs, which makes no difference once you concatenate.

Nodes connect out, never in. A node sends chunks to Turion over its existing outbound connection, and Turion serves them to you. Nothing connects into a node, including nodes in your own fleet, and a slow reader cannot stall the model.

  • Sync mode: one round-trip results, and the SSE branch of /v1/run.
  • Webhooks & live status: the status stream (/stream), which reports status changes rather than output.
  • REST API: the reference for GET /v1/predictions/{id}/output/stream.