Jangada AIJangada AI

Streaming

Receive incremental tokens with stream() (sync) or astream() (async).

for token in llm.stream("Tell me about {{x}}", x="João Pessoa"):
    print(token, end="")
async for token in llm.astream("..."):   # e.g.: FastAPI StreamingResponse
    ...

Streaming with tools, tokens and cost (stream_complete)

stream()/astream() only deliver text. In an agent — the model writes and, midway, asks for tools — use stream_complete()/astream_complete(): text events while the answer is generated and, at the end, a done event with the full Completion, same as complete (tool_calls, usage, cost, finish_reason).

from jangada_ai import LLM

llm = LLM("anthropic", "claude-sonnet-5-5")

async for ev in llm.astream_complete("Create the welcome file", tools=[write_file]):
    if ev.type == "text":
        print(ev.text, end="", flush=True)      # shows up while the model writes
    else:  # "done"
        comp = ev.completion
        for call in comp.tool_calls:             # requested tools, as in complete
            ...
        print("\ncost:", comp.cost)
  • Takes the same arguments as complete (tools=, tool_choice=, mcp_servers=, history=, files=, params=…).
  • Real streaming on Anthropic, OpenAI, Azure and DeepSeek. On the others (and on OpenAI's Responses API) it is the regular call and the text arrives in a single chunk, followed by done — no cost or tool call is lost.
  • Retry and fallback before the first event, as in stream. The done is reported to observability with the streaming capability.
  • Output guardrails and cache don't apply (the text is already out).

Retry and fallback in streaming

Retry and fallback happen before the first token: if opening the stream fails with a transient error, jangada retries (backoff) and, if needed, falls back to the next candidate — all before you receive any content. Once the first token is out, the stream runs to the end.

See Retry and fallback for the full policy.

Notes

  • stream()/astream() accept the same system=, history=, images=, files=, and params= as the other calls.
  • For cost and tokens use the non-stream calls (complete/parse), which return usage/cost in the response — see Cost and tokens.

What changed in 1.9.0

  • Chunks without content (e.g. Azure's first content-filter chunk) no longer break the stream, and the connection is closed if you stop consuming the iterator halfway.
  • After the stream, the provider keeps usage and finish_reason in llm.provider.last_stream (OpenAI, Azure, DeepSeek, Mistral, Ollama). It is per instance: with several threads sharing the same LLM, read it right after the stream.
  • The stream is reported to observability at the end.
  • With native tools, streaming emits only the text (Anthropic/Gemini/OpenAI); on Mistral and Ollama, streaming + native tools raises UnsupportedError.

Example

examples/async_example.py — executable script.

On this page