Open History to see the last commit
1---
2title: Cohere
3id: cohere-adapter
4order: 11
5description: "Use Cohere with TanStack AI via @tanstack/ai-cohere: embed-v4.0 multimodal embeddings for semantic search and RAG, plus rerank models for relevance."
6keywords:
7 - tanstack ai
8 - cohere
9 - embed-v4
10 - embeddings
11 - multimodal embeddings
12 - semantic search
13 - rerank
14 - reranking
15 - retrieval
16 - adapter
17---
18
19The Cohere adapter covers the two retrieval steps of a RAG pipeline:
20
21- **Embeddings** (`cohereEmbedding`): turn text, images, and fused text+image inputs into vectors with `embed()`.
22- **Reranking** (`cohereRerank`): reorder candidate documents by relevance to a query with `rerank()`.
23
24It does not support `chat()`, `summarize()`, or media generation. Use OpenAI, Anthropic, or Gemini for those. The adapter talks to Cohere's HTTP API directly over `fetch`, with no SDK dependency.
25
26## Installation
27
28<!-- ::start:tabs variant="package-manager" mode="install" -->
29
30react: @tanstack/ai @tanstack/ai-cohere
31vue: @tanstack/ai @tanstack/ai-cohere
32solid: @tanstack/ai @tanstack/ai-cohere
33svelte: @tanstack/ai @tanstack/ai-cohere
34preact: @tanstack/ai @tanstack/ai-cohere
35angular: @tanstack/ai @tanstack/ai-cohere
36vanilla: @tanstack/ai @tanstack/ai-cohere
37octane: @tanstack/ai @tanstack/ai-cohere
38
39<!-- ::end:tabs -->
40
41## Embeddings
42
43```typescript
44import { embed } from "@tanstack/ai";
45import { cohereEmbedding } from "@tanstack/ai-cohere";
46
47const result = await embed({
48 adapter: cohereEmbedding("embed-v4.0"),
49 input: ["a red guitar", "a blue drum kit"],
50 modelOptions: { inputType: "search_document" },
51});
52
53console.log(result.embeddings[0]?.vector);
54console.log(result.usage?.promptTokens);
55```
56
57`inputType` is required by Cohere's API. Use `search_document` at index time and `search_query` at query time (or `classification` / `clustering` for those workloads). TanStack AI enforces this at the type level, so `modelOptions` is required for Cohere embedding calls.
58
59### Multimodal Embeddings
60
61embed-v4.0 embeds images alongside text. An image part produces an image vector. A nested array of parts (`[textPart, imagePart]`) fuses text and image into one vector, which suits product catalogs and screenshot search. The outer array is the item list, so nest to fuse:
62
63```typescript
64import { embed } from "@tanstack/ai";
65import { cohereEmbedding } from "@tanstack/ai-cohere";
66
67const productPhoto = "iVBORw0KGgo..."; // base64 image data
68
69const result = await embed({
70 adapter: cohereEmbedding("embed-v4.0"),
71 input: [
72 {
73 type: "image",
74 source: {
75 type: "data",
76 value: productPhoto,
77 mimeType: "image/png",
78 },
79 },
80 // A nested array fuses its parts into a single vector.
81 [
82 { type: "text", content: "Fender Stratocaster, sunburst finish" },
83 {
84 type: "image",
85 source: {
86 type: "data",
87 value: productPhoto,
88 mimeType: "image/png",
89 },
90 },
91 ],
92 ],
93 modelOptions: { inputType: "search_document" },
94});
95
96console.log(result.embeddings.length); // 2
97```
98
99Cohere's API does not fetch remote image URLs. Pass base64 data (or a `data:` URI), or opt into adapter-side downloading:
100
101```typescript
102import { embed } from "@tanstack/ai";
103import { cohereEmbedding } from "@tanstack/ai-cohere";
104
105const adapter = cohereEmbedding("embed-v4.0", { allowUrlFetch: true });
106
107const result = await embed({
108 adapter,
109 input: {
110 type: "image",
111 source: { type: "url", value: "https://example.com/guitar.png" },
112 },
113 modelOptions: { inputType: "search_document" },
114});
115```
116
117### Requesting Dimensions
118
119embed-v4.0 supports Matryoshka output dimensions via the top-level `dimensions` option:
120
121```typescript
122import { embed } from "@tanstack/ai";
123import { cohereEmbedding } from "@tanstack/ai-cohere";
124
125const result = await embed({
126 adapter: cohereEmbedding("embed-v4.0"),
127 input: "a red guitar",
128 dimensions: 1024, // 256 | 512 | 1024 | 1536
129 modelOptions: { inputType: "search_document" },
130});
131```
132
133## Reranking
134
135```typescript
136import { rerank } from "@tanstack/ai";
137import { cohereRerank } from "@tanstack/ai-cohere";
138
139const { rerankedDocuments } = await rerank({
140 adapter: cohereRerank("rerank-v3.5"),
141 query: "talk about rain",
142 documents: ["sunny day at the beach", "rainy afternoon in the city"],
143});
144
145console.log(rerankedDocuments[0]); // 'rainy afternoon in the city'
146```
147
148For the full reranking guide, with object documents, RAG pipelines, options, and the result shape, see [Reranking](../rerank/rerank).
149
150Per-request rerank options go on `modelOptions`:
151
152```typescript
153import { rerank } from "@tanstack/ai";
154import { cohereRerank } from "@tanstack/ai-cohere";
155
156const { ranking } = await rerank({
157 adapter: cohereRerank("rerank-v3.5"),
158 query: "refund policy",
159 documents: ["Returns accepted within 30 days.", "Free shipping over $50."],
160 modelOptions: {
161 maxTokensPerDoc: 512, // Cap tokens kept per document (Cohere default: 4096)
162 },
163});
164
165console.log(ranking);
166```
167
168## Models
169
170| Model | Capability | Description |
171| -------------------------- | ---------- | ----------------------------------------------------------------- |
172| `embed-v4.0` | Embeddings | Multimodal (text + images), Matryoshka `dimensions` support |
173| `rerank-v3.5` | Reranking | Latest multilingual reranker (recommended) |
174| `rerank-english-v3.0` | Reranking | English-optimized reranker |
175| `rerank-multilingual-v3.0` | Reranking | Multilingual reranker |
176
177## Environment Variables
178
179Both adapters read your API key from the environment:
180
181```bash
182COHERE_API_KEY=your-cohere-api-key
183```
184
185| Variable | Required | Description |
186| ---------------- | -------- | ------------------- |
187| `COHERE_API_KEY` | Yes | Your Cohere API key |
188
189Get a key from the [Cohere dashboard](https://dashboard.cohere.com/api-keys).
190
191## Explicit API Keys
192
193To pass a key directly instead of reading the environment, use the `create*` factories:
194
195```typescript
196import {
197 createCohereEmbedding,
198 createCohereRerank,
199} from "@tanstack/ai-cohere";
200
201const embedAdapter = createCohereEmbedding(
202 "embed-v4.0",
203 process.env.MY_COHERE_KEY!,
204);
205const rerankAdapter = createCohereRerank("rerank-v3.5", "your-cohere-api-key");
206```
207
208## Behind a proxy
209
210Route every request through a gateway, such as Cloudflare AI Gateway or a corporate proxy, with `baseURL` and `defaultHeaders`. These two option names are the same on every TanStack AI adapter, so one gateway config works for all of them.
211
212```typescript
213import { createCohereEmbedding } from "@tanstack/ai-cohere";
214
215const adapter = createCohereEmbedding("embed-v4.0", process.env.COHERE_API_KEY!, {
216 baseURL: "https://gateway.example.com/cohere",
217 defaultHeaders: { "cf-aig-authorization": `Bearer ${process.env.GATEWAY_TOKEN}` },
218});
219```
220
221`baseUrl` and `headers` are aliases of the same two options. If you set both forms, `baseURL` and `defaultHeaders` win.
222
223## API Reference
224
225### `cohereEmbedding(model, config?)`
226
227Creates an embedding adapter using `COHERE_API_KEY` from the environment.
228
229- `model`: `"embed-v4.0"`
230- `config.baseURL`: override the API base URL (default `https://api.cohere.com`). `baseUrl` is an alias.
231- `config.defaultHeaders`: extra request headers. `headers` is an alias.
232- `config.allowUrlFetch`: download `http(s)` image URLs and inline them as base64 (default `false`)
233
234### `createCohereEmbedding(model, apiKey, config?)`
235
236Same as `cohereEmbedding` with an explicit API key.
237
238### `cohereRerank(model, config?)`
239
240Creates a rerank adapter using `COHERE_API_KEY` from the environment.
241
242- `model`: one of the rerank models above
243- `config.baseURL`: override the API base URL (default `https://api.cohere.com`). `baseUrl` is an alias.
244- `config.defaultHeaders`: extra request headers. `headers` is an alias.
245
246### `createCohereRerank(model, apiKey, config?)`
247
248Same as `cohereRerank` with an explicit API key.
249
250## Next Steps
251
252- [Embeddings guide](../embeddings.md): the full `embed()` API
253- [Reranking guide](../rerank/rerank): full walkthrough including RAG pipelines
254- [Generation Hooks](../media/generation-hooks.md): usage and lifecycle middleware