Open History to see the last commit
1---
2title: Cohere
3id: cohere-adapter
4order: 11
5description: "Use Cohere in TanStack AI with @tanstack/ai-cohere: embed-v4.0 multimodal embeddings for semantic search and RAG, and rerank models to sort results by 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 embeds text and images, and it reranks documents for a query. This package has no `chat()`, `summarize()`, or media generation.
20
21For those, use [OpenAI](./openai), [Anthropic](./anthropic), or [Gemini](./gemini). The adapter calls the HTTP API of Cohere with `fetch`, with no SDK dependency.
22
23## 1. Install
24
25<!-- ::start:tabs variant="package-manager" mode="install" -->
26
27react: @tanstack/ai @tanstack/ai-cohere
28vue: @tanstack/ai @tanstack/ai-cohere
29solid: @tanstack/ai @tanstack/ai-cohere
30svelte: @tanstack/ai @tanstack/ai-cohere
31preact: @tanstack/ai @tanstack/ai-cohere
32angular: @tanstack/ai @tanstack/ai-cohere
33vanilla: @tanstack/ai @tanstack/ai-cohere
34octane: @tanstack/ai @tanstack/ai-cohere
35
36<!-- ::end:tabs -->
37
38## 2. Set the API key
39
40Get a key from the [Cohere dashboard](https://dashboard.cohere.com/api-keys). Put it in the environment of your server:
41
42```bash
43COHERE_API_KEY=your-cohere-api-key
44```
45
46`cohereEmbedding` and `cohereRerank` read `COHERE_API_KEY`. To pass the key in code, see [Explicit API keys](#explicit-api-keys).
47
48## 3. Embed documents
49
50```typescript
51import { embed } from "@tanstack/ai";
52import { cohereEmbedding } from "@tanstack/ai-cohere";
53
54const result = await embed({
55 adapter: cohereEmbedding("embed-v4.0"),
56 input: ["a red guitar", "a blue drum kit"],
57 modelOptions: { inputType: "search_document" },
58});
59
60console.log(result.embeddings[0]?.vector);
61console.log(result.usage?.promptTokens);
62```
63
64The Cohere API requires `inputType`, and the types require `modelOptions` on every Cohere embedding call:
65
66- `search_document`: when you index documents.
67- `search_query`: when you embed the search query.
68- `classification` or `clustering`: for those workloads.
69
70## 4. Rerank results
71
72```typescript
73import { rerank } from "@tanstack/ai";
74import { cohereRerank } from "@tanstack/ai-cohere";
75
76const { rerankedDocuments } = await rerank({
77 adapter: cohereRerank("rerank-v3.5"),
78 query: "talk about rain",
79 documents: ["sunny day at the beach", "rainy afternoon in the city"],
80});
81
82console.log(rerankedDocuments[0]); // 'rainy afternoon in the city'
83```
84
85You now have both retrieval steps. For object documents, RAG pipelines, the options, and the result shape, see [Reranking](../rerank/rerank).
86
87## Multimodal embeddings
88
89embed-v4.0 embeds images next to text:
90
91- An image part gives an image vector.
92- A nested array of parts (`[textPart, imagePart]`) joins text and image into one vector. Use it for product catalogs and screenshot search.
93
94The outer array is the list of items, so nest the parts to join them:
95
96```typescript
97import { embed } from "@tanstack/ai";
98import { cohereEmbedding } from "@tanstack/ai-cohere";
99
100const productPhoto = "iVBORw0KGgo..."; // base64 image data
101
102const result = await embed({
103 adapter: cohereEmbedding("embed-v4.0"),
104 input: [
105 {
106 type: "image",
107 source: {
108 type: "data",
109 value: productPhoto,
110 mimeType: "image/png",
111 },
112 },
113 // A nested array fuses its parts into a single vector.
114 [
115 { type: "text", content: "Fender Stratocaster, sunburst finish" },
116 {
117 type: "image",
118 source: {
119 type: "data",
120 value: productPhoto,
121 mimeType: "image/png",
122 },
123 },
124 ],
125 ],
126 modelOptions: { inputType: "search_document" },
127});
128
129console.log(result.embeddings.length); // 2
130```
131
132The Cohere API does not fetch remote image URLs. Pass base64 data (or a `data:` URI), or turn on [downloads in the adapter](#image-urls).
133
134## Shorter vectors
135
136embed-v4.0 takes the top-level `dimensions` option (`256`, `512`, `1024`, or `1536`):
137
138```typescript
139import { embed } from "@tanstack/ai";
140import { cohereEmbedding } from "@tanstack/ai-cohere";
141
142const result = await embed({
143 adapter: cohereEmbedding("embed-v4.0"),
144 input: "a red guitar",
145 dimensions: 1024, // 256 | 512 | 1024 | 1536
146 modelOptions: { inputType: "search_document" },
147});
148```
149
150## Rerank options
151
152Rerank options for one request go in `modelOptions`:
153
154```typescript
155import { rerank } from "@tanstack/ai";
156import { cohereRerank } from "@tanstack/ai-cohere";
157
158const { ranking } = await rerank({
159 adapter: cohereRerank("rerank-v3.5"),
160 query: "refund policy",
161 documents: ["Returns accepted within 30 days.", "Free shipping over $50."],
162 modelOptions: {
163 maxTokensPerDoc: 512, // Cap tokens kept per document (Cohere default: 4096)
164 },
165});
166
167console.log(ranking);
168```
169
170## Models
171
172| Model | Activity | Notes |
173| --- | --- | --- |
174| `embed-v4.0` | Embeddings | Text and images, `dimensions` support |
175| `rerank-v3.5` | Reranking | Newest multilingual reranker. Recommended |
176| `rerank-english-v3.0` | Reranking | Tuned for English |
177| `rerank-multilingual-v3.0` | Reranking | Multilingual reranker |
178
179## Advanced
180
181### Explicit API keys
182
183To pass a key in code, not from the environment, use the `create*` factories:
184
185```typescript
186import {
187 createCohereEmbedding,
188 createCohereRerank,
189} from "@tanstack/ai-cohere";
190
191const embedAdapter = createCohereEmbedding(
192 "embed-v4.0",
193 process.env.MY_COHERE_KEY!,
194);
195const rerankAdapter = createCohereRerank("rerank-v3.5", "your-cohere-api-key");
196```
197
198### Image URLs
199
200To let the adapter download `http(s)` image URLs and send them as base64, set `allowUrlFetch`:
201
202```typescript
203import { embed } from "@tanstack/ai";
204import { cohereEmbedding } from "@tanstack/ai-cohere";
205
206const adapter = cohereEmbedding("embed-v4.0", { allowUrlFetch: true });
207
208const result = await embed({
209 adapter,
210 input: {
211 type: "image",
212 source: { type: "url", value: "https://example.com/guitar.png" },
213 },
214 modelOptions: { inputType: "search_document" },
215});
216```
217
218### Behind a proxy
219
220Send 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.
221
222```typescript
223import { createCohereEmbedding } from "@tanstack/ai-cohere";
224
225const adapter = createCohereEmbedding("embed-v4.0", process.env.COHERE_API_KEY!, {
226 baseURL: "https://gateway.example.com/cohere",
227 defaultHeaders: { "cf-aig-authorization": `Bearer ${process.env.GATEWAY_TOKEN}` },
228});
229```
230
231`baseUrl` and `headers` are aliases of the same two options. If you set both forms, `baseURL` and `defaultHeaders` win.
232
233### API reference
234
235| Factory | Creates |
236| --- | --- |
237| `cohereEmbedding(model, config?)` | Embedding adapter. Reads `COHERE_API_KEY` |
238| `createCohereEmbedding(model, apiKey, config?)` | Embedding adapter with an explicit API key |
239| `cohereRerank(model, config?)` | Rerank adapter. Reads `COHERE_API_KEY` |
240| `createCohereRerank(model, apiKey, config?)` | Rerank adapter with an explicit API key |
241
242Config for both adapters:
243
244- `config.baseURL`: the API base URL (default `https://api.cohere.com`). `baseUrl` is an alias.
245- `config.defaultHeaders`: extra request headers. `headers` is an alias.
246- `config.allowUrlFetch` (embeddings only): download `http(s)` image URLs and send them as base64. Default `false`.
247
248For the full APIs, see the [Embeddings guide](../embeddings.md), the [Reranking guide](../rerank/rerank), and [Generation Hooks](../media/generation-hooks.md) for usage and lifecycle middleware.