Registry / llm-agents / perplexityai

perplexityai

JSON →
library0.43.3pypypi✓ verified 25d ago

Official Python SDK for the Perplexity API — web-grounded chat completions with real-time search, citations, and reasoning.

pip install perplexityai
INSTALL
IMPORT
SIG · PERPLEXITYAI
P
perplexityai
llm-agentspythonv0.43.3
Install
4.7s avg
Import
2125ms
Disk
32MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.43.3 · 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.910 runs
installs and imports cleanly · install 0.0s · import 1.774s · 33.6MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.7s · import 1.626s · 33MB
32MB installed
● package 32MB
Code
Verified usage

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

Perplexity
from perplexity import Perplexity
import perplexityai
Package installs as perplexityai but module name is perplexity
AsyncPerplexity
from perplexity import AsyncPerplexity
from perplexityai import AsyncPerplexity
Same pattern — import from perplexity, not perplexityai

Web-grounded chat completion with citations

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)
Debug
Known issues
breakingAll llama-3.1-sonar-* and llama-3-sonar-* model names removed Feb 22, 2025. Using them returns a 404/model-not-found error.
fix
Use current Sonar family: sonar, sonar-pro, sonar-reasoning, sonar-reasoning-pro, sonar-deep-research
affects: all
breakingpplx-7b-online, pplx-70b-online, pplx-7b-chat, pplx-70b-chat all deprecated and removed. These were the original Perplexity model names.
fix
Migrate to sonar (lightweight) or sonar-pro (advanced)
affects: all
breakingR1-1776 removed Aug 1, 2025.
fix
Use sonar-reasoning-pro (powered by DeepSeek-R1 with stronger performance)
affects: all
deprecatedAfter April 18, 2025: citation tokens and search result counts no longer returned in usage field for Sonar Pro and Sonar Reasoning Pro.
fix
Do not rely on usage.citations or usage.search_results_count — these fields removed from API response
affects: all
gotchapip install package is perplexityai but the module you import from is perplexity. These are different names — a common confusion source.
fix
pip install perplexityai, then from perplexity import Perplexity
affects: all
gotchaEnv 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.
fix
export PERPLEXITY_API_KEY=your_key
affects: all
gotchasonar-deep-research is async by design — responses can take minutes. Do not use with short timeouts. Supports reasoning_effort: low/medium/high parameter.
fix
Set httpx timeout to 300+ seconds for deep research. Use stream=True to get incremental output.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'perplexity'
The `perplexityai` Python package is not installed in your current environment.
fix
Install the package using pip: `pip install perplexityai`
perplexity.AuthenticationError: Invalid API key.
The API key provided is either missing, incorrect, or expired, leading to authentication failure.
fix
Ensure your `PERPLEXITY_API_KEY` environment variable is correctly set with a valid key, or pass it explicitly when initializing the Perplexity client: `client = Perplexity(api_key="your_api_key_here")`
400 'messages' is a required property.
When making a chat completions request, the `messages` parameter is either not provided, is empty, or does not conform to the expected structure where each message object must have at least a 'role' and 'content' field.
fix
Provide a list of message dictionaries, each with a 'role' (e.g., 'user', 'assistant') and 'content' key. Example: `messages=[{"role": "user", "content": "Hello!"}]`
Perplexity API error (400): {"error":{"message":"Invalid model 'perplexity/sonar-pro'. Permitted models can be found in the documentation at https://docs.perplexity.ai/getting-started/models.","type":"invalid_model","code":400}}
An incorrect or improperly formatted model name was specified in the API request. Some environments or examples might prepend 'perplexity/' to the model name, which the API does not expect.
fix
Use the exact model name without any prefixes, such as 'sonar-small-online', 'sonar-medium-online', 'sonar-large-online', or 'llama-3-sonar-large-32k'. Refer to the official Perplexity documentation for the list of supported models.
AttributeError: 'str' object has no attribute 'choices'
This typically occurs when the API returns an error message as a string instead of a structured response object with a 'choices' attribute, or if you are trying to access 'choices' on a non-response object due to an unexpected API outcome (e.g., rate limiting, internal server error).
fix
Implement error handling (e.g., `try-except` blocks for `perplexity.APIStatusError` or `perplexity.RateLimitError`) to catch non-successful responses before attempting to access response attributes. Inspect the raw API response to understand its format in case of an error.
Upgrade
Version history
0.43.3latest on PyPI · released Aug 11, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
44 hits · last 30 days
node
38
OpenAI (training)
1
Resources
perplexityai — pip install perplexityai · libregistry