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}), which differs 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 tool description below was tuned for the Search API. Keep it verbatim: its wording is what produces short, keyword-style queries. Each provider page wraps these two strings in its own tool format. A date in the query steers relevance but does not restrict publication dates. To restrict them, set search_recency_filter or the date filters in the handler.
Strict mode constraints. When
strict: true, the JSON Schema must set additionalProperties: false and list every property in required. The schema above states the “at most three queries” guidance in the description, and the handler truncates to three queries as a guard.Tool handler
The handler wrapsclient.search.create(). It passes the model’s queries through as one multi-query request, capped at three to match the tool description (the API accepts up to five). max_results caps the combined list across queries, so the model gets at most five results per call. The handler skips results with no extracted text, dedupes by URL, and shortens each snippet to keep the tool result small.
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 parameter reference.
Next Steps
Use with Anthropic SDK
Wire the Search API into the Anthropic SDK.
Use with Gemini SDK
Wire the Search API into the Gemini SDK.
Search API Parameter Reference
Every request parameter, the response schema, and error responses.
Search Best Practices
Query writing, multi-query requests, filters, and rate limits.