Skip to main content

Overview

The Pi AI toolkit provides four main functions for generating assistant messages:
  • stream() - Stream assistant messages with full event control
  • complete() - Get complete assistant message without streaming
  • streamSimple() - Stream with simplified reasoning options
  • completeSimple() - 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 unified reasoning levels to provider-specific parameters.
SimpleStreamOptions
Extends StreamOptions with reasoning support.

Example

completeSimple()

Get complete response with simplified reasoning options.
Parameters and return type are the same as streamSimple() and complete().

Example

Context

The Context 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

The AssistantMessageEventStream 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.
Arguments may be incomplete during toolcall_delta. Always check for field existence.
{ 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

Every AssistantMessage 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

Use AbortSignal to cancel in-progress requests:
Aborted messages can be added to context and continued: