Open History to see the last commit
1---
2id: chat
3title: chat
4---
5
6```ts
7function chat<TAdapter, TSchema, TStream, TTools, TInterrupts, TContext, TMiddleware, TAgents>(options): TextActivityResult<TSchema, TStream, TTools>;
8```
9
10Defined in: [packages/ai/src/activities/chat/index.ts:4949](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/index.ts#L4949)
11
12Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.
13
14This activity supports four modes:
151. **Streaming agentic text**: Stream responses with automatic tool execution
162. **Streaming one-shot text**: Simple streaming request/response without tools
173. **Non-streaming text**: Returns collected text as a string (stream: false)
184. **Agentic structured output**: Run tools, then return structured data
19
20## Type Parameters
21
22### TAdapter
23
24`TAdapter` *extends* [`AnyTextAdapter`](../type-aliases/AnyTextAdapter.md)
25
26### TSchema
27
28`TSchema` *extends* [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined` = `undefined`
29
30### TStream
31
32`TStream` *extends* `boolean` = `boolean`
33
34### TTools
35
36`TTools` *extends*
37 \| readonly (
38 \| `Omit`\<[`Tool`](../interfaces/Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
39 \| [`ProviderTool`](../interfaces/ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
40 \| `undefined` =
41 \| readonly (
42 \| `Omit`\<[`Tool`](../interfaces/Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
43 \| [`ProviderTool`](../interfaces/ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
44 \| `undefined`
45
46### TInterrupts
47
48`TInterrupts` *extends* readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[] = \[\]
49
50### TContext
51
52`TContext` = `unknown`
53
54### TMiddleware
55
56`TMiddleware` *extends* `unknown`[] \| `undefined` = `undefined`
57
58### TAgents
59
60`TAgents` *extends* readonly [`DefinedAgent`](../interfaces/DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[]\>[] = readonly [`DefinedAgent`](../interfaces/DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](../interfaces/InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[]\>[]
61
62## Parameters
63
64### options
65
66`TextActivityOptionsWithContext`\<`TAdapter`, `TSchema`, `TStream`, `TTools`, `TInterrupts`, `TContext`, `TMiddleware`, `TAgents`\>
67
68## Returns
69
70`TextActivityResult`\<`TSchema`, `TStream`, `TTools`\>
71
72## Examples
73
74**Full agentic text (streaming with tools)**
75
76```ts
77import { chat } from '@tanstack/ai'
78import { openaiText } from '@tanstack/ai-openai'
79
80for await (const chunk of chat({
81 adapter: openaiText('gpt-5.5'),
82 messages: [{ role: 'user', content: 'What is the weather?' }],
83 tools: [weatherTool]
84})) {
85 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
86 console.log(chunk.delta)
87 }
88}
89```
90
91**One-shot text (streaming without tools)**
92
93```ts
94for await (const chunk of chat({
95 adapter: openaiText('gpt-5.5'),
96 messages: [{ role: 'user', content: 'Hello!' }]
97})) {
98 console.log(chunk)
99}
100```
101
102**Non-streaming text (stream: false)**
103
104```ts
105const text = await chat({
106 adapter: openaiText('gpt-5.5'),
107 messages: [{ role: 'user', content: 'Hello!' }],
108 stream: false
109})
110// text is a string with the full response
111```
112
113**Agentic structured output (tools + structured response)**
114
115```ts
116import { z } from 'zod'
117
118const result = await chat({
119 adapter: openaiText('gpt-5.5'),
120 messages: [{ role: 'user', content: 'Research and summarize the topic' }],
121 tools: [researchTool, analyzeTool],
122 outputSchema: z.object({
123 summary: z.string(),
124 keyPoints: z.array(z.string())
125 })
126})
127// result is { summary: string, keyPoints: string[] }
128```