Effect 4 bindings for TanStack AI. Each function takes the options of the core function and returns an Effect Effect or Stream. For a walkthrough, start with Effect.
npm i @tanstack/ai @tanstack/ai-effect effectpnpm add @tanstack/ai @tanstack/ai-effect effectyarn add @tanstack/ai @tanstack/ai-effect effectbun add @tanstack/ai @tanstack/ai-effect effectPeer dependencies: @tanstack/ai and effect 4.1 or later.
Runs core chat() as an Effect. See Effect.
import * as AI from '@tanstack/ai-effect'
import { openaiText } from '@tanstack/ai-openai'
const stream = AI.chat({
adapter: openaiText('gpt-6.1-sol'),
messages: [{ role: 'user', content: 'Hello' }],
threadId: 'hello',
})The options of core chat, with two changes:
| Options | Returns |
|---|---|
| Default | Stream.Stream<StreamChunk, AIError, R> |
| stream: false | Effect.Effect<ChatResult, AIError, R>. ChatResult is { text, chunks }. |
| outputSchema | Effect.Effect<T, AIError, R>. T is the type of the schema. |
| outputSchema and stream: true | Stream.Stream<StreamChunk, AIError, R> |
R is the union of the services of the serverTool values in tools. With no serverTool, R is never.
Makes a server tool from a tool definition and a function that returns an Effect. See Effect tools.
function serverTool<TDef, E, R>(
definition: TDef,
execute: (
input: InferSchemaType<TDef['inputSchema']>,
context: ToolExecutionContext,
) => Effect.Effect<InferSchemaType<TDef['outputSchema']>, E, R>,
): EffectServerTool<TDef, R>A failure of the Effect goes to the model as a tool error. The run continues. If the run aborts, the tool fiber is interrupted.
Turns an Effect Schema into an inputSchema or outputSchema for toolDefinition().
function toolSchema<S extends Schema.Constraint>(
schema: S,
): StandardJSONSchemaV1<S['Encoded'], S['Type']>The type that serverTool returns. Pass it in the tools of chat.
| Field | Type |
|---|---|
| _tag | 'EffectServerTool' |
| def | TDef |
| execute | (input, ctx) => Effect.Effect<Output, unknown, R> |
The error of every function in this package. It wraps a throw or a rejection from TanStack AI.
class AIError extends Data.TaggedError('AIError')<{
readonly message: string
readonly cause: unknown
}> {}A RUN_ERROR chunk does not fail a Stream. It is a normal chunk. See Effect activities.
Each activity takes the options of its core function, without abortSignal. It returns Effect.Effect<Result, AIError>. With stream: true, it returns Stream.Stream<StreamChunk, AIError>.
| Function | Core function | Supports stream: true |
|---|---|---|
| summarize | summarize | Yes |
| generateImage | generateImage | Yes |
| generateAudio | generateAudio | Yes |
| generateSpeech | generateSpeech | Yes |
| generateVoice | generateVoice | Yes |
| generateTranscription | generateTranscription | Yes |
| generateVideo | generateVideo | Yes |
| generateWorld | generateWorld | Yes |
| generateLiveVideo | generateLiveVideo | Yes |
| getVideoJobStatus | getVideoJobStatus | No |
| embed | embed | No |
| rerank | rerank | No |
| decide | decide | No |
| listVoices | listVoices | No |
| uploadFile | uploadFile | No |
| getFile | getFile | No |
| deleteFile | deleteFile | No |
For the core options and results, see @tanstack/ai. For examples, see Effect activities.