Overview
This guide shows how to expose Perplexity’s Search API to an OpenAI model as a function tool. The Responses API uses a manual tool-call loop: the model emits afunction_call item with a call_id, you execute the tool (in this case, call client.search.create), and you send the result back as a function_call_output item paired by call_id. The loop continues until the response no longer contains pending function calls.
This page uses the Responses API (
client.responses.create), the newer OpenAI surface. The tool shape is flat ({type: "function", name, description, parameters, strict}) — different from the legacy chat.completions function-calling shape that nests under function: {…}.Prerequisites
Tool definition
The Responses API takes a flat tool object:type, name, description, parameters (JSON Schema), and optional strict. The description below was tuned for Perplexity’s Search API; keep it verbatim — the wording is what produces good, short, keyword-style queries.
Tool handler
The handler is a thin wrapper aroundclient.search.create. The Search API natively accepts an array of queries, so the array the model emits can be passed straight through.
Tool-call loop
The Responses API returnsresponse.output as a flat list of items. Walk it for items whose type is "function_call", execute each call, and append a paired function_call_output item to the running input array. Re-call responses.create until the response has no more function calls.
Streaming
For streaming, useclient.responses.stream(...). The SDK emits typed events: response.output_item.added when a function_call item starts, response.function_call_arguments.delta for each chunk of the arguments JSON, and response.function_call_arguments.done when the argument string is complete. The loop structure is otherwise identical to the non-streaming version.
Notes
call_idpairs requests with results. Everyfunction_call_outputitem must include the originatingcall_id. Multiple parallel function calls in one assistant turn each get their own paired output.- Server-side state. Instead of resending the whole
inputarray on each turn, you can passprevious_response_id=<id>and only append new items. This is useful for long agent loops. output_textshortcut.response.output_textflattens the assistant text content for you. If you need granular access (annotations, segments), iterateresponse.outputand pulloutput_textblocks out of the message item.- Domains and dates. Pass
search_domain_filter,country, and other Search API parameters insiderun_web_searchif you want fixed retrieval constraints. See the Search API quickstart for the full parameter list.
Next Steps
Use with Anthropic SDK
Wire Search API into the Anthropic Messages API.
Use with Gemini SDK
Wire Search API into Google’s
google-genai SDK.Search API Quickstart
Full Search API parameter reference.
Search Best Practices
Patterns for production search workloads.