For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
DASHBOARDPLAYGROUNDDOCSCOMMUNITYLOG IN
Guides and conceptsAPI ReferenceRelease NotesLLMUCookbooks
Guides and conceptsAPI ReferenceRelease NotesLLMUCookbooks
  • Cohere API
    • About
    • Teams and Roles
    • Errors
    • Migrating From API v1 to API v2
    • Using the OpenAI SDK
  • Endpoints
      • POSTRerank API (v2)
  • Deprecated
LogoLogodocs
DASHBOARDPLAYGROUNDDOCSCOMMUNITYLOG IN
Endpointsv2/rerank

Rerank API (v2)

POST
https://api.cohere.com/v2/rerank
POST
/v2/rerank
1import cohere
2
3co = cohere.ClientV2()
4
5docs = [
6 "Carson City is the capital city of the American state of Nevada.",
7 "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
8 "Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
9 "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
10 "Capital punishment has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.",
11]
12
13response = co.rerank(
14 model="rerank-v4.0-pro",
15 query="What is the capital of the United States?",
16 documents=docs,
17 top_n=3,
18)
19print(response)
1{
2 "results": [
3 {
4 "index": 3,
5 "relevance_score": 0.999071
6 },
7 {
8 "index": 4,
9 "relevance_score": 0.7867867
10 },
11 {
12 "index": 0,
13 "relevance_score": 0.32713068
14 }
15 ],
16 "id": "07734bd2-2473-4f07-94e1-0d9f0e6843cf",
17 "meta": {
18 "api_version": {
19 "version": "2",
20 "is_experimental": false
21 },
22 "billed_units": {
23 "search_units": 1
24 }
25 }
26}
This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
Was this page helpful?
Previous

Embed API (v2)

Next
Built with

Authentication

AuthorizationBearer

Bearer authentication of the form Bearer <token>, where token is your auth token.

Headers

X-Client-NamestringOptional
The name of the project that is making the request.

Request

modelstringRequired

The identifier of the model to use, eg rerank-v3.5.

querystringRequired
The search query
documentslist of stringsRequired
A list of texts that will be compared to the `query`. For optimal performance we recommend against sending more than 1,000 documents in a single request. **Note**: long documents will automatically be truncated to the value of `max_tokens_per_doc`. **Note**: structured data should be formatted as YAML strings for best performance.
top_nintegerOptional>=1
Limits the number of returned rerank results to the specified value. If not passed, all the rerank results will be returned.
max_tokens_per_docintegerOptional

Defaults to 4096. Long documents will be automatically truncated to the specified number of tokens.

priorityintegerOptional0-999Defaults to 0

Controls how early the request is handled. Lower numbers indicate higher priority (default: 0, the highest). When the system is under load, higher-priority requests are processed first and are the least likely to be dropped.

Response

OK
resultslist of objects
An ordered list of ranked documents
idstring
metaobject

Errors

400
Bad Request Error
401
Unauthorized Error
403
Forbidden Error
404
Not Found Error
422
Unprocessable Entity Error
429
Too Many Requests Error
498
Invalid Token Error
499
Client Closed Request Error
500
Internal Server Error
501
Not Implemented Error
503
Service Unavailable Error
504
Gateway Timeout Error

A list of texts that will be compared to the query. For optimal performance we recommend against sending more than 1,000 documents in a single request.

Note: long documents will automatically be truncated to the value of max_tokens_per_doc.

Note: structured data should be formatted as YAML strings for best performance.