Learning resourcesReviewed · Perplexity

Use Search API with the OpenAI SDK

This tutorial helps Python and TypeScript developers add web search to OpenAI assistants, but requires credentials for two services and an application-managed tool loop. It connects Perplexity Search to the OpenAI Responses API through a function tool and explains query limits, result formatting and streaming.

Researched 25 Nov 2026

In short.

This tutorial teaches Python and TypeScript developers to give OpenAI assistants web search, but developers must connect two services and manage repeated tool calls themselves. It supplies a function schema, a Perplexity search handler and a Responses API conversation loop. The handler forwards up to three queries, requests at most five combined results and returns shortened snippets with source URLs. Streaming examples and optional retrieval filters extend the basic pattern. Prices, reuse rights, supported SDK versions and measured answer quality are not established. The examples have not been executed for this assessment.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage includes the complete page at [https://docs.perplexity.ai/docs/search/agent-sdks/openai.md]: prerequisites, tool descriptions and schemas, handlers, conversation loops, streaming examples and notes. Linked documentation indexes, quickstarts, filter guides, parameter references, best practices and other provider guides are excluded. No separate license notice appears in the supplied page. Nothing was executed; runtime compatibility, retrieval quality, generated answers, latency and actual costs remain unassessed.

The overview

Identity

Use Search API with the OpenAI SDK is a tutorial in Perplexity’s documentation at [https://docs.perplexity.ai/docs/search/agent-sdks/openai.md]. It explains how to expose Perplexity Search as a function tool that an OpenAI model can request through the Responses API.

The resource contains parallel Python and TypeScript examples covering installation, tool registration, search execution, conversation state and streaming.

Assessment

This is a useful starting point for an application that uses OpenAI models and needs search results from Perplexity. It follows the connection from a model’s search request through retrieval and back to the model, making the responsibilities of the application clear.

Its value is the implementation pattern and explanatory code. The page supplies no execution results, benchmarks or generated answers demonstrating reliability, retrieval quality or latency.

Users

The guide suits developers building assistants that need current or externally sourced information. Its example asks about recent AI infrastructure announcements.

Readers should be comfortable with SDK calls, environment variables and JSON. Beginners receive examples in two languages, but must assemble several snippets and understand that their application performs searches requested by the model. Experienced developers can adapt the handler’s retrieval constraints and result formatting.

Capabilities

The web_search function accepts an array of queries. Its handler forwards the first three queries together to Perplexity Search and sets max_results to five across the combined results. The page says the underlying API accepts up to five queries; the example deliberately uses a smaller limit.

The formatter skips results without snippet text, removes duplicate URLs within the response, replaces snippet line breaks with spaces and truncates snippets to 400 characters before adding an ellipsis. Each retained result includes its title, URL and snippet.

The streaming examples show text-delta consumption and retrieval of the completed response. The page also describes events for function-call arguments; completing the streamed tool loop requires applying the earlier loop logic.

Using it

Requirements

The integration requires credentials for both services, supplied through OPENAI_API_KEY and PERPLEXITY_API_KEY. The Python examples use the openai and perplexityai packages. The TypeScript examples use openai and @perplexity-ai/perplexity_ai.

The sample model identifier is gpt-5.5. Minimum runtime versions, pinned SDK releases, model access conditions and a tested compatibility matrix are unspecified.

Mechanism

A function tool lets the model ask application code to perform an operation. Here, the application registers web_search using the Responses API’s flat tool structure: type, name, description, parameters and strict. The example schema requires a queries array and rejects additional object properties.

The application inspects response.output for function_call items, saves the model’s output, parses each call’s arguments and runs the search handler. It then appends a function_call_output containing the originating call_id, which connects each result to its request.

The application sends the accumulated conversation back to responses.create until a response contains no function calls, then returns response.output_text. The examples execute multiple requested calls sequentially and provide a separate output for each.

Setup

Install the packages for the chosen language and set both API-key environment variables. Assemble the supplied tool-description strings, WEB_SEARCH_TOOL schema, Perplexity handler and OpenAI conversation loop, then initialize both SDK clients.

The TypeScript snippets include temporary declare statements referring to definitions in earlier snippets. The page explains that these declarations should be removed when the steps share one file.

The tool format is specific to the Responses API. The page distinguishes its flat function definition from the nested function structure used by legacy chat.completions examples.

Example

The supplied invocation asks, “What were the major AI infrastructure announcements this week?” Python passes this question to chat_with_search; TypeScript uses chatWithSearch.

If the model requests web_search, the handler retrieves and formats search results, and the loop returns them to the model for another response. The page shows no actual search results or final generated answer for this invocation, so its answer quality is unknown.

Assessment & evidence

Limitations

The tool description recommends short, keyword-focused queries and says complex queries requiring multi-step reasoning are unsupported. The JSON Schema does not encode the three-query limit; the handler enforces it by discarding queries after the third.

The default handler applies no date, domain or country filters. The page explains that mentioning a date in a query affects relevance without restricting publication dates; actual restrictions require filter parameters in the handler.

Snippet truncation limits the context available to the model, and filtering or deduplication can leave fewer than five results. The examples include no explicit retry policy, application-level timeout policy, recovery from parsing or API errors, or maximum loop count. Returning URLs also does not enforce citations in the final answer.

Reasoning

The pattern gives developers direct control over which search parameters reach Perplexity and how much retrieved text reaches the OpenAI model. This is useful when an application needs a consistent retrieval policy while continuing to use OpenAI for answer generation.

The same control creates implementation work: the application owns conversation state, tool execution and failure handling. Strict argument structure helps keep calls predictable, but does not establish that retrieved material is relevant or that the final answer is accurate. These tradeoffs follow from the code; their practical impact has not been measured.

Alternatives

The page points to separate integrations for the Anthropic SDK [https://docs.perplexity.ai/docs/search/agent-sdks/anthropic.md] and Gemini SDK [https://docs.perplexity.ai/docs/search/agent-sdks/gemini.md]. Their implementations and comparative performance are not established by this page.

Within the OpenAI approach, the page describes previous_response_id as an alternative to resending the full conversation, and responses.stream as an option for incremental text output. It supplies no comparative cost or latency measurements for these choices.

Evidence

The evidence is the complete Perplexity documentation page at [https://docs.perplexity.ai/docs/search/agent-sdks/openai.md], captured on 25 November 2026 at 20:24:32.746 UTC. It includes explanatory text, installation commands, Python and TypeScript examples, streaming snippets and implementation notes.

This is provider-authored documentation. It supports describing the intended integration and inspecting the example’s control flow, but supplies no execution logs, benchmark results or independent verification.

Access & freshness

Costs

The page states no OpenAI model prices, Perplexity Search prices, free allowances or billing terms. Total cost is unknown.

An answer can require repeated OpenAI requests and a Search API request for each web_search call. The example has no overall iteration limit. Its three-query and five-result limits constrain individual searches, without establishing a total request count or spending limit.

Rights

The page contains no explicit license or redistribution terms for its prose or code examples. Permission to redistribute an unchanged copy or repackage the examples is not established by this evidence.

Terms governing commercial use, data handling and reuse of retrieved snippets are also unspecified. The original guide is available at [https://docs.perplexity.ai/docs/search/agent-sdks/openai.md].

Freshness

The capture is dated 25 November 2026. The page itself provides no publication date, last-update timestamp or pinned dependency versions.

The examples name gpt-5.5 and specific SDK methods, but current model availability and SDK compatibility have not been assessed. The capture date does not establish when the examples were last maintained.

Actions

Confirm access to both services, model availability, compatible SDK versions, current prices and applicable terms before adopting the integration. Assemble the examples for one language and run a small end-to-end request in your environment to establish actual behavior.

For an application, add an overall tool-call budget, error and retry handling, and a defined response to the handler’s “No results.” output. Configure date or domain restrictions in the handler when required, and assess returned sources and generated answers against representative questions.

Evidence for this report

selected-page.txtselected · checked 2026-11-25