Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.perplexity.ai/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The Vercel AI SDK is a TypeScript-first toolkit for building AI-powered apps with a unified provider interface, streaming primitives, and React hooks. The official @ai-sdk/perplexity provider gives you full access to Perplexity’s Sonar models — including streaming, citations, image results, and PDF inputs — with a one-line model identifier.
Vercel AI SDK powers generateText, streamText, generateObject, and React hooks like useChat and useCompletion. Learn more at ai-sdk.dev.

Installation

pnpm add @ai-sdk/perplexity ai

API Key Setup

Set your Perplexity API key:
export PERPLEXITY_API_KEY="your_api_key_here"

Get API Key

Generate your Perplexity API key from the API portal.

Quick Start

Import the default provider instance and call generateText:
import { perplexity } from "@ai-sdk/perplexity";
import { generateText } from "ai";

const { text } = await generateText({
  model: perplexity("sonar-pro"),
  prompt: "What are the latest developments in quantum computing?",
});

console.log(text);

Accessing Citations

Every response includes a sources array of the URLs Perplexity consulted:
import { perplexity } from "@ai-sdk/perplexity";
import { generateText } from "ai";

const { text, sources } = await generateText({
  model: perplexity("sonar-pro"),
  prompt: "What are the latest developments in quantum computing?",
});

console.log(text);
console.log(sources);

Provider Options

Pass Perplexity-specific parameters through providerOptions.perplexity:
import { perplexity } from "@ai-sdk/perplexity";
import { generateText } from "ai";

const result = await generateText({
  model: perplexity("sonar-pro"),
  prompt: "What are the latest developments in quantum computing?",
  providerOptions: {
    perplexity: {
      return_images: true,
      search_recency_filter: "week",
    },
  },
});

console.log(result.providerMetadata);
// {
//   perplexity: {
//     usage: { citationTokens: 5286, numSearchQueries: 1 },
//     images: [{ imageUrl: "...", originUrl: "...", height: 1280, width: 720 }],
//   },
// }
Any other Perplexity API parameter can be passed the same way.
OptionTypeDescription
return_imagesbooleanInclude images in the response (Tier-2+ accounts).
search_recency_filterstringOne of 'hour', 'day', 'week', 'month'.

Custom Provider Configuration

Use createPerplexity when you need a custom base URL, headers, or a custom fetch implementation:
import { createPerplexity } from "@ai-sdk/perplexity";

const perplexity = createPerplexity({
  apiKey: process.env.PERPLEXITY_API_KEY ?? "",
  baseURL: "https://api.perplexity.ai",
  headers: {
    "X-Pplx-Integration": "my-app/1.0",
  },
});

PDF Inputs

Sonar models can read PDF files passed as file message parts:
import { perplexity } from "@ai-sdk/perplexity";
import { generateText } from "ai";
import fs from "node:fs";

const result = await generateText({
  model: perplexity("sonar-pro"),
  messages: [
    {
      role: "user",
      content: [
        { type: "text", text: "What is this document about?" },
        {
          type: "file",
          data: fs.readFileSync("./data/ai.pdf"),
          mediaType: "application/pdf",
          filename: "ai.pdf",
        },
      ],
    },
  ],
});
You can also pass a PDF URL:
{
  type: "file",
  data: new URL("https://example.com/document.pdf"),
  mediaType: "application/pdf",
  filename: "document.pdf",
}

Supported Models

ModelImage InputObject Generation
sonarYesYes
sonar-proYesYes
sonar-reasoningYesYes
sonar-reasoning-proYesYes
sonar-deep-researchNoYes
See all available models for full capability details.

Streaming and React Hooks

The provider works seamlessly with streamText, useChat, and useCompletion from ai/react. Drop perplexity("sonar-pro") into any AI SDK helper that takes a model:
import { perplexity } from "@ai-sdk/perplexity";
import { streamText } from "ai";

const result = streamText({
  model: perplexity("sonar"),
  prompt: "Give me a one-sentence summary of this week's AI news.",
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

Vercel AI SDK Provider

Official @ai-sdk/perplexity provider docs.

Vercel AI SDK Docs

Full Vercel AI SDK documentation.

Perplexity API Reference

Full Perplexity API parameter reference.

Perplexity Models

Available Sonar models and capabilities.

Support

Need help with the integration?