Overview
Perplexity’s API is fully compatible with OpenAI’s SDKs. You can use your existing OpenAI client libraries with the Agent API by simply changing the base URL and providing your Perplexity API key.Quick Start
Use the OpenAI SDK with Perplexity’s Agent API:- Python
- Typescript
Configuration
Setting Up the OpenAI SDK
Configure OpenAI SDKs to work with Perplexity by setting thebase_url to https://api.perplexity.ai/v1:
- Python
- Typescript
Important: Use
base_url="https://api.perplexity.ai/v1" (with /v1) for the Agent API.Agent API
Perplexity’s Agent API is fully compatible with OpenAI’s Agent API interface.Basic Usage
- Python
- Typescript
Using Presets
Presets are pre-configured setups optimized for specific use cases. Use theextra_body parameter (Python) or cast the parameter (Typescript) to pass presets:
- Python
- Typescript
See Agent API Presets for available presets and their configurations.
Using Third-Party Models
You can also specify third-party models directly instead of using presets:- Python
- Typescript
Streaming Responses
Streaming works with the Agent API:- Python
- Typescript
Using Tools
The Agent API supports tools, including web search:- Python
- Typescript
API Compatibility
Standard OpenAI Parameters
These parameters work exactly the same as OpenAI’s API: Agent API:model- Model name (use 3rd party models likeopenai/gpt-5.2)input- Input text or message arrayinstructions- System instructionsmax_output_tokens- Maximum tokens in responsestream- Enable streaming responsestools- Array of tools includingweb_search
Perplexity-Specific Parameters
Agent API:preset- Preset name (use Perplexity presets likepro-search)tools[].filters- Search filters within web_search tooltools[].user_location- User location for localized results
See Agent API Reference for complete parameter details.
Response Structure
Agent API
Perplexity Agent API matches OpenAI’s Agent API format:output- Structured output array containing messages withcontent[].textmodel- The model name usedusage- Token consumption detailsid,created_at,status- Response metadata
Best Practices
1
Use the correct base URL
Always use
https://api.perplexity.ai/v1 (with /v1) for the Agent API.2
Handle errors gracefully
Use the OpenAI SDK’s error handling:
3
Use streaming for better UX
Stream responses for real-time user experience:
Recommended: Perplexity SDK
We recommend using Perplexity’s native SDKs for the best developer experience:- Cleaner preset syntax - Use
preset="pro-search"directly instead ofextra_body={"preset": "pro-search"} - Type safety - Full Typescript/Python type definitions for all parameters
- Enhanced features - Direct access to all Perplexity-specific features
- Better error messages - Perplexity-specific error handling
- Simpler setup - No need to configure base URLs
Migrating to the Perplexity SDK
Switch to the Perplexity SDK for enhanced features and cleaner syntax. With the Perplexity SDK, you can use presets directly withoutextra_body and get full type safety:
1
Install the Perplexity SDK
- Python
- Typescript
2
Update the import and client
- Python
- Typescript
No base URL needed - The Perplexity SDK automatically uses the correct endpoint.
3
Update the API calls
The API calls are very similar:
- Python
- Typescript
4
Use presets with cleaner syntax
The Perplexity SDK supports presets with cleaner syntax compared to OpenAI SDK:
- Python
- Typescript
Next Steps
Agent API Quickstart
Get started with Agent API using OpenAI SDKs.
Models
Explore direct model selection and third-party models.
API Reference
View complete endpoint documentation.
Output Control
Configure streaming responses and structured outputs with JSON schema.
Model Fallback
Specify multiple models for automatic failover and higher availability.
Filters
Apply filters to web search results.