Skip to main content

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.
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.

Quick Start

Use the OpenAI SDK with Perplexity’s Agent API:

Configuration

Setting Up the OpenAI SDK

Configure OpenAI SDKs to work with Perplexity by setting the base_url to https://api.perplexity.ai/v1:
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

Using Presets

Presets are pre-configured setups optimized for specific use cases. Use the extra_body parameter (Python) or cast the parameter (Typescript) to pass presets:
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:

Streaming Responses

Streaming works with the Agent API:

Using Tools

The Agent API supports tools, including web search:

API Compatibility

Standard OpenAI Parameters

These parameters work exactly the same as OpenAI’s API: Agent API:
  • model - Model name (use 3rd party models like openai/gpt-5.2)
  • input - Input text or message array
  • instructions - System instructions
  • max_output_tokens - Maximum tokens in response
  • stream - Enable streaming responses
  • tools - Array of tools including web_search

Perplexity-Specific Parameters

Agent API:
  • preset - Preset name (use Perplexity presets like pro-search)
  • tools[].filters - Search filters within web_search tool
  • tools[].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 with content[].text
  • model - The model name used
  • usage - Token consumption details
  • id, 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:
We recommend using Perplexity’s native SDKs for the best developer experience:
  • Cleaner preset syntax - Use preset="pro-search" directly instead of extra_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
See the Perplexity SDK Guide for details.

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 without extra_body and get full type safety:
1

Install the Perplexity SDK

2

Update the import and client

No base URL needed - The Perplexity SDK automatically uses the correct endpoint.
3

Update the API calls

The API calls are very similar:
4

Use presets with cleaner syntax

The Perplexity SDK supports presets with cleaner syntax compared to OpenAI SDK:

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.