# google-genai

> GenAI Python SDK

- **URL**: https://www.freshcrate.ai/projects/google-genai
- **Author**: pypi
- **Category**: Developer Tools
- **Latest version**: `v2.8.0` (2026-06-03)
- **License**: Unknown
- **Source**: https://github.com/googleapis/python-genai
- **Homepage**: https://pypi.org/project/google-genai/
- **Language**: Python
- **GitHub**: 3,627 stars, 833 forks
- **Registry**: pypi (`google-genai`)
- **Tags**: `pypi`

## Description

# Google Gen AI SDK

[![PyPI version](https://img.shields.io/pypi/v/google-genai.svg)](https://pypi.org/project/google-genai/)
![Python support](https://img.shields.io/pypi/pyversions/google-genai)
[![PyPI - Downloads](https://img.shields.io/pypi/dw/google-genai)](https://pypistats.org/packages/google-genai)

--------
**Documentation:** https://googleapis.github.io/python-genai/

-----

Google Gen AI Python SDK provides an interface for developers to integrate
Google's generative models into their Python applications. It supports the
[Gemini Developer API](https://ai.google.dev/gemini-api/docs) and
[Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)
APIs.

## Code Generation

Generative models are often unaware of recent API and SDK updates and may suggest outdated or legacy code.

We recommend using our Code Generation instructions [`codegen_instructions.md`](https://raw.githubusercontent.com/googleapis/python-genai/refs/heads/main/codegen_instructions.md) when generating Google Gen AI SDK code to guide your model towards using the more recent SDK features. Copy and paste the instructions into your development environment to provide the model with the necessary context.

## Installation

```sh
pip install google-genai
```

<small>With `uv`:</small>

```sh
uv pip install google-genai
```

## Imports

```python
from google import genai
from google.genai import types
```

## Create a client

Please run one of the following code blocks to create a client for
different services ([Gemini Developer API](https://ai.google.dev/gemini-api/docs) or [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)).

```python
from google import genai

# Only run this block for Gemini Developer API
client = genai.Client(api_key='GEMINI_API_KEY')
```

```python
from google import genai

# Only run this block for Vertex AI API
client = genai.Client(
    vertexai=True, project='your-project-id', location='us-central1'
)
```

## Using types

All API methods support Pydantic types and dictionaries, which you can access
from `google.genai.types`. You can import the types module with the following:

```python
from google.genai import types
```

Below is an example `generate_content()` call using types from the types module:

```python
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents=types.Part.from_text(text='Why is the sky blue?'),
    config=types.GenerateContentConfig(
        temperature=0,
        top_p=0.95,
        top_k=20,
    ),
)
```

Alternatively, you can accomplish the same request using dictionaries instead of
types:

```python
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents={'text': 'Why is the sky blue?'},
    config={
        'temperature': 0,
        'top_p': 0.95,
        'top_k': 20,
    },
)
```

**(Optional) Using environment variables:**

You can create a client by configuring the necessary environment variables.
Configuration setup instructions depends on whether you're using the Gemini
Developer API or the Gemini API in Vertex AI.

**Gemini Developer API:** Set the `GEMINI_API_KEY` or `GOOGLE_API_KEY`.
It will automatically be picked up by the client. It's recommended that you
set only one of those variables, but if both are set, `GOOGLE_API_KEY` takes
precedence.

```bash
export GEMINI_API_KEY='your-api-key'
```

**Gemini API on Vertex AI:** Set `GOOGLE_GENAI_USE_VERTEXAI`,
`GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`, as shown below:

```bash
export GOOGLE_GENAI_USE_VERTEXAI=true
export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='us-central1'
```

```python
from google import genai

client = genai.Client()
```

## Close a client

Explicitly close the sync client to ensure that resources, such as the
underlying HTTP connections, are properly cleaned up and closed.

```python
from google.genai import Client

client = Client()
response_1 = client.models.generate_content(
    model=MODEL_ID,
    contents='Hello',
)
response_2 = client.models.generate_content(
    model=MODEL_ID,
    contents='Ask a question',
)
# Close the sync client to release resources.
client.close()
```

To explicitly close the async client:

```python
from google.genai import Client

aclient = Client(
    vertexai=True, project='my-project-id', location='us-central1'
).aio
response_1 = await aclient.models.generate_content(
    model=MODEL_ID,
    contents='Hello',
)
response_2 = await aclient.models.generate_content(
    model=MODEL_ID,
    contents='Ask a question',
)
# Close the async client to release resources.
await aclient.aclose()
```

## Client context managers

By using the sync client context manager, it will close the underlying
sync client when exiting the with block and avoid httpx "client has been closed" error like [issues#1763](https://github.com/googleapis/python-genai/issues/1763).

```python
from google.genai import Client

with Client() as client:
    respons

## Recent releases

| Version | Date | Urgency | Changes |
| --- | --- | --- | --- |
| `v2.8.0` | 2026-06-03 | High | ## [2.8.0](https://github.com/googleapis/python-genai/compare/v2.7.0...v2.8.0) (2026-06-03)   ### Features  * Add Agent Platform MCP support to async generate_content ([e3be9af](https://github.com/googleapis/python-genai/commit/e3be9af3c21c9f8f625aecd890a5ee1f9993fb3d)) * Add transcription language code. ([53ea3f6](https://github.com/googleapis/python-genai/commit/53ea3f689bf02ecbdbecf8a2a4aff77c48016eb9)) * Add TranslationConfig for live translation. ([4775314](https://github.com/googleapis/pyt |
| `v2.7.0` | 2026-05-28 | High | ## [2.7.0](https://github.com/googleapis/python-genai/compare/v2.6.0...v2.7.0) (2026-05-27)   ### Features  * Additional computer_use field support for vertex. ([b4828fa](https://github.com/googleapis/python-genai/commit/b4828fa5b085d2dbb47a0adc9c10e4b35d60ad64)) * **interaction-api:** Allow "text/csv" as a supported document mime type for Interaction API. ([543137b](https://github.com/googleapis/python-genai/commit/543137b78d392c7d81a287f3916e215f13845a72)) * **interaction-api:** Enable BigQuer |
| `v2.6.0` | 2026-05-22 | High | ## [2.6.0](https://github.com/googleapis/python-genai/compare/v2.5.0...v2.6.0) (2026-05-21)   ### Features  * Add `enable_prompt_injection_detection` for Computer Use feature for the Gemini API. ([b1f632d](https://github.com/googleapis/python-genai/commit/b1f632d5d617a8e24fd4cb87556a4eb180b6d29b)) * Add budget_exceeded status ([15443c0](https://github.com/googleapis/python-genai/commit/15443c0211446c41a9ab2705dd2282d324581eb7)) * Add gemini-3.5-flash ([15443c0](https://github.com/googleapis/pyth |
| `v2.3.0` | 2026-05-15 | High | ## [2.3.0](https://github.com/googleapis/python-genai/compare/v2.2.0...v2.3.0) (2026-05-15)   ### Features  * Add content union to UserInputStep ([a5059a8](https://github.com/googleapis/python-genai/commit/a5059a82dc596f9555dd3221aa6e7414d50df24a)) * Interaction.{output_text,output_image,output_audio,output_video} ([975d16a](https://github.com/googleapis/python-genai/commit/975d16a3cea49282137dd2a901b219820a641b64)) * Migrate Agent Engines, Evaluation, Prompt Management, and Skill features to ag |
| `v2.0.1` | 2026-05-09 | High | ## [2.0.1](https://github.com/googleapis/python-genai/compare/v2.0.0...v2.0.1) (2026-05-09)   ### Bug Fixes  * Update response_format field names to snake_case. ([97142b1](https://github.com/googleapis/python-genai/commit/97142b11fa71ccb5b6b844662db5d4478a50da4c)) |
| `v1.75.0` | 2026-05-04 | High | ## [1.75.0](https://github.com/googleapis/python-genai/compare/v1.74.0...v1.75.0) (2026-05-04)   ### Features  * [Python] Multimodal file search ([fe5d69e](https://github.com/googleapis/python-genai/commit/fe5d69e04026e82314dfb93f47e184e01c993a22))   ### Bug Fixes  * Avoid caching stale token in async mTLS path ([4e17a9c](https://github.com/googleapis/python-genai/commit/4e17a9c3cbba4a9229f9ca2958f7a26257e8e2e7))   ### Documentation  * Fix python docstrings for Image.from_file() to use kwargs ([ |
| `v1.74.0` | 2026-04-29 | High | ## [1.74.0](https://github.com/googleapis/python-genai/compare/v1.73.1...v1.74.0) (2026-04-29)   ### Features  * [Interactions] Add FileCitation.{custom_metadata,media_id,page_number} ([aed41ec](https://github.com/googleapis/python-genai/commit/aed41ecf4940f63446fc3e22744663be4d1057a6)) * Add `output_info` to `BatchJob` ([7b77ab8](https://github.com/googleapis/python-genai/commit/7b77ab850283a2c55cb711084e8de6b6da5e589c)) * Add gemini-3.1-flash-tts-preview model to options ([8bdc1c3](https://git |
| `1.73.1` | 2026-04-21 | Low | Imported from PyPI (1.73.1) |
| `v1.73.1` | 2026-04-14 | Medium | ## [1.73.1](https://github.com/googleapis/python-genai/compare/v1.73.0...v1.73.1) (2026-04-14)   ### Bug Fixes  * Refactor Webhook types in GenAI SDKs for easier useage ([3f36ca1](https://github.com/googleapis/python-genai/commit/3f36ca11b30904c8f82dd3e7e3b59eff3bde6a3b)) * Rename `webhooks.retrieve` to `webhooks.get`. ([649f4b0](https://github.com/googleapis/python-genai/commit/649f4b06d7bd78a23dd77b06713c6ca5c65321f9))   ### Documentation  * Update python docs for 1.73.0 ([acd3767](https://git |
| `v1.73.1` | 2026-04-14 | Medium | ## [1.73.1](https://github.com/googleapis/python-genai/compare/v1.73.0...v1.73.1) (2026-04-14)   ### Bug Fixes  * Refactor Webhook types in GenAI SDKs for easier useage ([3f36ca1](https://github.com/googleapis/python-genai/commit/3f36ca11b30904c8f82dd3e7e3b59eff3bde6a3b)) * Rename `webhooks.retrieve` to `webhooks.get`. ([649f4b0](https://github.com/googleapis/python-genai/commit/649f4b06d7bd78a23dd77b06713c6ca5c65321f9))   ### Documentation  * Update python docs for 1.73.0 ([acd3767](https://git |

## Citation

- HTML: https://www.freshcrate.ai/projects/google-genai
- Markdown: https://www.freshcrate.ai/projects/google-genai.md
- Dependencies JSON: https://www.freshcrate.ai/api/projects/google-genai/deps

_Generated by freshcrate.ai. Indexes pypi releases for AI-agent ecosystem packages._
