Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python RerankResponse Model

From Leeroopedia
Field Value
Type Implementation
Source Cohere Python SDK
Domain NLP Information Retrieval Response Parsing
Last Updated 2026-02-15
Implements Principle:Cohere_ai_Cohere_python_Rerank_Response_Processing

Overview

Concrete Pydantic models for rerank API responses with ranked results and relevance scores.

Description

V2RerankResponse and RerankResponse contain id, results (ordered list), and meta. RerankResponseResultsItem has index (int, position in original list), relevance_score (float, 0-1), and optional document field.

Code Reference

  • src/cohere/types/rerank_response.py Lines L1-27
  • src/cohere/types/rerank_response_results_item.py Lines L1-33
  • src/cohere/v2/types/v2rerank_response.py Lines L1-27

Signature

class RerankResponse(UncheckedBaseModel):
    id: typing.Optional[str] = None
    results: typing.List[RerankResponseResultsItem]
    meta: typing.Optional[ApiMeta] = None

class RerankResponseResultsItem(UncheckedBaseModel):
    document: typing.Optional[RerankResponseResultsItemDocument] = None
    index: int
    relevance_score: float

Import

from cohere.types import RerankResponse (accessed via client.rerank() return)

Inputs

Parameter Type Required Description
Raw API response JSON Yes The raw JSON response from the rerank API

Outputs

results (List[RerankResponseResultsItem]) ordered by relevance, each with index (int) and relevance_score (float).

Example

response = client.rerank(model="rerank-v4.0-pro", query="query", documents=docs, top_n=5)
for r in response.results:
    print(f"Doc[{r.index}]: score={r.relevance_score:.4f}, text={docs[r.index][:50]}...")

Related

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment