Registry / llm-agents / langchain-perplexity

langchain-perplexity

JSON →
library1.4.0pypypi✓ verified 85d ago

langchain-perplexity is an official LangChain integration package that connects Perplexity AI's powerful conversational models and search capabilities with the LangChain framework. It allows developers to build LLM applications with real-time web search, streaming, and advanced search controls using Perplexity's API. The library is actively maintained as part of the LangChain ecosystem, with current version 1.1.0, and typically follows the release cadence of other LangChain partner packages.

pip install langchain-perplexity
INSTALL
IMPORT
SIG · LANGCHAIN-PERPLEXI
L
langchain-perplexity
llm-agentspythonv1.4.0
Install
7.2s avg
Import
2563ms
Disk
73MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 2.681s · 67.5MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 7.2s · import 2.446s · 76MB
73MB installed
● package 73MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ChatPerplexity
from langchain_perplexity import ChatPerplexity
PerplexitySearchRetriever
from langchain_perplexity.retrievers import PerplexitySearchRetriever
PerplexitySearchResults
from langchain_perplexity.tools import PerplexitySearchResults
WebSearchOptions
from langchain_perplexity import WebSearchOptions

This quickstart demonstrates how to set up and use `ChatPerplexity` for conversational AI with real-time web search capabilities. It includes setting the API key and invoking a simple chat chain.

import os from langchain_perplexity import ChatPerplexity from langchain_core.prompts import ChatPromptTemplate # Set your Perplexity API key as an environment variable # It's recommended to load this from a .env file or similar in production os.environ["PERPLEXITY_API_KEY"] = os.environ.get("PERPLEXITY_API_KEY", "your_perplexity_api_key_here") # Initialize the chat model chat = ChatPerplexity(model="sonar") # Define a prompt template prompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant."), ("human", "{input}") ]) # Create a chain chain = prompt | chat # Invoke the chain with a question response = chain.invoke({"input": "What breakthroughs in fusion energy have been announced this year?"}) print(response.content) # Example with Pro Search (requires 'sonar-pro' model and WebSearchOptions) # from langchain_perplexity import WebSearchOptions # pro_chat = ChatPerplexity( # model="sonar-pro", # # web_search_options=WebSearchOptions(search_type="pro") # Uncomment if specific search_type needed # ) # pro_response = pro_chat.invoke("How does the electoral college work?") # print(pro_response.content)
Debug
Known issues
breakingThe LangChain ecosystem, including `langchain-core`, undergoes frequent updates and breaking changes. This may lead to needing to update import paths, class names, or method signatures in your `langchain-perplexity` integration code to maintain compatibility.
fix
Regularly consult the official LangChain migration guides and changelogs. Update your code to use the LangChain Expression Language (LCEL) and `Runnable` interface where applicable for future-proofing.
affects: All versions, especially when upgrading `langchain-core` across major or minor releases (e.g., 0.x to 1.x).
gotchaPerplexity API key must be correctly configured. The library expects the API key to be set either as an environment variable named `PERPLEXITY_API_KEY` (or `PPLX_API_KEY` for older patterns) or passed directly to the `ChatPerplexity` constructor via the `perplexity_api_key` parameter.
fix
Ensure your environment variable `PERPLEXITY_API_KEY` is set: `export PERPLEXITY_API_KEY='your_api_key_here'` or `os.environ["PERPLEXITY_API_KEY"] = "your_api_key_here"`. Alternatively, instantiate `ChatPerplexity(perplexity_api_key="your_api_key_here", ...)`.
affects: All versions
gotchaUsing `ChatPerplexity` with LangChain's `with_structured_output` functionality can fail if Perplexity models (especially 'reasoning' models) include their intermediate thought processes (e.g., text wrapped in `<think>...</think>`) in the output, breaking JSON parsing.
fix
If structured output is critical, inspect the raw output for `<think>` tags. You may need to implement a custom output parser that explicitly strips these tags before attempting JSON deserialization, or avoid using `with_structured_output` with models that exhibit this behavior.
affects: All versions up to 1.1.0 (as of verification date), particularly with Perplexity's 'sonar-reasoning' or 'sonar-pro' models.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'langchain_perplexity'
The `langchain-perplexity` package is not installed in your Python environment.
fix
Run `pip install langchain-perplexity` to install the package.
ValueError: Did not find perplexity_api_key, please add an environment variable `PERPLEXITY_API_KEY` or pass `perplexity_api_key` as a named parameter.
The Perplexity API key is not provided through an environment variable or directly in the `ChatPerplexity` constructor.
fix
Set the environment variable: `export PERPLEXITY_API_KEY="YOUR_API_KEY"` (or `os.environ["PERPLEXITY_API_KEY"] = "YOUR_API_KEY"` in Python code), or pass it during initialization: `ChatPerplexity(perplexity_api_key="YOUR_API_KEY", model="sonar")`.
json.decoder.JSONDecodeError: Expecting value: line X column Y (char Z) (or similar JSON parsing error with structured output)
Perplexity models, particularly reasoning-capable ones, might embed non-JSON content (like internal thought processes in `<think>` tags) within their response, which interferes with LangChain's `with_structured_output` parsing.
fix
Temporarily avoid using `with_structured_output` with Perplexity models if this issue occurs. Inspect the raw model output for extraneous text. Consider implementing a custom output parser that pre-processes the model's text to remove non-JSON elements before parsing.
Upgrade
Version history
1.4.0latest on PyPI · released Jun 9, 2026
Audit
Dependencies
langchain-corerequiredCore components for all LangChain integrations, providing base abstractions and functionalities.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
langchain-perplexity — pip install langchain-perplexity · libregistry