Open History to see the last commit
1---
2title: Embeddings
3id: embeddings
4order: 1
5description: "Generate text and multimodal embedding vectors with OpenAI, Cohere, Gemini, Mistral, Amazon Bedrock, Ollama, and Vercel AI Gateway via TanStack AI's embed() API."
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---
22
23TanStack AI provides embedding generation through dedicated embedding adapters that follow the same tree-shakeable, per-model-typed architecture as every other activity. The `embed()` function turns text — and, for multimodal models, images — into vectors for semantic search, RAG, clustering, and classification.
24
25## Overview
26
27Currently supported:
28
29- **OpenAI**: text-embedding-3-small, text-embedding-3-large (text)
30- **Cohere**: embed-v4.0 (text + image, fused multimodal)
31- **Google Gemini**: gemini-embedding-001 (text)
32- **Mistral**: mistral-embed, codestral-embed (text)
33- **Amazon Bedrock**: Titan Text Embeddings V2 (text), Titan Multimodal Embeddings G1 (text + image), Cohere Embed v3 on Bedrock (text)
34- **Ollama**: nomic-embed-text, mxbai-embed-large, and any local embedding model (text)
35- **Vercel AI Gateway**: OpenAI-compatible embedding models such as openai/text-embedding-3-small (text)
36
37## Basic Usage
38
39```typescript
40import { embed } from "@tanstack/ai";
41import { openaiEmbedding } from "@tanstack/ai-openai";
42
43const result = await embed({
44 adapter: openaiEmbedding("text-embedding-3-small"),
45 input: "a red guitar",
46});
47
48console.log(result.embeddings[0]?.vector); // number[]
49```
50
51Vercel AI Gateway uses the same `embed()` call. Pass a `creator/model` id such as `openai/text-embedding-3-small`:
52
53```typescript
54import { embed } from "@tanstack/ai";
55import { vercelGatewayEmbedding } from "@tanstack/ai-vercel-gateway";
56
57const result = await embed({
58 adapter: vercelGatewayEmbedding("openai/text-embedding-3-small"),
59 input: "a red guitar",
60});
61```
62
63`input` accepts a single item or an array of items; the result always carries an `embeddings` array with one vector per input item, in input order:
64
65```typescript
66import { embed } from "@tanstack/ai";
67import { openaiEmbedding } from "@tanstack/ai-openai";
68
69const result = await embed({
70 adapter: openaiEmbedding("text-embedding-3-large"),
71 input: ["a red guitar", "a blue drum kit", "a vintage synthesizer"],
72});
73
74for (const embedding of result.embeddings) {
75 console.log(embedding.index, embedding.vector.length);
76}
77```
78
79## Requesting Dimensions
80
81Models with configurable (Matryoshka) dimensions accept a top-level `dimensions` option:
82
83```typescript
84import { embed } from "@tanstack/ai";
85import { openaiEmbedding } from "@tanstack/ai-openai";
86
87const result = await embed({
88 adapter: openaiEmbedding("text-embedding-3-large"),
89 input: "a red guitar",
90 dimensions: 1024,
91});
92```
93
94Adapters for fixed-dimension models (for example `mistral-embed` or Ollama models) throw a clear runtime error when `dimensions` is set.
95
96## Multimodal Embeddings
97
98Multimodal models embed images — alone, or fused with text into a single vector. Image inputs reuse the same content-part shapes as chat messages, and the accepted item types are narrowed per model at compile time: passing an image to a text-only model is a type error.
99
100The top-level `input` array is always the list of items, and **each item produces exactly one vector**:
101
102- a string or text part embeds that text
103- an image part embeds that image
104- a **nested array of parts** (`[textPart, imagePart]`) fuses those parts into one vector — the same `Array<ContentPart>` shape a chat message's `content` uses
105
106Because a top-level array is the item list, fuse by nesting: `[textPart, imagePart]` is two vectors, while `[[textPart, imagePart]]` is one fused vector.
107
108```typescript
109import { embed } from "@tanstack/ai";
110import { cohereEmbedding } from "@tanstack/ai-cohere";
111
112const productPhoto = "iVBORw0KGgo..."; // base64 image data
113
114const result = await embed({
115 adapter: cohereEmbedding("embed-v4.0"),
116 input: [
117 "a red guitar",
118 {
119 type: "image",
120 source: {
121 type: "data",
122 value: productPhoto,
123 mimeType: "image/png",
124 },
125 },
126 // A nested array fuses its parts into a single vector.
127 [
128 { type: "text", content: "Fender Stratocaster, sunburst finish" },
129 {
130 type: "image",
131 source: {
132 type: "data",
133 value: productPhoto,
134 mimeType: "image/png",
135 },
136 },
137 ],
138 ],
139 modelOptions: { inputType: "search_document" },
140});
141
142console.log(result.embeddings.length); // 3 — one vector per input item
143```
144
145Amazon Titan Multimodal works the same way — nest the parts to fuse them into one vector:
146
147```typescript
148import { embed } from "@tanstack/ai";
149import { bedrockEmbedding } from "@tanstack/ai-bedrock";
150
151const productPhoto = "iVBORw0KGgo..."; // base64 image data
152
153const result = await embed({
154 adapter: bedrockEmbedding("amazon.titan-embed-image-v1"),
155 input: [
156 [
157 { type: "text", content: "a red guitar" },
158 {
159 type: "image",
160 source: {
161 type: "data",
162 value: productPhoto,
163 mimeType: "image/png",
164 },
165 },
166 ],
167 ],
168 dimensions: 1024,
169});
170```
171
172Adapters do not fetch remote image URLs by default — pass base64 data (or a `data:` URI). The Cohere adapter accepts an `allowUrlFetch` config option to opt into downloading `http(s)` image URLs on your behalf.
173
174## Search Documents vs. Queries
175
176Retrieval-tuned models embed documents and queries differently. Cohere requires an `inputType`, which TanStack AI enforces at the type level — `modelOptions` is required for models with required options:
177
178```typescript
179import { embed } from "@tanstack/ai";
180import { cohereEmbedding } from "@tanstack/ai-cohere";
181
182// Index time: embed documents
183const docs = await embed({
184 adapter: cohereEmbedding("embed-v4.0"),
185 input: ["doc one", "doc two"],
186 modelOptions: { inputType: "search_document" },
187});
188
189// Query time: embed the query
190const query = await embed({
191 adapter: cohereEmbedding("embed-v4.0"),
192 input: "which doc mentions one?",
193 modelOptions: { inputType: "search_query" },
194});
195```
196
197Gemini expresses the same idea through an optional `taskType`:
198
199```typescript
200import { embed } from "@tanstack/ai";
201import { geminiEmbedding } from "@tanstack/ai-gemini";
202
203const result = await embed({
204 adapter: geminiEmbedding("gemini-embedding-001"),
205 input: "a red guitar",
206 modelOptions: { taskType: "RETRIEVAL_DOCUMENT" },
207});
208```
209
210## Usage and Observability
211
212Adapters report token usage when the provider does, and `embed()` supports the same observe-only generation middleware as the media activities (see [Generation Hooks](./media/generation-hooks.md)):
213
214```typescript
215import { embed } from "@tanstack/ai";
216import { openaiEmbedding } from "@tanstack/ai-openai";
217
218const result = await embed({
219 adapter: openaiEmbedding("text-embedding-3-small"),
220 input: ["a red guitar", "a blue drum kit"],
221 middleware: [
222 {
223 name: "usage-logger",
224 onUsage: (ctx, usage) => {
225 console.log(`${ctx.model}: ${usage.promptTokens} tokens`);
226 },
227 },
228 ],
229});
230
231console.log(result.usage?.promptTokens);
232```
233
234## Error Handling
235
236`embed()` rejects with the provider error; middleware `onError` hooks run before the rejection propagates:
237
238```typescript
239import { embed } from "@tanstack/ai";
240import { openaiEmbedding } from "@tanstack/ai-openai";
241
242try {
243 await embed({
244 adapter: openaiEmbedding("text-embedding-3-small"),
245 input: "a red guitar",
246 });
247} catch (error) {
248 console.error("embedding failed", error);
249}
250```