Skip to content

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

Closed
huangjeff5 wants to merge 3 commits into
mainfrom
fix/py-genai-http-options-client
Closed

huangjeff5 wants to merge 3 commits into
mainfrom
fix/py-genai-http-options-client

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

GoogleAI and VertexAI now accept a caller-owned httpx client in http_options and route every request through it. Previously, passing one crashed at plugin construction.

google-genai supports HttpOptions(httpx_async_client=...) for proxies, mTLS, and custom transports. The plugin previously called model_copy(deep=True) on http_options in three places so plugin-added headers would never mutate the caller's object, which attempted to pickle the httpx client's internal _thread.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')
opts = genai_types.HttpOptions(httpx_async_client=client)
ai_plugin = GoogleAI(api_key='...', http_options=opts)

# 2. Both models.list and generateContent use that client
# 3. opts is unchanged after requests with base_url/api_version/location overrides

Decisions

  • copy_http_options() replaces the three model_copy(deep=True) calls in _google.py, _gemini.py, and _veo.py. It makes a shallow copy with fresh headers, client_args, and async_client_args dicts, deep-copies only the JSON-shaped extra_body and retry_options, and keeps httpx_client, httpx_async_client, and aiohttp_client by reference. Sharing the client reference is safe because google-genai does not close caller-supplied clients when disposing of a per-request client.
  • Attribution headers and per-request base_url, api_version, and location overrides write to the copied container dicts, so a caller's HttpOptions instance (which may be shared across plugin instances) is never mutated.

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 copy_http_options helper function to safely copy HttpOptions while keeping transport clients (like httpx and aiohttp clients) shared, preventing deep-copy errors. This helper is integrated across Gemini, Veo, and Google GenAI models, and comprehensive tests are added. Feedback suggests making the copying of retry_options more robust by checking for the model_copy attribute to prevent potential AttributeErrors if a dictionary is passed instead of a model instance.

…ions-client

# Conflicts:
#	py/packages/genkit-google-genai/src/genkit_google_genai/_models/_gemini.py
@huangjeff5

Copy link
Copy Markdown
Contributor Author

Superseded by #6826: GoogleAI/VertexAI now take http_client= directly instead of the SDK's HttpOptions.

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