ChatPerplexity()Model wrapper that returns outputs formatted to match the given schema for Preplexity. Currently, Perplexity only supports "json_schema" method for structured output as per their official documentation.
Perplexity AI Chat models API.
Setup:
To use, you should have the environment variable PPLX_API_KEY set to your API key.
Any parameters that are valid to be passed to the perplexity.create call
can be passed in, even if not explicitly saved on this class.
export PPLX_API_KEY=your_api_key
Key init args - completion params: model: Name of the model to use. e.g. "sonar" temperature: Sampling temperature to use. max_tokens: Maximum number of tokens to generate. streaming: Whether to stream the results or not.
Key init args - client params: pplx_api_key: API key for PerplexityChat API. request_timeout: Timeout for requests to PerplexityChat completion API. max_retries: Maximum number of retries to make when generating.
See full list of supported init args and their descriptions in the params section.
Instantiate:
from langchain_perplexity import ChatPerplexity
model = ChatPerplexity(model="sonar", temperature=0.7)
Invoke:
messages = [("system", "You are a chatbot."), ("user", "Hello!")]
model.invoke(messages)
Invoke with structured output:
from pydantic import BaseModel
class StructuredOutput(BaseModel):
role: str
content: str
model.with_structured_output(StructuredOutput)
model.invoke(messages)
Stream:
for chunk in model.stream(messages):
print(chunk.content)
Token usage:
response = model.invoke(messages)
response.usage_metadata
Response metadata:
response = model.invoke(messages)
response.response_metadata
Agent API (Responses):
Set use_responses_api=True to route requests through Perplexity's Agent
API (the Perplexity-flavored Responses API), or leave it unset to have it
auto-detected when a built-in tool (e.g. web_search) or any
Responses-only field (previous_response_id, instructions, input,
include) is supplied.
The Agent API uses its own model catalog (see
https://docs.perplexity.ai/docs/agent-api/models). Select routing by
passing either an explicit model= such as "openai/gpt-5.6-sol", or a
Perplexity preset ("fast", "low", "medium", "high", "xhigh",
or "wide-research") through
model_kwargs. Perplexity's built-in tools and Agent-API-only fields
also go through model_kwargs:
from langchain_perplexity import ChatPerplexity
model = ChatPerplexity(
use_responses_api=True,
model_kwargs={
"preset": "medium",
"tools": [{"type": "web_search"}],
},
)
model.invoke("What did Perplexity announce most recently?")
Auto-detection example (routes to the Agent API because a built-in
web_search tool is present):
model = ChatPerplexity(model="openai/gpt-5.6-sol")
model.invoke(
"Find recent news about AI.",
tools=[{"type": "web_search"}],
)