Skip to content

Async call fails with mTLS endpoint when CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE=True #2282

@jorsj

Description

@jorsj

When running the google-genai SDK with vertexai=True and the environment variable CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE=True, asynchronous calls fail with an error indicating a failure to send the request to the mTLS endpoint. This is followed by an "Unclosed client session" warning. Synchronous calls work as expected under the same conditions.

When CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE is set to False (or unset), both sync and async calls succeed.

Reproduction Steps

  1. Set the environment variable:
export CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE=True
  1. Run the following Python script:
import asyncio
import os
from google import genai
from google.genai import types

async def main():
    client = genai.Client(
        project=os.environ.get("GOOGLE_CLOUD_PROJECT", "your-project-id"),
        vertexai=True,
        location="us-central1",
        http_options=types.HttpOptions(
            api_version="v1",
        ),
    )

    model_name = "gemini-2.5-flash"

    # 1. Try Sync (Works)
    try:
        print("Trying sync call...")
        response = client.models.generate_content(
            model=model_name,
            contents="Hello",
        )
        print("Sync Success!")
    except Exception as e:
        print(f"Sync Error: {e}")

    # 2. Try Async (Fails)
    try:
        print("\nTrying async call...")
        response = await client.aio.models.generate_content(
            model=model_name,
            contents="Hello",
        )
        print("Async Success!")
    except Exception as e:
        print(f"Async Error: {e}")

if __name__ == "__main__":
    asyncio.run(main())

Expected Behavior

Both sync and async calls should succeed, or if mTLS is not supported for async calls in this configuration, it should fail with a clear error message without leaking the client session.

Actual Behavior

The sync call succeeds, but the async call fails with:

Trying sync call...
Sync Success!

Trying async call...
Async Error: Failed to send request to https://us-central1-aiplatform.mtls.googleapis.com/v1/projects/sandcastle-401718/locations/us-central1/publishers/google/models/gemini-2.5-flash:generateContent.
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x11bf0d6a0>

Environment

  • OS: macOS
  • Library Version: google-genai>=1.73.0
  • Python Version: >= 3.13 (as per project requirements)

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions