# Google Gen AI (/docs/guides/sdks/google-gen-ai)

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](/docs/guides/existing-sdks) explains how this works for every SDK.

## Install [#install]

```bash
npm install @google/genai @tensorpass/aiconnect
```

Until `@tensorpass/aiconnect` is published on npm, the [JavaScript quickstart](/docs/quickstart/javascript#install-the-package) shows how to point your project at a checkout of the repository.

## The change [#the-change]

```ts
// 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](/docs/quickstart/react) does: a request from a site the visitor has not connected fails with an `ApiError` of status 401.

## generateContent with function calling [#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.

```ts
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](/docs/guides/mcp-tools#with-an-ai-sdk) 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 [#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:

```ts
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-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](/docs/guides/existing-sdks#model-names) tells which event carries which name.

## Errors and retries [#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](/docs/guides/existing-sdks#errors) 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 [#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](/docs/guides/existing-sdks#what-the-wallet-takes-from-a-request) lists what the wallet passes on to each account.
