Skip to main content

Overview

This guide shows how to expose Perplexity’s Search API to Gemini as a function tool through Google’s google-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 a Tool 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.
Wrap the two strings in the Gemini tool format:

Tool handler

The handler wraps client.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 with AutomaticFunctionCallingConfig(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, use generate_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 Content plumbing.
    The auto-loop depth caps at 10 by default. Raise it via AutomaticFunctionCallingConfig(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_call parts in a single response. Build one function_response part per call, carrying that call’s id and name, and put them all in one follow-up Content.
  • Pair function_call and function_response. Both turns must be present in the next generate_content call: the model needs to see its own function_call part alongside your function_response part. Dropping either side produces “missing function response” errors.
  • Domains and dates. Pass search_domain_filter, country, and other Search API parameters inside run_web_search if 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.