Skip to Content
TensorPass
GuidesAI SDKs

Google Gen AI

Keep your Google Gen AI code, generateContent streamed or whole, and let it answer on the visitor's own AI account by passing the AiConnect client's fetch in the HTTP options.

Your @google/genai client keeps its methods, its response objects and its function calling. It sends its requests through your AiConnect client's fetch, and every call then answers through the visitor's wallet, on the account the visitor picked for your site. That account can be a Gemini plan, a Claude plan, an OpenAI key or any other account the wallet holds. The AI SDK guide explains how this works for every SDK.

Install

npm install @google/genai @tensorpass/aiconnect

Until @tensorpass/aiconnect is published on npm, the JavaScript quickstart shows how to point your project at a checkout of the repository.

The change

// Before
const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

// After
const client = new GoogleGenAI({ apiKey: aiconnect.sdkOptions.apiKey, httpOptions: { fetch: aiconnect.fetch } });

aiconnect is your AiConnect client. The SDK requires a key, so the placeholder aiwallet of sdkOptions.apiKey fills it, and the wallet never reads it. The Gemini API protocol answers, the one the SDK speaks when it is built with an API key.

The call runs in the page, where the wallet answers. Show your connect entry point first, as the React quickstart does: a request from a site the visitor has not connected fails with an ApiError of status 401.

generateContent with function calling

The bookshop demo in apps/demo of the AI Connect repository runs this loop in its SDK mode. search_books stands for your own tools, runTool runs one and returns its result as text, and showText writes the answer as it streams.

import { GoogleGenAI } from '@google/genai';
import type { Content, Part } from '@google/genai';

const client = new GoogleGenAI({ apiKey: aiconnect.sdkOptions.apiKey, httpOptions: { fetch: aiconnect.fetch } });
const config = { systemInstruction: system, tools: [{ functionDeclarations }], abortSignal: signal };
const contents: Content[] = [{ role: 'user', parts: [{ text: question }] }];
for (let step = 0; step < 4; step += 1) {
  const parts: Part[] = [];
  for await (const chunk of await client.models.generateContentStream({ model: 'gemini-3-pro', contents, config })) {
    for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {
      if (part.text && !part.thought) {
        showText(part.text);
      }
      parts.push(part);
    }
  }
  contents.push({ role: 'model', parts });
  const calls = parts.flatMap((part) => (part.functionCall ? [part.functionCall] : []));
  if (!calls.length) {
    break;
  }
  const responses: Part[] = [];
  for (const { id, name = '', args = {} } of calls) {
    responses.push({ functionResponse: { id, name, response: { output: await runTool(name, args) } } });
  }
  contents.push({ role: 'user', parts: responses });
}

functionDeclarations holds your tools as { name, description, parametersJsonSchema }, or with parameters in Gemini's own schema format. When they live on your MCP server, the MCP guide shows how to read them in the page. toolConfig.functionCallingConfig reaches the account as the tool choice: ANY with one allowed name forces that tool.

Gemini may leave tool call ids out. The wallet then numbers the calls in order, and each function response answers the first open call of its name. Parts marked thought are left out when you send the conversation back.

Images and structured output

An image as inlineData reaches every account. An image as fileData reaches the accounts whose provider fetches images, and the Gemini plan refuses it. Audio, video and documents are refused with a 400.

responseMimeType: 'application/json' with responseJsonSchema or responseSchema reaches the account in its own provider's format:

const result = await client.models.generateContent({
  model: 'gemini-3-pro',
  contents: question,
  config: { responseMimeType: 'application/json', responseJsonSchema: pickSchema },
});
const pick = JSON.parse(result.text ?? '{}');

thinkingConfig is not passed on: the visitor sets the thinking effort in the wallet.

The model that answers

The model you pass is ignored, since the visitor chooses the model in the wallet. The first chunk of a stream carries the model of the account that answers in modelVersion. The model names section tells which event carries which name.

Errors and retries

A failure before any text throws the SDK's ApiError, whose status is the wallet's status: 401 when the site is not connected, 403 when the visitor declined or left the wallet locked, 429 when the account's plan is spent. A failure after the text started ends the stream. The errors section lists every status.

The SDK retries only when you set httpOptions.retryOptions. On Auto the wallet already moves to the visitor's next account, so a site can leave retries off.

config.abortSignal stops the answer in the wallet.

What a request can carry

candidateCount above 1 and Gemini's own tools, such as Google Search grounding or code execution, are refused with a 400. The AI SDK guide lists what the wallet passes on to each account.

On this page