Repository navigation
fix(py): accept custom httpx clients in google-genai http_options - #6691
huangjeff5 wants to merge 1 commit into
Conversation
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.
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
| 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) |
|
Moved to #6695 so the branch lives in genkit-ai/genkit. |
GoogleAIandVertexAInow accept a caller-owned httpx client inhttp_optionsand 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-copiedhttp_optionsin three places so plugin-added headers would never leak into the caller's object. Deep-copying an httpx client tries to pickle its internalRLock.Decisions
copy_http_options()makes a shallow copy and gives it freshheaders,client_argsandasync_client_argsdicts. It deep-copies only the JSON-shapedextra_bodyandretry_options, and keepshttpx_client,httpx_async_clientandaiohttp_clientby reference. It replaces the threemodel_copy(deep=True)calls in_google.py,_gemini.pyand_veo.py. Sharing the client is safe because google-genai doesn't close user-supplied clients when it disposes of a per-request client.base_url/api_version/locationoverrides never reach a caller-ownedHttpOptions, which may be shared across plugin instances. Those writes now land on the copy's fresh dicts. Tests check that the original keepsbase_url=None,api_version=None, its headers, itsextra_bodyand the same client object after overridden requests on both backends.Verification
tests/http_options_client_test.py, 6 tests, all failing without the fix:generateContentthrough a userhttpx.AsyncClient(transport=MockTransport), including per-request base URL and location overrides.http_options.httpx_client.copy_http_options.packages/*/src, ty anduv lock --check: all clean. pytest: 5217 passed.