{"id":7353,"library":"langchain-perplexity","title":"Perplexity LangChain Integration","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/langchain-ai/langchain/tree/master/libs/partners/perplexity","tags":["LangChain","LLM","Perplexity AI","Chatbot","RAG","Search"],"install":[{"cmd":"pip install langchain-perplexity","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core components for all LangChain integrations, providing base abstractions and functionalities.","package":"langchain-core","optional":false}],"imports":[{"symbol":"ChatPerplexity","correct":"from langchain_perplexity import ChatPerplexity"},{"symbol":"PerplexitySearchRetriever","correct":"from langchain_perplexity.retrievers import PerplexitySearchRetriever"},{"symbol":"PerplexitySearchResults","correct":"from langchain_perplexity.tools import PerplexitySearchResults"},{"symbol":"WebSearchOptions","correct":"from langchain_perplexity import WebSearchOptions"}],"quickstart":{"code":"import os\nfrom langchain_perplexity import ChatPerplexity\nfrom langchain_core.prompts import ChatPromptTemplate\n\n# Set your Perplexity API key as an environment variable\n# It's recommended to load this from a .env file or similar in production\nos.environ[\"PERPLEXITY_API_KEY\"] = os.environ.get(\"PERPLEXITY_API_KEY\", \"your_perplexity_api_key_here\")\n\n# Initialize the chat model\nchat = ChatPerplexity(model=\"sonar\")\n\n# Define a prompt template\nprompt = ChatPromptTemplate.from_messages([\n    (\"system\", \"You are a helpful assistant.\"),\n    (\"human\", \"{input}\")\n])\n\n# Create a chain\nchain = prompt | chat\n\n# Invoke the chain with a question\nresponse = chain.invoke({\"input\": \"What breakthroughs in fusion energy have been announced this year?\"})\nprint(response.content)\n\n# Example with Pro Search (requires 'sonar-pro' model and WebSearchOptions)\n# from langchain_perplexity import WebSearchOptions\n# pro_chat = ChatPerplexity(\n#     model=\"sonar-pro\",\n# #    web_search_options=WebSearchOptions(search_type=\"pro\") # Uncomment if specific search_type needed\n# )\n# pro_response = pro_chat.invoke(\"How does the electoral college work?\")\n# print(pro_response.content)","lang":"python","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"breaking","affected_versions":"All versions, especially when upgrading `langchain-core` across major or minor releases (e.g., 0.x to 1.x)."},{"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\", ...)`.","message":"Perplexity 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Using `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.","severity":"gotcha","affected_versions":"All versions up to 1.1.0 (as of verification date), particularly with Perplexity's 'sonar-reasoning' or 'sonar-pro' models."}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install langchain-perplexity` to install the package.","cause":"The `langchain-perplexity` package is not installed in your Python environment.","error":"ModuleNotFoundError: No module named 'langchain_perplexity'"},{"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\")`.","cause":"The Perplexity API key is not provided through an environment variable or directly in the `ChatPerplexity` constructor.","error":"ValueError: Did not find perplexity_api_key, please add an environment variable `PERPLEXITY_API_KEY` or pass `perplexity_api_key` as a named parameter."},{"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.","cause":"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.","error":"json.decoder.JSONDecodeError: Expecting value: line X column Y (char Z) (or similar JSON parsing error with structured output)"}]}