How to beginner · 3 min read

Perplexity API for search

Quick answer
Use the OpenAI SDK with the perplexity-search model to perform search queries via the Perplexity API. Send your query as a chat message and parse the response for relevant search results.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the official openai Python package and set your API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Set your API key in your shell: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows).
bash
pip install openai
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Use the OpenAI client to call the Perplexity search model with a chat completion request. The example below sends a search query and prints the top result.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

messages = [
    {"role": "user", "content": "Search for the latest AI research papers on GPT models."}
]

response = client.chat.completions.create(
    model="perplexity-search",
    messages=messages
)

print("Search result:", response.choices[0].message.content)
output
Search result: Here are the latest AI research papers on GPT models: 1. "Scaling Laws for Neural Language Models" by OpenAI, 2. "GPT-4 Technical Report" by OpenAI, 3. "Improving Language Understanding with Retrieval-Augmented Generation" ...

Common variations

You can use async calls for better performance or stream results for real-time output. Also, you can switch to other search-related models if available.

python
import asyncio
import os
from openai import OpenAI

async def async_search():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [{"role": "user", "content": "Find recent breakthroughs in AI."}]
    
    # Async call
    response = await client.chat.completions.acreate(
        model="perplexity-search",
        messages=messages
    )
    print("Async search result:", response.choices[0].message.content)

asyncio.run(async_search())
output
Async search result: Recent breakthroughs in AI include advancements in large language models like GPT-4, improvements in multimodal AI, and new techniques in reinforcement learning.

Troubleshooting

  • If you get authentication errors, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the model perplexity-search is not found, check your API access or use an alternative search model.
  • For rate limits, implement retries with exponential backoff.

Key Takeaways

  • Use the OpenAI SDK with perplexity-search model for search queries.
  • Set your API key securely via environment variables before running code.
  • Async and streaming calls improve responsiveness for search applications.
  • Check model availability and API permissions if you encounter errors.
Verified 2026-04 · perplexity-search
Verify ↗