Overview
This guide shows how to expose Perplexity’s Search API to Gemini as a function tool through Google’sgoogle-genai SDK. The model emits a functionCall part with name and args, you execute the tool (in this case, call client.search.create), and you send the result back as a functionResponse part. The loop continues until the model returns a final text response.
The Google Gen AI Python SDK can run the tool-call loop automatically if you pass a Python callable directly to
tools=[...], and the SDK introspects the signature and invokes your function. This page uses the manual loop so the pattern matches the Anthropic and OpenAI integration pages and so you keep explicit control over the call. The auto-loop variant is shown in Notes.Prerequisites
Tool definition
Gemini accepts aTool containing one or more FunctionDeclaration objects. Each declaration takes a name, description, and a JSON Schema for parameters.
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.
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
Disable the automatic loop withAutomaticFunctionCallingConfig(disable=True) (Python). In TypeScript, plain function declarations always need a manual loop. Then walk response.function_calls, execute each call, and send the model a follow-up Content array that contains the original function_call parts and the matching function_response parts.
Streaming
For streaming, usegenerate_content_stream (Python) or generateContentStream (TypeScript). A single functionCall part can be split across stream chunks, so accumulate parts before invoking your handler. The loop structure is otherwise identical to the non-streaming version.
Notes
-
Automatic loop (Python only). The Google Gen AI Python SDK can run the tool-call loop for you. Pass a Python callable directly and the SDK will introspect its signature, invoke it, and feed the result back to the model, with no manual loop or
Contentplumbing.The auto-loop depth caps at 10 by default. Raise it viaAutomaticFunctionCallingConfig(maximum_remote_calls=N)for long research chains. If your function raises, the library sends the model{"error": "<message>"}as the function response. -
Parallel calls. Gemini can emit multiple
function_callparts in a single response. Build onefunction_responsepart per call, carrying that call’sidandname, and put them all in one follow-upContent. -
Pair
function_callandfunction_response. Both turns must be present in the nextgenerate_contentcall: the model needs to see its ownfunction_callpart alongside yourfunction_responsepart. Dropping either side produces “missing function response” errors. -
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 OpenAI SDK
Wire the Search API into the OpenAI SDK.
Use with Anthropic SDK
Wire the Search API into the Anthropic 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.