Implementation:Cohere ai Cohere python RerankResponseResultsItem Model
| Knowledge Sources | |
|---|---|
| Domains | SDK, Rerank |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
RerankResponseResultsItem is a Pydantic model representing an individual ranked result from the Cohere rerank API, containing the document's index, relevance score, and optionally the document itself.
Description
The RerankResponseResultsItem class extends UncheckedBaseModel and encapsulates a single item in the rerank response results list. Each item contains an index field that maps back to the position in the original input documents list, a relevance_score float normalized to the range [0, 1] indicating how relevant the document is to the query, and an optional document field of type RerankResponseResultsItemDocument that contains the document text when return_documents=True is set in the request. Results are typically returned sorted by relevance score in descending order.
Usage
Use RerankResponseResultsItem when processing results from the Cohere rerank API. This type is found in the results list of the rerank response. It enables you to identify which original documents are most relevant to a query, access their relevance scores for filtering or thresholding, and retrieve the document contents when requested.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/types/rerank_response_results_item.py
Signature
class RerankResponseResultsItem(UncheckedBaseModel):
document: typing.Optional[RerankResponseResultsItemDocument] = pydantic.Field(default=None)
index: int = pydantic.Field()
relevance_score: float = pydantic.Field()
Import
from cohere.types import RerankResponseResultsItem
I/O Contract
Fields
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
document |
Optional[RerankResponseResultsItemDocument] |
No | None |
The document content. Returns None if return_documents is set to False; returns the document passed in if True.
|
index |
int |
Yes | -- | Index in the original list of documents to which this ranked result corresponds. |
relevance_score |
float |
Yes | -- | Relevance score normalized to [0, 1]. Scores close to 1 indicate high relevance; scores close to 0 indicate low relevance. |
Usage Examples
import cohere
client = cohere.Client(api_key="YOUR_API_KEY")
# Rerank documents against a query
documents = [
"Carson City is the capital of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands.",
"Washington, D.C. is the capital of the United States.",
"Capital punishment has existed in the United States since before its founding.",
]
response = client.rerank(
model="rerank-english-v3.0",
query="What is the capital of the United States?",
documents=documents,
top_n=3,
return_documents=True,
)
# Process ranked results
for result in response.results:
print(f"Index: {result.index}")
print(f"Relevance score: {result.relevance_score:.4f}")
if result.document:
print(f"Document: {result.document.text}")
print("---")
# Filter results by relevance threshold
threshold = 0.5
relevant_results = [r for r in response.results if r.relevance_score >= threshold]
print(f"Documents above threshold: {len(relevant_results)}")