Choose the native route
UsePOST /v1beta/models/{model}:generateContent with a native Gemini channel and a model supporting the selected tool. The gateway DTO represents googleSearch, urlContext, and codeExecution; the native response handler forwards the original JSON body. This preserves candidate metadata such as urlContextMetadata even when it is not a typed DTO field. It does not guarantee preservation through Chat or Responses conversion.
Use the camelCase wire fields above. Python SDK snake_case attributes and newer Interactions tool formats are different representations. This gateway has no Interactions endpoint. Enable one tool first; combinations, model versions, provider availability, and deployment policy need separate verification. A model listed by
/v1/models does not establish tool support.
This fills the search/fetch topic illustrated by OpenRouter server tools. OpenRouter’s openrouter:web_search, openrouter:web_fetch, plugins, and automatic search-provider fallback are not enabled by these native Gemini fields. For another protocol see Responses web search; for application-executed tools see function calling.
Check evidence as well as completion
A successful HTTP response andfinishReason: STOP do not establish that a tool ran. The example deliberately requires evidence for its selected task, rejecting a direct answer without it. It accepts one completed model candidate, hides thought/signature data, and retains original part indices in visible text. Errors produce one generic message, with no automatic retry.
Search mode preserves the association between source indices and original text parts. It validates UTF-8 byte boundaries and the optional segment text, rather than applying offsets to concatenated JavaScript strings. It displays citation data as JSON without opening links. It is a terminal diagnostic, not a production citation renderer: it omits search suggestions HTML. A user-facing search UI must follow the upstream display requirements for searchEntryPoint and sources. See Google grounding and the Segment reference.
URL mode sends one explicit HTTPS URL in the prompt. It requires successful metadata for that exact normalized URL; a redirect to a different URL stops this conservative example for review. Retrieval failure, paywalls, unsafe URLs, or missing metadata do not become successful summaries. The script does not fetch the page itself, send gateway credentials to it, or crawl nested links. Use public pages without embedded credentials or private signed URLs; URL syntax validation is not a public-network accessibility check. See URL context.
Code mode asks the upstream to calculate a small fixed example. It accepts ordered Python code/result pairs with OUTCOME_OK, including empty stdout, and requires visible final text. A failed attempt, missing result, or no execution stops the example, even if later text claims success. The code is printed as JSON data and is never run locally. This is not a gateway sandbox or a custom function dispatch loop. See code execution.
Run the three modes
From the documentation directory, use Node 22.18+ or 24 withexamples/http-client.mjs alongside the script. The shared HTTP runtime uses a gateway Bearer token, supports root path prefixes, bounds JSON responses to 1 MiB, and times out after 60 seconds. Each command makes one generation request; each tool may need a different enabled model.
Scope and source verification
The example handles a single non-streaming turn with one built-in tool, without custom function combinations, artifact downloads, or history replay. Source at30a6d88a4: relaykit/dto/gemini.go, relay/channel/gemini/adaptor.go, and relay/channel/gemini/relay-gemini-native.go. The DTO can drop newer unknown fields on request re-serialization (for example new code-part IDs); raw response preservation is not proof of lossless multi-turn replay. Verify those requirements before adding a continuation loop.
For SSE use the native streaming guide for framing, but its text/function example is not a built-in metadata accumulator. Retain metadata across events and validate completion before presenting a grounded result. Local fixtures verify request and parser contracts, not live search quality, URL accessibility, execution accuracy, or tool accounting.
阅读中文