Perplexity API OpenAI Compatibility: What Is Supported
🔍 WiseChecker

Perplexity API OpenAI Compatibility: What Is Supported

Developers building AI-powered applications often ask whether the Perplexity API works with tools and libraries built for OpenAI. The Perplexity API is designed to be compatible with the OpenAI client libraries and API format, making migration or integration simpler. This article explains exactly which OpenAI features are supported by the Perplexity API and which are not. You will learn the key differences in endpoints, parameters, authentication, and streaming behavior.

Key Takeaways: Perplexity API OpenAI Compatibility

  • Endpoint structure: Perplexity uses https://api.perplexity.ai with the same chat completions path as OpenAI.
  • Authentication: Both APIs require a Bearer token in the Authorization header.
  • Model names: Perplexity model names like sonar-pro replace OpenAI model names like gpt-4.

ADVERTISEMENT

Perplexity API Design and OpenAI Compatibility Overview

The Perplexity API is built on the same REST architecture as OpenAI’s API. This means you can use the same HTTP methods, headers, and JSON body structure for most requests. The goal is to let developers switch between providers with minimal code changes.

The main difference is the base URL and model names. Perplexity does not support every OpenAI feature. Some parameters like function_call and tools are missing. The API is focused on chat completions and does not offer image generation, embeddings, or fine-tuning.

Base URL and Endpoint Path

OpenAI’s chat completions endpoint is at https://api.openai.com/v1/chat/completions. Perplexity uses https://api.perplexity.ai/chat/completions. The path after the base URL is identical. This means you only need to change the base URL and authentication token to switch from OpenAI to Perplexity.

Authentication Method

Both APIs authenticate using an API key passed as a Bearer token in the Authorization header. For example: Authorization: Bearer pplx-xxxxxxxxxxxxxxxx. Perplexity API keys start with pplx-. You generate them from the Perplexity account dashboard under API Keys.

Model Names

OpenAI uses model names like gpt-4, gpt-3.5-turbo, and gpt-4-turbo. Perplexity uses its own model names such as sonar-pro, sonar-small, and sonar-medium. You must replace the model name in your request body. The full list of available models is at https://api.perplexity.ai/models.

Supported OpenAI Features in the Perplexity API

The following table lists the OpenAI features that Perplexity supports. If a feature is not listed, assume it is not supported.

Feature OpenAI Parameter Perplexity Support
Chat completions /v1/chat/completions Yes
Streaming stream: true Yes
System messages role: system Yes
User messages role: user Yes
Assistant messages role: assistant Yes
Temperature temperature Yes
Max tokens max_tokens Yes
Top P top_p Yes
Stop sequences stop Yes
Presence penalty presence_penalty Yes
Frequency penalty frequency_penalty Yes
N (number of choices) n Yes
Logprobs logprobs No
Functions functions No
Tools tools No
Tool choice tool_choice No
Response format (JSON mode) response_format No

ADVERTISEMENT

Steps to Switch an OpenAI Application to Perplexity

Follow these steps to migrate a simple chat application from OpenAI to Perplexity. These steps assume you are using the official OpenAI Python library or a similar client.

  1. Get your Perplexity API key
    Log in to perplexity.ai/settings/api. Click Generate API Key. Copy the key that starts with pplx-.
  2. Change the base URL in your client
    In the OpenAI client, set base_url to https://api.perplexity.ai. For example: client = OpenAI(api_key='pplx-...', base_url='https://api.perplexity.ai').
  3. Replace the model name
    Change the model parameter from gpt-4 to sonar-pro or another Perplexity model. Example: model='sonar-pro'.
  4. Remove unsupported parameters
    Delete any functions, tools, tool_choice, logprobs, or response_format fields from your request body.
  5. Test the connection
    Send a simple chat completion request. If you receive a 401 error, check your API key. If you receive a 404 error, verify the endpoint path.

Common Misconceptions and Limitations

Can I Use the OpenAI Python Library Directly?

Yes, you can use the openai Python library with the Perplexity API by changing the base URL and API key. The library will format requests correctly. However, the library may try to call unsupported endpoints like /v1/models or /v1/embeddings. You need to handle those calls separately or disable them.

Does Perplexity Support Streaming Responses?

Yes, Perplexity supports streaming with stream: true. The response format is identical to OpenAI’s streaming format. Each chunk contains a choices[0].delta object with content or role. You can use the same streaming logic.

Are Function Calls or Tool Use Available?

No, Perplexity does not support function calls or tool use. The functions, tools, and tool_choice parameters are ignored or cause an error. If your application relies on function calling, you need to implement a separate mechanism or choose a different provider.

Does Perplexity Support Embeddings or Image Generation?

No, Perplexity only provides chat completions. It does not offer embeddings, image generation, or audio processing. Use OpenAI or another provider for those tasks.

Perplexity API vs OpenAI API: Key Differences

Item Perplexity API OpenAI API
Base URL https://api.perplexity.ai https://api.openai.com
Chat completions endpoint /chat/completions /v1/chat/completions
Model naming sonar-pro, sonar-small, sonar-medium gpt-4, gpt-3.5-turbo, etc
Function calling Not supported Supported
Tool use Not supported Supported
Streaming Supported Supported
Logprobs Not supported Supported
JSON mode Not supported Supported
Embeddings Not available Available
Image generation Not available Available

The Perplexity API offers a compatible interface for basic chat completions with streaming and standard sampling parameters. It does not support advanced OpenAI features like function calling, tools, JSON mode, or embeddings. To migrate an existing application, change the base URL to https://api.perplexity.ai, replace the model name with a Perplexity model, and remove any unsupported parameters. For a deeper integration, consider using the Perplexity-specific features like the search_domain parameter for web search or academic search results. Test your application thoroughly after switching because some edge cases like error response formats may differ.

ADVERTISEMENT