Open History to see the last commit
1---
2title: Tree-Shaking
3id: tree-shaking
4order: 7
5description: "Import only the activities and adapters that you use. Your bundle then holds only the code for those features and providers."
6keywords:
7 - tanstack ai
8 - tree-shaking
9 - bundle size
10 - modular imports
11 - performance
12 - tree-shakeable
13---
14
15Import one adapter from its package subpath. The bundle then omits image generation and the adapters you do not call.
16
17TanStack AI exports each activity and each adapter as a separate named function. Import only the ones you use, and your bundler drops the rest.
18
19## Quick setup
20
211. Import the activity that you use from `@tanstack/ai`.
222. Import the one adapter that you use from the provider package.
23
24```ts
25import { chat } from '@tanstack/ai'
26import { openaiText } from '@tanstack/ai-openai'
27
28const stream = chat({
29 adapter: openaiText('gpt-6.1-sol'),
30 messages: [{ role: 'user', content: 'Hello!' }],
31 threadId: 'thread-1',
32})
33
34for await (const chunk of stream) {
35 console.log(chunk)
36}
37```
38
39Your bundle now holds:
40
41- The `chat` function, with its streaming and tool code.
42- The `openaiText` adapter.
43
44It does not hold `summarize`, `generateImage`, the other activities, or the adapters of other providers.
45
46## Use named imports
47
48Import each function by name. Some bundlers cannot drop the unused exports of a namespace import:
49
50```ts
51// Good: only what you use
52import { chat } from '@tanstack/ai'
53import { openaiText } from '@tanstack/ai-openai'
54```
55
56```ts
57// Avoid: a namespace import can pull in every export
58import * as ai from '@tanstack/ai'
59import * as openai from '@tanstack/ai-openai'
60```
61
62## Use more than one activity
63
64Import each activity and its adapter. Each one is independent:
65
66```ts
67import { chat, summarize } from '@tanstack/ai'
68import { openaiSummarize, openaiText } from '@tanstack/ai-openai'
69
70const stream = chat({
71 adapter: openaiText('gpt-6.1-sol'),
72 messages: [{ role: 'user', content: 'Hello!' }],
73 threadId: 'thread-1',
74})
75
76const summary = await summarize({
77 adapter: openaiSummarize('gpt-5.4-mini'),
78 text: 'Long text to summarize.',
79 threadId: 'thread-1',
80})
81```
82
83The `create*Options` helpers are separate exports too. See [Typed Pre-Configured Options](./typed-options).
84
85## Tips for smaller bundles
86
87- Use a specific adapter function, for example `openaiText`, for each activity.
88- Put different activities in different API routes.
89- Load code-split routes with dynamic imports.
90- In React Native and Expo, keep the chat screens client-only. Import `useChat` and the connection adapters there. Do not import provider SDKs, server response helpers, React DOM UI, devtools UI, or other framework packages. See [Quick Start: React Native](../getting-started/quick-start-react-native) for the server-only provider boundary and the mobile transport setup.
91
92## How it works
93
94- Each function is a named ES module export, not a default export.
95- Each activity and adapter lives in its own module.
96- `@tanstack/ai` declares `"sideEffects": false` in its `package.json`, so bundlers can drop modules that you do not import.
97- Type-only imports are removed at build time.
98
99Bundlers such as Vite, Webpack, Rollup, and esbuild follow the static imports and drop the code that you do not reference.
100
101## Advanced
102
103### Activity functions
104
105Each activity is a separate export of `@tanstack/ai`:
106
107| Function | Activity |
108|---|---|
109| `chat` | Chat and text generation |
110| `summarize` | Summarization |
111| `generateImage` | Image generation |
112| `generateSpeech` | Text-to-speech |
113| `generateTranscription` | Audio transcription |
114| `generateVideo` | Video generation |
115| `generateLiveVideo` | Live video sessions |
116| `generateWorld` | Live world sessions |
117
118### Adapter functions by provider
119
120Each provider package exports one function for each activity. Some examples:
121
122| Package | Adapters |
123|---|---|
124| `@tanstack/ai-openai` | `openaiText`, `openaiSummarize`, `openaiImage`, `openaiSpeech`, `openaiTranscription`, `openaiVideo`, `openaiEmbedding` |
125| `@tanstack/ai-anthropic` | `anthropicText`, `anthropicSummarize` |
126| `@tanstack/ai-gemini` | `geminiText`, `geminiSummarize`, `geminiImage`, `geminiSpeech` (experimental), `geminiEmbedding` |
127| `@tanstack/ai-ollama` | `ollamaText`, `ollamaSummarize`, `ollamaEmbedding` |
128
129Each adapter page lists the full set for its provider.
130
131### Adapter types
132
133Each adapter type has its own interface and a `kind` property:
134
135| Interface | Main method | `kind` |
136|---|---|---|
137| `TextAdapter` | `chatStream()` | `'text'` |
138| `SummarizeAdapter` | `summarize()` | `'summarize'` |
139| `ImageAdapter` | `generateImages()` | `'image'` |
140| `TTSAdapter` | `generateSpeech()` | `'tts'` |
141| `TranscriptionAdapter` | `transcribe()` | `'transcription'` |
142| `VideoAdapter` | `createVideoJob()`, `getVideoStatus()`, `getVideo()` | `'video'` |
143| `LiveVideoAdapter` | `createLiveVideo()` | `'liveVideo'` |
144| `WorldAdapter` | `createWorld()` | `'world'` |
145
146```ts
147import { openaiSummarize, openaiText } from '@tanstack/ai-openai'
148
149const chatAdapter = openaiText('gpt-6.1-sol')
150console.log(chatAdapter.kind) // 'text'
151
152const summarizeAdapter = openaiSummarize('gpt-5.4-mini')
153console.log(summarizeAdapter.kind) // 'summarize'
154```
155
156### Type safety
157
158Separate exports keep the full types. Each adapter knows the models of its provider:
159
160```ts ignore
161import { openaiText, type OpenAIChatModel } from '@tanstack/ai-openai'
162
163const adapter = openaiText('gpt-6.1-sol')
164
165const model: OpenAIChatModel = 'gpt-6.1-sol' // OK
166const model2: OpenAIChatModel = 'invalid' // Type error
167```