Skip to main content

Overview

stream_text is an async iterator for low-latency streaming text generation. It provides real-time access to tokens as they’re generated, with optional callbacks for chunk processing, error handling, and completion.

Basic usage

1

Kick off the request

2

Consume the async iterator

Alternatively call await stream_res.text() once to get the full text.

Parameters

Return value

stream_text returns a StreamTextResult with:
  • text_stream: Async iterator yielding text chunks
  • text(): Async method to get the complete text
  • usage: Token usage statistics
  • finish_reason: Why the stream ended
  • tool_calls: Tool calls if any were made

Examples

Basic streaming

With callbacks

With system instruction

With custom parameters

Tool-calling with streaming

See the dedicated Tool page for a complete walkthrough.

stream_text is provider-agnostic. Swap openai() for anthropic() or any other future implementation – no code changes required.