Skip to content

fix(py): accept custom httpx clients in google-genai http_options - #6691

Closed
huangjeff5 wants to merge 1 commit into
genkit-ai:mainfrom
huangjeff5:fix/py-genai-http-options-client
Closed

huangjeff5 wants to merge 1 commit into
genkit-ai:mainfrom
huangjeff5:fix/py-genai-http-options-client

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

GoogleAI and VertexAI now accept a caller-owned httpx client in http_options and use it for every request. Before this, passing one crashed at plugin construction.

google-genai supports HttpOptions(httpx_async_client=...) for proxies, mTLS and custom transports. The plugin deep-copied http_options in three places so plugin-added headers would never leak into the caller's object. Deep-copying an httpx client tries to pickle its internal RLock.

import httpx
from google.genai import types as genai_types
from genkit_google_genai import GoogleAI

# --- BEFORE ---
GoogleAI(api_key='...', http_options=genai_types.HttpOptions(httpx_async_client=httpx.AsyncClient()))
# => TypeError: cannot pickle '_thread.RLock' object

# --- AFTER ---
# 1. Route all Gemini traffic through a corporate proxy
client = httpx.AsyncClient(proxy='http://proxy.internal:3128')
ai_plugin = GoogleAI(api_key='...', http_options=genai_types.HttpOptions(httpx_async_client=client))

# 2. The model list GET and every generateContent call use that client
# 3. The caller's HttpOptions is unchanged after requests with base_url/api_version/location overrides

Decisions

  • Copy the options, share the client: the new copy_http_options() makes a shallow copy and gives it fresh headers, client_args and async_client_args dicts. It deep-copies only the JSON-shaped extra_body and retry_options, and keeps httpx_client, httpx_async_client and aiohttp_client by reference. It replaces the three model_copy(deep=True) calls in _google.py, _gemini.py and _veo.py. Sharing the client is safe because google-genai doesn't close user-supplied clients when it disposes of a per-request client.
  • The non-mutation guarantee stays: the deep copy existed so the attribution headers (appended in place) and the base_url/api_version/location overrides never reach a caller-owned HttpOptions, which may be shared across plugin instances. Those writes now land on the copy's fresh dicts. Tests check that the original keeps base_url=None, api_version=None, its headers, its extra_body and the same client object after overridden requests on both backends.

Verification

  • New tests/http_options_client_test.py, 6 tests, all failing without the fix:
    • GoogleAI and VertexAI route the GET and generateContent through a user httpx.AsyncClient(transport=MockTransport), including per-request base URL and location overrides.
    • Dict-form http_options.
    • A sync httpx_client.
    • A unit test of copy_http_options.
  • Ran ruff, pyrefly, pyright packages/*/src, ty and uv lock --check: all clean. pytest: 5217 passed.

GoogleAI/VertexAI deep-copied the caller's HttpOptions, so a user
httpx_client / httpx_async_client (RLock inside) raised
"TypeError: cannot pickle '_thread.RLock' object" at construction.

copy_http_options gives the copy fresh headers/client_args/extra_body/
retry_options and shares the transport clients by reference. Plugin
setup and per-request base_url/api_version/location overrides still
leave the caller's object untouched.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a helper function copy_http_options to copy HttpOptions without deep-copying transport clients (such as httpx or aiohttp clients), which are not deep-copyable and should remain shared. This helper is integrated across the Google GenAI, Gemini, and Veo model configurations, and a comprehensive test suite is added to verify the behavior. The reviewer suggested making copy_http_options more robust against different SDK versions by using getattr with a default value of None to avoid potential AttributeError exceptions if fields are renamed or missing.

Comment on lines +37 to +44
for field in ('headers', 'client_args', 'async_client_args'):
value = getattr(opts, field)
if value is not None:
update[field] = dict(value)
if opts.extra_body is not None:
update['extra_body'] = copy.deepcopy(opts.extra_body)
if opts.retry_options is not None:
update['retry_options'] = opts.retry_options.model_copy(deep=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make copy_http_options more robust against different versions of the google-genai SDK (where some fields like async_client_args or retry_options might be renamed, deprecated, or not yet present in older versions), it is safer to use getattr(opts, field, None) instead of direct attribute access or getattr without a default value. This prevents potential AttributeError exceptions.

Suggested change
for field in ('headers', 'client_args', 'async_client_args'):
value = getattr(opts, field)
if value is not None:
update[field] = dict(value)
if opts.extra_body is not None:
update['extra_body'] = copy.deepcopy(opts.extra_body)
if opts.retry_options is not None:
update['retry_options'] = opts.retry_options.model_copy(deep=True)
for field in ('headers', 'client_args', 'async_client_args'):
value = getattr(opts, field, None)
if value is not None:
update[field] = dict(value)
extra_body = getattr(opts, 'extra_body', None)
if extra_body is not None:
update['extra_body'] = copy.deepcopy(extra_body)
retry_options = getattr(opts, 'retry_options', None)
if retry_options is not None:
update['retry_options'] = retry_options.model_copy(deep=True)

@huangjeff5

Copy link
Copy Markdown
Contributor Author

Moved to #6695 so the branch lives in genkit-ai/genkit.

@huangjeff5 huangjeff5 closed this Oct 8, 2026
@huangjeff5
huangjeff5 deleted the fix/py-genai-http-options-client branch October 8, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant