Overview
The Pi AI toolkit provides four main functions for generating assistant messages:stream()- Stream assistant messages with full event controlcomplete()- Get complete assistant message without streamingstreamSimple()- Stream with simplified reasoning optionscompleteSimple()- Complete with simplified reasoning options
stream()
Stream an assistant message with granular event handling.Model<TApi>
required
The model to use for generation. Get models via
getModel(provider, modelId).Context
required
The conversation context including system prompt, messages, and tools.
ProviderStreamOptions
Optional provider-specific streaming options.
AsyncIterable<AssistantMessageEvent>
An async iterable stream that emits events as the assistant message is generated.
Call
.result() to get the final AssistantMessage after streaming completes.Example
complete()
Get a complete assistant message without streaming.Model<TApi>
required
The model to use for generation.
Context
required
The conversation context.
ProviderStreamOptions
Same options as
stream().Promise<AssistantMessage>
The complete assistant message.
Example
streamSimple()
Stream with simplified reasoning/thinking options. Maps unifiedreasoning levels to provider-specific parameters.
SimpleStreamOptions
Extends
StreamOptions with reasoning support.Example
completeSimple()
Get complete response with simplified reasoning options.streamSimple() and complete().
Example
Context
TheContext interface represents a conversation’s state.
string
System-level instructions for the assistant.
Message[]
required
Conversation history. Can include
UserMessage, AssistantMessage, and ToolResultMessage.Tool[]
Available tools for the assistant to call. See tools documentation.
Context Serialization
Context objects are fully JSON-serializable:Events
TheAssistantMessageEventStream emits these event types:
{ type: 'start'; partial: AssistantMessage }
Stream begins. Contains initial message structure.
{ type: 'text_start'; contentIndex: number; partial: AssistantMessage }
Text block starts at the given content index.
{ type: 'text_delta'; contentIndex: number; delta: string; partial: AssistantMessage }
Text chunk received.
delta contains the new text.{ type: 'text_end'; contentIndex: number; content: string; partial: AssistantMessage }
Text block complete.
content contains the full text.{ type: 'thinking_start'; contentIndex: number; partial: AssistantMessage }
Thinking block starts (for models with reasoning capabilities).
{ type: 'thinking_delta'; contentIndex: number; delta: string; partial: AssistantMessage }
Thinking chunk received.
{ type: 'thinking_end'; contentIndex: number; content: string; partial: AssistantMessage }
Thinking block complete.
{ type: 'toolcall_start'; contentIndex: number; partial: AssistantMessage }
Tool call begins.
{ type: 'toolcall_delta'; contentIndex: number; delta: string; partial: AssistantMessage }
Tool arguments streaming.
partial.content[contentIndex].arguments contains partially parsed JSON.{ type: 'toolcall_end'; contentIndex: number; toolCall: ToolCall; partial: AssistantMessage }
Tool call complete.
toolCall contains the full parsed tool call.{ type: 'done'; reason: StopReason; message: AssistantMessage }
Stream complete successfully.
reason is "stop", "length", or "toolUse".{ type: 'error'; reason: 'error' | 'aborted'; error: AssistantMessage }
Error occurred.
error contains partial message and error details.Stop Reasons
EveryAssistantMessage has a stopReason field:
string
Normal completion - the model finished its response.
string
Output hit the maximum token limit.
string
Model is calling tools and expects tool results.
string
An error occurred during generation. Check
errorMessage field.string
Request was cancelled via
AbortSignal.Aborting Requests
UseAbortSignal to cancel in-progress requests: