OpenAI compatibility at a glance
Perplexity’s Sonar API was designed with OpenAI compatibility in mind, matching the Chat Completions API interface. You can seamlessly use your existing OpenAI client libraries by simply changing the base URL and providing your Perplexity API key.Keep using your existing OpenAI SDKs to get started fast; switch to our native endpoints later as needed.
Configuring OpenAI SDKs to call Sonar
To start using Sonar with OpenAI’s client libraries, pass your Perplexity API key and change the base_url tohttps://api.perplexity.ai
:
Your responses will match OpenAI’s format exactly. See the response structure section below for complete field details.
API compatibility
Standard OpenAI parameters
These parameters work exactly the same as OpenAI’s API:model
- Model name (use Perplexity model names)messages
- Chat messages arraytemperature
- Sampling temperature (0-2)max_tokens
- Maximum tokens in responsetop_p
- Nucleus sampling parameterfrequency_penalty
- Frequency penalty (-2.0 to 2.0)presence_penalty
- Presence penalty (-2.0 to 2.0)stream
- Enable streaming responses
Perplexity-specific parameters
These Perplexity-specific parameters are also included:search_domain_filter
- Limit or exclude specific domainssearch_recency_filter
- Filter by content recencyreturn_images
- Include image URLs in responsereturn_related_questions
- Include related questionssearch_mode
- “web” (default) or “academic” mode selector.
See API Reference for parameter details and models.
Examples with OpenAI’s client libraries
Basic Usage
Start with these simple examples to make your first API calls:Advanced Examples
For more control over search behavior and response generation:Response structure
Perplexity API responses include both standard OpenAI fields and additional search metadata:Standard OpenAI Fields
choices[0].message.content
- The AI-generated responsemodel
- The model name usedusage
- Token consumption detailsid
,created
,object
- Standard response metadata
Perplexity-Specific Fields
search_results
- Array of web sources with titles, URLs, and datesusage.search_context_size
- Search context setting used
Search results are returned even when streaming is enabled, but they arrive in the final chunk of the stream. See the Streaming Guide for details.
Unsupported and notable differences
While compatibility is high, note the following differences from OpenAI:- Model names: Use Perplexity models like
sonar-pro
,sonar-reasoning
. - Search controls: Perplexity adds web/academic search parameters via
extra_body
(Python) or root fields (TypeScript) as shown above.
If you previously used OpenAI-only fields that aren’t applicable to Perplexity search controls, remove or ignore them. Check the API Reference for the current list of supported fields.
Technical notes
- Error format: Same as OpenAI’s API for compatibility
- Rate limiting: Apply standard rate limiting practices
- Model names: Use Perplexity model names (
sonar-pro
,sonar-reasoning
, etc.) - Authentication: Use
Bearer
token format in Authorization header