Perplexity AI Python SDK
Official Python SDK for the Perplexity API — web-grounded chat completions with real-time search, citations, and reasoning.
Warnings
- breaking All llama-3.1-sonar-* and llama-3-sonar-* model names removed Feb 22, 2025. Using them returns a 404/model-not-found error.
- breaking pplx-7b-online, pplx-70b-online, pplx-7b-chat, pplx-70b-chat all deprecated and removed. These were the original Perplexity model names.
- breaking R1-1776 removed Aug 1, 2025.
- deprecated After April 18, 2025: citation tokens and search result counts no longer returned in usage field for Sonar Pro and Sonar Reasoning Pro.
- gotcha pip install package is perplexityai but the module you import from is perplexity. These are different names — a common confusion source.
- gotcha Env var is PERPLEXITY_API_KEY. Some third-party docs incorrectly show PPLX_API_KEY (legacy curl examples) or PERPLEXITYAI_API_KEY. Only PERPLEXITY_API_KEY is auto-read by the SDK.
- gotcha sonar-deep-research is async by design — responses can take minutes. Do not use with short timeouts. Supports reasoning_effort: low/medium/high parameter.
Install
-
pip install perplexityai -
pip install openai
Imports
- Perplexity
from perplexity import Perplexity
- AsyncPerplexity
from perplexity import AsyncPerplexity
Quickstart
from perplexity import Perplexity
client = Perplexity() # reads PERPLEXITY_API_KEY automatically
response = client.chat.completions.create(
model='sonar',
messages=[{'role': 'user', 'content': 'What happened in AI this week?'}]
)
print(response.choices[0].message.content)