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

# Client

> Synchronous client for making requests to the Gemini API or Vertex AI API.

The `Client` class is the main entry point for making synchronous requests to the Gemini Developer API or Vertex AI API. It provides access to all SDK features including models, files, caches, tunings, and more.

## Constructor

Create a new client instance for either the Gemini API or Vertex AI API.

<ParamField path="vertexai" type="bool" default="False">
  Indicates whether the client should use the Vertex AI API endpoints. Defaults to False (uses Gemini Developer API endpoints).
</ParamField>

<ParamField path="api_key" type="str">
  The [API key](https://ai.google.dev/gemini-api/docs/api-key) to use for authentication. Applies to the Gemini Developer API only. Can also be set via the `GOOGLE_API_KEY` environment variable.
</ParamField>

<ParamField path="credentials" type="google.auth.credentials.Credentials">
  The credentials to use for authentication when calling the Vertex AI APIs. Credentials can be obtained from environment variables and default credentials. For more information, see [Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc). Applies to the Vertex AI API only.
</ParamField>

<ParamField path="project" type="str">
  The [Google Cloud project ID](https://cloud.google.com/vertex-ai/docs/start/cloud-environment) to use for quota. Can be obtained from environment variables (e.g., `GOOGLE_CLOUD_PROJECT`). Applies to the Vertex AI API only. Find your [project ID](https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects).
</ParamField>

<ParamField path="location" type="str">
  The [location](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations) to send API requests to (e.g., `us-central1`). Can be obtained from environment variables (e.g., `GOOGLE_CLOUD_LOCATION`). Applies to the Vertex AI API only.
</ParamField>

<ParamField path="debug_config" type="DebugConfig">
  Configuration settings that control network behavior of the client. This is typically used when running test code.
</ParamField>

<ParamField path="http_options" type="types.HttpOptions | dict">
  HTTP options to use for the client. These options will be applied to all requests made by the client.

  See [HttpOptions](#httpoptions) for available fields.
</ParamField>

## Usage Examples

### Gemini Developer API

```python theme={null}
from google import genai

client = genai.Client(api_key='my-api-key')
```

### Vertex AI API

```python theme={null}
from google import genai

client = genai.Client(
    vertexai=True,
    project='my-project-id',
    location='us-central1'
)
```

### With HTTP Options

```python theme={null}
from google import genai
from google.genai import types

client = genai.Client(
    api_key='my-api-key',
    http_options=types.HttpOptions(
        api_version='v1',
        timeout=30000,  # 30 seconds in milliseconds
        headers={'Custom-Header': 'value'}
    )
)
```

### Using Context Manager

```python theme={null}
from google import genai

with genai.Client(api_key='my-api-key') as client:
    response = client.models.generate_content(
        model='gemini-2.0-flash',
        contents='Hello World'
    )
    print(response.text)
# Client is automatically closed when exiting the context
```

## Properties

### models

<ResponseField name="models" type="Models">
  Access to the Models API for content generation, embeddings, and model management.

  See [generate\_content](/api/models/generate-content) for details.
</ResponseField>

```python theme={null}
response = client.models.generate_content(
    model='gemini-2.0-flash',
    contents='Tell me a story'
)
```

### aio

<ResponseField name="aio" type="AsyncClient">
  Access to the asynchronous client for non-blocking operations.

  See [AsyncClient](/api/async-client) for details.
</ResponseField>

```python theme={null}
async_client = client.aio
response = await async_client.models.generate_content(
    model='gemini-2.0-flash',
    contents='Hello async world'
)
```

### chats

<ResponseField name="chats" type="Chats">
  Access to multi-turn conversation functionality.

  See [chats.create](/api/chats/create) for details.
</ResponseField>

```python theme={null}
chat = client.chats.create(model='gemini-2.0-flash')
response = chat.send_message('Hello!')
```

### files

<ResponseField name="files" type="Files">
  Access to the Files API for uploading and managing media files.

  See [files.upload](/api/files/upload) for details.
</ResponseField>

```python theme={null}
file = client.files.upload(path='image.jpg')
print(file.name)
```

### caches

<ResponseField name="caches" type="Caches">
  Access to the Caches API for context caching.

  See [caches.create](/api/caches/create) for details.
</ResponseField>

```python theme={null}
cache = client.caches.create(
    model='gemini-2.0-flash',
    contents='Long document...'
)
```

### file\_search\_stores

<ResponseField name="file_search_stores" type="FileSearchStores">
  Access to the File Search Stores API for semantic search.
</ResponseField>

### batches

<ResponseField name="batches" type="Batches">
  Access to the Batches API for batch processing.

  See [batches.create](/api/batches/create) for details.
</ResponseField>

### tunings

<ResponseField name="tunings" type="Tunings">
  Access to the Tunings API for model fine-tuning.

  See [tunings.tune](/api/tunings/tune) for details.
</ResponseField>

### auth\_tokens

<ResponseField name="auth_tokens" type="Tokens">
  Access to authentication token management.
</ResponseField>

### operations

<ResponseField name="operations" type="Operations">
  Access to long-running operations management.
</ResponseField>

### interactions

<ResponseField name="interactions" type="InteractionsResource">
  Access to the experimental Interactions API for live, streaming interactions.

  <Warning>
    This API is experimental and may change in future versions.
  </Warning>
</ResponseField>

### vertexai

<ResponseField name="vertexai" type="bool">
  Returns whether the client is using the Vertex AI API.
</ResponseField>

```python theme={null}
if client.vertexai:
    print("Using Vertex AI API")
else:
    print("Using Gemini Developer API")
```

## Methods

### close()

Closes the synchronous client explicitly and releases resources.

<Note>
  This method does not close the async client. Use `client.aio.aclose()` or the async context manager to close the async client.
</Note>

```python theme={null}
from google.genai import Client

client = Client(
    vertexai=True,
    project='my-project-id',
    location='us-central1'
)

response_1 = client.models.generate_content(
    model='gemini-2.0-flash',
    contents='Hello World'
)

response_2 = client.models.generate_content(
    model='gemini-2.0-flash',
    contents='Goodbye World'
)

# Close the client to release resources
client.close()
```

### Context Manager Methods

The Client supports the context manager protocol for automatic resource cleanup.

#### \_\_enter\_\_()

Enters the runtime context and returns the client.

#### \_\_exit\_\_(exc\_type, exc\_value, traceback)

Exits the runtime context and closes the client.

```python theme={null}
with genai.Client(api_key='my-api-key') as client:
    # Use the client
    response = client.models.generate_content(
        model='gemini-2.0-flash',
        contents='Hello'
    )
# Client is automatically closed here
```

## HttpOptions

HTTP configuration options for customizing client behavior.

<ParamField path="base_url" type="str">
  The base URL for the AI platform service endpoint.
</ParamField>

<ParamField path="api_version" type="str">
  Specifies the version of the API to use.
</ParamField>

<ParamField path="headers" type="dict[str, str]">
  Additional HTTP headers to be sent with each request.
</ParamField>

<ParamField path="timeout" type="int">
  Timeout for requests in milliseconds.
</ParamField>

<ParamField path="client_args" type="dict[str, Any]">
  Arguments passed to the HTTP client.
</ParamField>

<ParamField path="async_client_args" type="dict[str, Any]">
  Arguments passed to the async HTTP client.
</ParamField>

<ParamField path="extra_body" type="dict[str, Any]">
  Extra parameters to add to the request body. The structure must match the backend API's request structure:

  * [VertexAI backend API docs](https://cloud.google.com/vertex-ai/docs/reference/rest)
  * [GeminiAPI backend API docs](https://ai.google.dev/api/rest)
</ParamField>

<ParamField path="retry_options" type="HttpRetryOptions">
  HTTP retry options for requests. See [HttpRetryOptions](#httpretryoptions).
</ParamField>

<ParamField path="httpx_client" type="httpx.Client">
  A custom httpx client to be used for requests.
</ParamField>

<ParamField path="httpx_async_client" type="httpx.AsyncClient">
  A custom httpx async client to be used for requests.
</ParamField>

<ParamField path="aiohttp_client" type="aiohttp.ClientSession">
  A custom aiohttp client session to be used for requests.
</ParamField>

## HttpRetryOptions

Configuration options for HTTP request retries.

<ParamField path="attempts" type="int" default="5">
  Maximum number of attempts, including the original request. If 0 or 1, it means no retries.
</ParamField>

<ParamField path="initial_delay" type="float" default="1.0">
  Initial delay before the first retry, in seconds.
</ParamField>

<ParamField path="max_delay" type="float" default="60.0">
  Maximum delay between retries, in seconds.
</ParamField>

<ParamField path="exp_base" type="float" default="2.0">
  Multiplier by which the delay increases after each attempt.
</ParamField>

<ParamField path="jitter" type="float" default="1.0">
  Randomness factor for the delay.
</ParamField>

<ParamField path="http_status_codes" type="list[int]">
  List of HTTP status codes that should trigger a retry. If not specified, a default set of retryable codes (408, 429, and 5xx) may be used.
</ParamField>

```python theme={null}
from google import genai
from google.genai import types

client = genai.Client(
    api_key='my-api-key',
    http_options=types.HttpOptions(
        retry_options=types.HttpRetryOptions(
            attempts=3,
            initial_delay=0.5,
            max_delay=10.0
        )
    )
)
```

## See Also

* [AsyncClient](/api/async-client) - Asynchronous client for non-blocking operations
* [generate\_content](/api/models/generate-content) - Generate content with models
* [chats.create](/api/chats/create) - Multi-turn conversations
* [files.upload](/api/files/upload) - Upload and manage media files
