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:Langchain ai Langchain PerplexityRetriever

From Leeroopedia
Revision as of 11:24, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Langchain_ai_Langchain_PerplexityRetriever.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains LLM, Retrieval, Perplexity, Search
Last Updated 2026-02-11 00:00 GMT

Overview

A LangChain retriever that wraps the Perplexity Search API to return web search results as Document objects.

Description

PerplexitySearchRetriever is a concrete implementation of BaseRetriever in the langchain-perplexity partner package. It uses the Perplexity Search API client to perform web searches and returns results as LangChain Document objects with metadata including title, URL, date, and last_updated fields. The retriever supports configurable result limits, token budgets, country filtering, domain filtering, and date-based recency filtering.

Usage

Import this retriever when you need to integrate Perplexity web search into a LangChain retrieval pipeline, such as in a RAG (Retrieval-Augmented Generation) chain or an agent that needs real-time web search capabilities.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/perplexity/langchain_perplexity/retrievers.py
  • Lines: 1-68

Signature

class PerplexitySearchRetriever(BaseRetriever):
    """Perplexity Search retriever."""

    k: int = Field(default=10, description="Max results (1-20)")
    max_tokens: int = Field(default=25000, description="Max tokens across all results")
    max_tokens_per_page: int = Field(default=1024, description="Max tokens per page")
    country: str | None = Field(default=None, description="ISO country code")
    search_domain_filter: list[str] | None = Field(default=None, description="Domain filter (max 20)")
    search_recency_filter: Literal["day", "week", "month", "year"] | None = None
    search_after_date: str | None = Field(default=None, description="Date filter (format: %m/%d/%Y)")
    search_before_date: str | None = Field(default=None, description="Date filter (format: %m/%d/%Y)")
    client: Any = Field(default=None, exclude=True)
    pplx_api_key: SecretStr = Field(default=SecretStr(""))

    def _get_relevant_documents(
        self, query: str, *, run_manager: CallbackManagerForRetrieverRun
    ) -> list[Document]:
        ...

Import

from langchain_perplexity import PerplexitySearchRetriever

I/O Contract

Inputs (Constructor Parameters)

Name Type Required Description
k int No Maximum number of search results to return (1-20). Defaults to 10.
max_tokens int No Maximum tokens across all results. Defaults to 25000.
max_tokens_per_page int No Maximum tokens per page. Defaults to 1024.
country None No ISO country code for filtering results. Defaults to None.
search_domain_filter None No Domain filter list (max 20 domains). Defaults to None.
search_recency_filter None No Time-based recency filter. Defaults to None.
search_after_date None No Date filter in %m/%d/%Y format. Defaults to None.
search_before_date None No Date filter in %m/%d/%Y format. Defaults to None.
pplx_api_key SecretStr No Perplexity API key. Can also be set via environment variable.

Inputs (_get_relevant_documents)

Name Type Required Description
query str Yes The search query string.
run_manager CallbackManagerForRetrieverRun Yes Callback manager for the retriever run.

Outputs

Name Type Description
return list[Document] List of LangChain Document objects, each with page_content set to the result snippet and metadata containing title, url, date, and last_updated.

Usage Examples

Basic Usage

from langchain_perplexity import PerplexitySearchRetriever

retriever = PerplexitySearchRetriever(
    pplx_api_key="your-api-key",
    k=5,
)

docs = retriever.invoke("What is LangChain?")
for doc in docs:
    print(doc.page_content)
    print(doc.metadata["url"])

With Filters

from langchain_perplexity import PerplexitySearchRetriever

retriever = PerplexitySearchRetriever(
    k=10,
    country="US",
    search_recency_filter="week",
    search_domain_filter=["example.com", "docs.example.com"],
)

docs = retriever.invoke("latest AI research papers")

Related Pages

Page Connections

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