> ## Documentation Index
> Fetch the complete documentation index at: https://docs.niadra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Google GenAI

> The wrap() of the google-genai client, for the Gemini API and Vertex AI: the pack in system_instruction, the answer recorded with usage_metadata.

`wrap()` (Python) and `wrapGoogleGenAI()` (TypeScript) wrap the Google GenAI client so `models.generate_content` and `generate_content_stream` (and the same under `client.aio`) get the context and record the answer.

## Install

<CodeGroup>
  ```sh Python theme={null}
  pip install 'niadra[google-genai]'   # google-genai 2.25 or newer, below 3
  ```

  ```sh TypeScript theme={null}
  npm install @niadra/sdk @google/genai   # @niadra/sdk/google-genai
  ```
</CodeGroup>

The TypeScript integration ships with `@niadra/sdk` 0.3.0, ready on `main` and on npm when it is published; until then the npm package is 0.1.1.

## The five primitives

| Primitive    | How the adapter wires it                                                                                                                                                                                                                   |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Context      | The pack (after the agent's notes, with `agent_memory=`) goes after your `system_instruction`, and the `turn_block` as a user part after the contents                                                                                      |
| Turns        | The answer is recorded as the agent's turn with Gemini's usage: prompt tokens, the ones that came from the content cache among them, and the model version. In TypeScript, the newest user message is also recorded as the customer's turn |
| Tools        | `conversation.tools()` hands over the kit; take `function.parameters` into the `function_declarations`                                                                                                                                     |
| Verification | `conversation.verify()` before the call                                                                                                                                                                                                    |
| Handoff      | `conversation.handoff()` where your flow transfers                                                                                                                                                                                         |

## Minimal example

<CodeGroup>
  ```python Python theme={null}
  """Gemini through the Google GenAI SDK with the customer's context."""

  from google import genai

  from niadra import Niadra, phone
  from niadra.integrations.google_genai import wrap

  niadra = Niadra(channel="chat")
  gemini = wrap(genai.Client())

  with niadra.conversation("thread-81", subject=phone("+5511912345678")) as conversation:
      conversation.customer("Where is my replacement lid?")
      response = gemini.models.generate_content(
          model="gemini-2.5-flash",
          contents="Where is my replacement lid?",
          config={"system_instruction": "You are Acme's agent."},
      )
      print(response.text)
  ```

  ```typescript TypeScript theme={null}
  import { GoogleGenAI } from "@google/genai";
  import { Niadra, handles } from "@niadra/sdk";
  import { wrapGoogleGenAI } from "@niadra/sdk/google-genai";

  const niadra = new Niadra();
  const genai = new GoogleGenAI({});

  /** One customer message in; `userId` comes from your session, never from the model. */
  export async function reply(userId: string, chatId: string, text: string): Promise<string> {
    const convo = niadra.conversation({ subject: handles.appUserId(userId), channel: "web_chat", conversation_id: chatId });
    const ai = wrapGoogleGenAI(genai, convo);
    const response = await ai.models.generateContent({
      model: "gemini-2.5-flash",
      contents: text,
      config: { systemInstruction: "You are Acme's support agent. Be brief." },
    });
    return response.text ?? "";
  }
  ```
</CodeGroup>

The same code is in `examples/google_genai_generate.py` and `examples/google-genai.ts`.

## Limits

* Outside a conversation or task block, calls pass through untouched, and nothing the wrapper does can fail the call.
* Only `generate_content` and `generate_content_stream` are intercepted; chats, embeddings and the rest of the client pass through untouched.
* Tested against `google-genai` 2.25 and `@google/genai` 2.24 with the transport replaced and Niadra on the emulator.

## Next steps

<CardGroup cols={2}>
  <Card title="Google ADK" href="/en/integrations/google-adk">
    Google's agent framework, through callbacks.
  </Card>

  <Card title="Context and views" href="/en/concepts/context">
    the target model and the cache floors.
  </Card>
</CardGroup>
