Open History to see the last commit
1---
2title: Embeddings
3id: embeddings
4order: 1
5description: "Turn text and images into vectors with embed(). Use them for semantic search, RAG, and clustering with OpenAI, Cohere, Gemini, Mistral, Amazon Bedrock, Ollama, Vercel AI Gateway, or Neon AI Gateway."
6keywords:
7 - tanstack ai
8 - embeddings
9 - embedding vectors
10 - multimodal embeddings
11 - semantic search
12 - rag
13 - embed
14 - openai
15 - cohere
16 - gemini
17 - mistral
18 - bedrock
19 - ollama
20 - vercel ai gateway
21 - neon ai gateway
22---
23
24`embed()` turns text into a vector. Search, RAG, and clustering then compare those vectors.
25
26Multimodal models also embed images.
27
28## Quick setup
29
301. Install the core package and an adapter:
31
32<!-- ::start:tabs variant="package-manager" mode="install" -->
33
34react: @tanstack/ai @tanstack/ai-openai
35vue: @tanstack/ai @tanstack/ai-openai
36solid: @tanstack/ai @tanstack/ai-openai
37svelte: @tanstack/ai @tanstack/ai-openai
38preact: @tanstack/ai @tanstack/ai-openai
39angular: @tanstack/ai @tanstack/ai-openai
40vanilla: @tanstack/ai @tanstack/ai-openai
41octane: @tanstack/ai @tanstack/ai-openai
42
43<!-- ::end:tabs -->
44
452. Put your key on the server. The adapter reads it at runtime:
46
47```bash
48OPENAI_API_KEY=your-openai-api-key
49```
50
513. Call `embed()` on the server:
52
53```typescript
54import { embed } from "@tanstack/ai";
55import { openaiEmbedding } from "@tanstack/ai-openai";
56
57const result = await embed({
58 adapter: openaiEmbedding("text-embedding-3-small"),
59 input: ["a red guitar", "a blue drum kit", "a vintage synthesizer"],
60});
61
62for (const embedding of result.embeddings) {
63 console.log(embedding.index, embedding.vector.length);
64}
65```
66
67You now have one `number[]` vector per input string. Store them in your vector database.
68
69## How it works
70
71- `input` is one item or an array of items.
72- Each item gives exactly one vector.
73- `result.embeddings` keeps the input order. Each entry has an `index` and a `vector`.
74- Every provider uses the same `embed()` call. To change provider, change the adapter.
75
76## Providers
77
78| Provider | Adapter | Models | Input |
79| ----------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------ |
80| OpenAI | `openaiEmbedding` (`@tanstack/ai-openai`) | `text-embedding-3-small`, `text-embedding-3-large` | text |
81| Cohere | `cohereEmbedding` (`@tanstack/ai-cohere`) | `embed-v4.0` | text + image |
82| Google Gemini | `geminiEmbedding` (`@tanstack/ai-gemini`) | `gemini-embedding-001` | text |
83| Mistral | `mistralEmbedding` (`@tanstack/ai-mistral`) | `mistral-embed`, `codestral-embed` | text |
84| Amazon Bedrock | `bedrockEmbedding` (`@tanstack/ai-bedrock`) | Titan Text Embeddings V2 (text), Titan Multimodal Embeddings G1 (text + image), Cohere Embed v3 (text) | text, or text + image |
85| Ollama | `ollamaEmbedding` (`@tanstack/ai-ollama`) | `nomic-embed-text`, `mxbai-embed-large`, and any local embedding model | text |
86| Vercel AI Gateway | `vercelGatewayEmbedding` (`@tanstack/ai-vercel-gateway`) | OpenAI-compatible models such as `openai/text-embedding-3-small` | text |
87| Neon AI Gateway | `neonEmbedding` (`@tanstack/ai-neon`) | `qwen3-embedding-0-6b`, `gte-large-en`, `bge-large-en` (1024 dimensions) | text |
88
89Vercel AI Gateway takes a `creator/model` id:
90
91```typescript
92import { embed } from "@tanstack/ai";
93import { vercelGatewayEmbedding } from "@tanstack/ai-vercel-gateway";
94
95const result = await embed({
96 adapter: vercelGatewayEmbedding("openai/text-embedding-3-small"),
97 input: "a red guitar",
98});
99```
100
101Neon AI Gateway uses the same call. The vectors fit a `vector(1024)` pgvector column in your Neon branch:
102
103```typescript
104import { embed } from "@tanstack/ai";
105import { neonEmbedding } from "@tanstack/ai-neon";
106
107const result = await embed({
108 adapter: neonEmbedding("qwen3-embedding-0-6b"),
109 input: "a red guitar",
110});
111```
112
113## Pick the vector size
114
115Some models (Matryoshka models) let you choose the number of dimensions. Pass `dimensions`:
116
117```typescript
118import { embed } from "@tanstack/ai";
119import { openaiEmbedding } from "@tanstack/ai-openai";
120
121const result = await embed({
122 adapter: openaiEmbedding("text-embedding-3-large"),
123 input: "a red guitar",
124 dimensions: 1024,
125});
126```
127
128Fixed-size models (for example `mistral-embed` or Ollama models) throw a runtime error when you set `dimensions`.
129
130## Embed documents and queries differently
131
132Retrieval models give better results when they know if the text is a document or a query.
133
134Cohere requires an `inputType`. The types make `modelOptions` required for this model:
135
136```typescript
137import { embed } from "@tanstack/ai";
138import { cohereEmbedding } from "@tanstack/ai-cohere";
139
140// Index time: embed the documents
141const docs = await embed({
142 adapter: cohereEmbedding("embed-v4.0"),
143 input: ["doc one", "doc two"],
144 modelOptions: { inputType: "search_document" },
145});
146
147// Query time: embed the query
148const query = await embed({
149 adapter: cohereEmbedding("embed-v4.0"),
150 input: "which doc mentions one?",
151 modelOptions: { inputType: "search_query" },
152});
153```
154
155Gemini has an optional `taskType` for the same purpose:
156
157```typescript
158import { embed } from "@tanstack/ai";
159import { geminiEmbedding } from "@tanstack/ai-gemini";
160
161const result = await embed({
162 adapter: geminiEmbedding("gemini-embedding-001"),
163 input: "a red guitar",
164 modelOptions: { taskType: "RETRIEVAL_DOCUMENT" },
165});
166```
167
168## Embed images
169
170Multimodal models embed images, or an image and text together as one vector. Image items use the same content-part shape as chat messages.
171
172The types narrow the accepted items per model. An image sent to a text-only model is a type error.
173
174Each item in the top-level `input` array gives one vector:
175
176- A string or a text part embeds that text.
177- An image part embeds that image.
178- A nested array of parts (`[textPart, imagePart]`) fuses the parts into one vector.
179
180So `[textPart, imagePart]` gives two vectors, and `[[textPart, imagePart]]` gives one fused vector.
181
182```typescript
183import { embed } from "@tanstack/ai";
184import { cohereEmbedding } from "@tanstack/ai-cohere";
185
186const productPhoto = "iVBORw0KGgo..."; // base64 image data
187
188const result = await embed({
189 adapter: cohereEmbedding("embed-v4.0"),
190 input: [
191 "a red guitar",
192 {
193 type: "image",
194 source: {
195 type: "data",
196 value: productPhoto,
197 mimeType: "image/png",
198 },
199 },
200 // A nested array fuses its parts into one vector.
201 [
202 { type: "text", content: "Fender Stratocaster, sunburst finish" },
203 {
204 type: "image",
205 source: {
206 type: "data",
207 value: productPhoto,
208 mimeType: "image/png",
209 },
210 },
211 ],
212 ],
213 modelOptions: { inputType: "search_document" },
214});
215
216console.log(result.embeddings.length); // 3, one vector per input item
217```
218
219Amazon Titan Multimodal works the same way. Nest the parts to fuse them:
220
221```typescript
222import { embed } from "@tanstack/ai";
223import { bedrockEmbedding } from "@tanstack/ai-bedrock";
224
225const productPhoto = "iVBORw0KGgo..."; // base64 image data
226
227const result = await embed({
228 adapter: bedrockEmbedding("amazon.titan-embed-image-v1"),
229 input: [
230 [
231 { type: "text", content: "a red guitar" },
232 {
233 type: "image",
234 source: {
235 type: "data",
236 value: productPhoto,
237 mimeType: "image/png",
238 },
239 },
240 ],
241 ],
242 dimensions: 1024,
243});
244```
245
246Pass images as base64 data or as a `data:` URI. Adapters do not download remote image URLs by default.
247
248Need to rank the results after a vector search? See [Reranking](./rerank/rerank).
249
250## Advanced
251
252### Remote image URLs
253
254The Cohere adapter has an `allowUrlFetch` config option. When you turn it on, the adapter downloads `http(s)` image URLs for you.
255
256### Token usage and middleware
257
258Adapters report token usage when the provider does. `embed()` accepts the same observe-only generation middleware as the media activities. See [Generation Hooks](./media/generation-hooks.md).
259
260```typescript
261import { embed } from "@tanstack/ai";
262import { openaiEmbedding } from "@tanstack/ai-openai";
263
264const result = await embed({
265 adapter: openaiEmbedding("text-embedding-3-small"),
266 input: ["a red guitar", "a blue drum kit"],
267 middleware: [
268 {
269 name: "usage-logger",
270 onUsage: (ctx, usage) => {
271 console.log(`${ctx.model}: ${usage.promptTokens} tokens`);
272 },
273 },
274 ],
275});
276
277console.log(result.usage?.promptTokens);
278```
279
280### Errors
281
282`embed()` rejects with the provider error. Middleware `onError` hooks run before the rejection reaches your code.
283
284```typescript
285import { embed } from "@tanstack/ai";
286import { openaiEmbedding } from "@tanstack/ai-openai";
287
288try {
289 await embed({
290 adapter: openaiEmbedding("text-embedding-3-small"),
291 input: "a red guitar",
292 });
293} catch (error) {
294 console.error("embedding failed", error);
295}
296```