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 Python SDK can run the tool-call loop automatically if you pass a Python callable directly to tools=[...] — 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 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 around client.search.create. The Search API natively accepts an array of queries (up to five), so the array Gemini emits can be passed straight through.

Tool-call loop

Disable the automatic loop with AutomaticFunctionCallingConfig(disable=True) (Python) — the JS SDK doesn’t auto-loop in core APIs to begin with. 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 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 — no manual loop, no Content plumbing.
    The auto-loop depth caps at 10 by default — raise it via AutomaticFunctionCallingConfig(maximum_remote_calls=N) for long research chains. Auto-mode is convenient but trades visibility for terseness: errors surface through exceptions rather than tool-result content the model can recover from.
  • Parallel calls. Gemini can emit multiple function_call parts in a single response. Build one function_response part per call (matched by name plus the call’s position within the turn) 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 quickstart for the full parameter list.

Next Steps

Use with Anthropic SDK

Wire Search API into the Anthropic Messages API.

Use with OpenAI SDK

Wire Search API into the OpenAI Responses API.

Search API Quickstart

Full Search API parameter reference.

Search Best Practices

Patterns for production search workloads.