Perplexity’s Agent API is fully compatible with OpenAI’s Responses API interface. You can use your existing OpenAI client libraries by simply changing the base URL and providing your Perplexity API key.
Endpoint Note: Perplexity’s canonical Agent API endpoint is POST /v1/agent. For OpenAI SDK compatibility, POST /v1/responses is also accepted as an alias — the OpenAI SDK automatically routes client.responses.create() to /v1/responses, which Perplexity handles seamlessly. No SDK changes are needed beyond setting the base URL.
We recommend using the Perplexity SDK for the best experience with full type safety, enhanced features, and preset support. Use OpenAI SDKs if you’re already integrated and need drop-in compatibility.
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai/v1")response = client.responses.create( model="openai/gpt-5.4", input="Explain the key differences between REST and GraphQL APIs")print(response.output_text)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai/v1"});const response = await client.responses.create({ model: "openai/gpt-5-mini", input: "Explain the key differences between REST and GraphQL APIs"});console.log(response.output_text);
Perplexity’s Agent API follows OpenAI’s Responses API request/response format. The OpenAI SDK’s client.responses.create() method works out of the box — the SDK sends requests to /v1/responses, which Perplexity accepts alongside the canonical /v1/agent endpoint.
Presets are pre-configured setups optimized for specific use cases. Use extra_body to pass presets via the OpenAI SDK:
Python
Typescript
Copy
Ask AI
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai/v1")# Pass preset via extra_bodyresponse = client.responses.create( input="What are the key differences between the latest iPhone and Samsung Galaxy flagship phones?", extra_body={ "preset": "pro-search" })print(response.output_text)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai/v1"});// Use type casting (as any) to pass preset via extra_bodyconst response = await (client.responses.create as any)({ input: "What are the key differences between the latest iPhone and Samsung Galaxy flagship phones?", extra_body: { "preset": "pro-search" }});console.log(response.output_text);
See Agent API Presets for available presets and their configurations.
You can also specify third-party models directly instead of using presets:
Python
Typescript
Copy
Ask AI
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai/v1")response = client.responses.create( model="openai/gpt-5.4", input="Explain the key differences between REST and GraphQL APIs")print(response.output_text)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai/v1"});const response = await client.responses.create({ model: "openai/gpt-5-mini", input: "Explain the key differences between REST and GraphQL APIs"});console.log(response.output_text);
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai/v1")response = client.responses.create( model="openai/gpt-5.4", input="Write a bedtime story about a unicorn.", stream=True)for event in response: if event.type == "response.output_text.delta": print(event.delta, end="", flush=True)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai/v1"});const response = await client.responses.create({ model: "openai/gpt-5-mini", input: "Write a bedtime story about a unicorn.", stream: true});for await (const event of response) { if (event.type === "response.output_text.delta") { process.stdout.write(event.delta); }}
The Agent API supports built-in tools, including web search. Use extra_body to pass tools via the OpenAI SDK:
Python
Typescript
Copy
Ask AI
from openai import OpenAIclient = OpenAI( api_key="YOUR_API_KEY", base_url="https://api.perplexity.ai/v1")# Pass tools via extra_bodyresponse = client.responses.create( model="openai/gpt-5.4", input="Which companies announced the largest AI acquisitions this quarter?", extra_body={ "tools": [ { "type": "web_search", "filters": { "search_domain_filter": ["techcrunch.com", "crunchbase.com"] } } ] })print(response.output_text)
Copy
Ask AI
import OpenAI from 'openai';const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.perplexity.ai/v1"});// Use type casting (as any) to pass tools via extra_bodyconst response = await (client.responses.create as any)({ model: "openai/gpt-5-mini", input: "Which companies announced the largest AI acquisitions this quarter?", extra_body: { "tools": [ { "type": "web_search", "filters": { "search_domain_filter": ["techcrunch.com", "crunchbase.com"] } } ] }});console.log(response.output_text);
Both paths accepted by Perplexity for compatibility
When using the OpenAI SDK, client.responses.create() sends requests to /v1/responses. Perplexity accepts this path as an alias for /v1/agent, so no SDK configuration changes are needed beyond base_url.
Switch to the Perplexity SDK for enhanced features and cleaner syntax. With the Perplexity SDK, you can use presets directly without extra_body and get full type safety:
1
Install the Perplexity SDK
Python
Typescript
Copy
Ask AI
pip install perplexityai
Copy
Ask AI
npm install @perplexity-ai/perplexity_ai
2
Update the import and client
Python
Typescript
Copy
Ask AI
# Before (OpenAI SDK)from openai import OpenAIclient = OpenAI( api_key="pplx-...", base_url="https://api.perplexity.ai/v1")# After (Perplexity SDK)from perplexity import Perplexityclient = Perplexity(api_key="pplx-...")# Or just: client = Perplexity() if PERPLEXITY_API_KEY env var is set
Copy
Ask AI
// Before (OpenAI SDK)import OpenAI from 'openai';const client = new OpenAI({ apiKey: "pplx-...", baseURL: "https://api.perplexity.ai/v1"});// After (Perplexity SDK)import Perplexity from '@perplexity-ai/perplexity_ai';const client = new Perplexity({ apiKey: "pplx-..." });// Or just: const client = new Perplexity() if PERPLEXITY_API_KEY env var is set
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
Copy
Ask AI
# Agent API - same interfaceresponse = client.responses.create( model="openai/gpt-5.4", input="Hello!")
Copy
Ask AI
// Agent API - same interfaceconst response = await client.responses.create({ model: "openai/gpt-5-mini", input: "Hello!"});
4
Use presets with cleaner syntax
The Perplexity SDK supports presets with cleaner syntax compared to OpenAI SDK:
Python
Typescript
Copy
Ask AI
# Before (OpenAI SDK) - extra_body requiredresponse = client.responses.create( input="What were the biggest tech IPOs this year and how did they perform on day one?", extra_body={"preset": "pro-search"})# After (Perplexity SDK) - direct parameterresponse = client.responses.create( preset="pro-search", input="What were the biggest tech IPOs this year and how did they perform on day one?")
Copy
Ask AI
// Before (OpenAI SDK) - type casting requiredconst response = await client.responses.create({ input: "What were the biggest tech IPOs this year and how did they perform on day one?", preset: "pro-search"} as any);// After (Perplexity SDK) - fully typedconst response = await client.responses.create({ preset: "pro-search", input: "What were the biggest tech IPOs this year and how did they perform on day one?"});