pi-ai streams LLM responses as typed events with distinct phases. Every call toDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/earendil-works/pi/llms.txt
Use this file to discover all available pages before exploring further.
stream() returns an AssistantMessageEventStream that emits events you can iterate with for await. Use these events to drive real-time UI updates, track partial tool arguments, or detect errors mid-stream.
Streaming vs. non-streaming functions
| Function | Returns | Use when |
|---|---|---|
stream(model, context, options?) | Async iterable of all event types | You need fine-grained control over every event |
streamSimple(model, context, options?) | Async iterable of all event types | You want the unified reasoning option instead of provider-specific options |
complete(model, context, options?) | Promise<AssistantMessage> | You don’t need incremental output |
completeSimple(model, context, options?) | Promise<AssistantMessage> | Same, with unified reasoning option |
stream and complete accept provider-specific options (e.g., thinkingEnabled for Anthropic, reasoningEffort for OpenAI). streamSimple and completeSimple accept a unified reasoning level ('minimal' | 'low' | 'medium' | 'high' | 'xhigh') that is mapped to the provider’s native format automatically.
Complete event reference
| Event type | Description | Key properties |
|---|---|---|
start | Stream begins | partial: initial empty AssistantMessage |
text_start | A text block starts | contentIndex: position in content array |
text_delta | Text chunk received | delta: new text; contentIndex |
text_end | Text block complete | content: full text; contentIndex |
thinking_start | A thinking block starts | contentIndex |
thinking_delta | Thinking chunk received | delta: new thinking text; contentIndex |
thinking_end | Thinking block complete | content: full thinking; contentIndex |
toolcall_start | A tool call begins | contentIndex |
toolcall_delta | Tool arguments streaming | delta: JSON chunk; partial.content[contentIndex].arguments: partial parsed args |
toolcall_end | Tool call complete | toolCall: complete ToolCall with id, name, arguments |
done | Stream complete | reason: "stop" | "length" | "toolUse"; message: final AssistantMessage |
error | Error or abort | reason: "error" | "aborted"; error: AssistantMessage with partial content |
Events for different content blocks are not guaranteed to be contiguous. A provider may emit text and tool call deltas interleaved in the same upstream chunk, so you may receive
text_delta, toolcall_delta, text_delta in sequence. Always use contentIndex to associate each delta with its block — do not assume a block’s start/delta/end sequence is uninterrupted.Stop reasons
EveryAssistantMessage (and every done event) carries a stopReason:
| Stop reason | Meaning |
|---|---|
"stop" | Normal completion — the model finished its response |
"length" | Output hit the maximum token limit |
"toolUse" | Model is calling tools and expects tool results before continuing |
"error" | An error occurred during generation |
"aborted" | Request was cancelled via AbortSignal |
Aborting requests
Pass anAbortSignal in the options to cancel an in-progress request. Aborted requests emit an error event with reason: 'aborted' and produce an AssistantMessage with stopReason: 'aborted'.
Continuing after abort
Aborted messages can be added to the conversation context and continued in a subsequent request:Thinking and reasoning
Models that expose areasoning property support thinking/reasoning mode. Passing reasoning options to a non-reasoning model is silently ignored.
- Unified interface (streamSimple)
- Provider-specific options (stream/complete)
- Streaming thinking content
Use
streamSimple or completeSimple with the reasoning option for a provider-agnostic interface:Error handling
When a request ends with an error, the stream emits anerror event. The partial AssistantMessage on event.error contains any content received before the failure.
Debugging with onPayload
Use theonPayload callback to inspect the exact request payload sent to the provider. This is useful for diagnosing request formatting issues or unexpected provider errors.
onPayload is supported by stream, complete, streamSimple, and completeSimple.