Proposing Native Document Reranking Support for langchain-openrouter
Target Namespace: libs/partners/openrouter
Base Interface Class: langchain_core.documents.BaseDocumentCompressor
Use Case
OpenRouter natively supports document reranking and context compression endpoints. Currently, developers building RAG architectures completely within the OpenRouter ecosystem are forced to split their integration pipelines—relying on langchain-openrouter for token generation, but dropping down to external third-party cross-encoder wrappers or generic OpenAI hacks just to handle document reranking.
Integrating a native reranker class directly into the existing langchain-openrouter partner package will unify dependency tracking and streamline context compression in retrieval chains.
Proposed API Interface
I suggest implementing a new OpenRouterRerank class inside a rerank.py module within libs/partners/openrouter/langchain_openrouter/:
from typing import Any, Sequence
from langchain_core.documents import BaseDocumentCompressor, Document
from pydantic import ConfigDict
class OpenRouterRerank(BaseDocumentCompressor):
"""OpenRouter document reranker integration."""
model: str
"""Model name to use for reranking."""
model_config = ConfigDict(
extra="forbid",
arbitrary_types_allowed=True,
)
def compress_documents(
self,
documents: Sequence[Document],
query: str,
callbacks: Any = None,
) -> Sequence[Document]:
"""Compress/rerank documents using the OpenRouter API."""
# Mapping logic to OpenRouter's native rerank payload specs
pass