Skip to content

Commit 743896d

Browse files
MarkDaoustcopybara-github
authored andcommitted
feat(client): Support httpx2 with GOOGLE_GENAI_HTTP_CLIENT configuration
Support httpx2 as an opt-in HTTP transport via GOOGLE_GENAI_HTTP_CLIENT=httpx2 or custom client injection, while preserving httpx as the default in minor releases for backward compatibility. #2680 PiperOrigin-RevId: 979252017
1 parent b88fded commit 743896d

6 files changed

Lines changed: 290 additions & 74 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 181 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
from google.auth.credentials import Credentials
4747
from google.auth.transport import mtls
4848
from google.auth import exceptions as auth_exceptions
49-
import httpx
49+
try:
50+
import httpx
51+
except ImportError:
52+
httpx = None # type: ignore[assignment]
5053
from pydantic import BaseModel
5154
from pydantic import ValidationError
5255
import tenacity
@@ -100,23 +103,33 @@
100103

101104
# httpx2 (https://github.com/pydantic/httpx2) is a drop-in fork of httpx under a
102105
# separate import namespace, so its classes are not instances of the httpx
103-
# equivalents. Widen the runtime type checks to accept either when httpx2 is
104-
# installed.
105-
_HTTPX_RESPONSE_TYPES = (
106-
(httpx.Response,) if httpx2 is None else (httpx.Response, httpx2.Response)
106+
# equivalents. Widen the runtime type checks to accept either when httpx or
107+
# httpx2 is installed.
108+
_HTTPX_RESPONSE_TYPES = tuple(
109+
cls
110+
for cls in (
111+
getattr(httpx, 'Response', None),
112+
getattr(httpx2, 'Response', None),
113+
)
114+
if cls is not None
107115
)
108-
_HTTPX_HEADERS_TYPES = (
109-
(httpx.Headers,) if httpx2 is None else (httpx.Headers, httpx2.Headers)
116+
_HTTPX_HEADERS_TYPES = tuple(
117+
cls
118+
for cls in (
119+
getattr(httpx, 'Headers', None),
120+
getattr(httpx2, 'Headers', None),
121+
)
122+
if cls is not None
110123
)
111-
_HTTPX_TRANSIENT_EXC = (
112-
(httpx.TimeoutException, httpx.ConnectError)
113-
if httpx2 is None
114-
else (
115-
httpx.TimeoutException,
116-
httpx.ConnectError,
117-
httpx2.TimeoutException,
118-
httpx2.ConnectError,
124+
_HTTPX_TRANSIENT_EXC = tuple(
125+
cls
126+
for cls in (
127+
getattr(httpx, 'TimeoutException', None),
128+
getattr(httpx, 'ConnectError', None),
129+
getattr(httpx2, 'TimeoutException', None),
130+
getattr(httpx2, 'ConnectError', None),
119131
)
132+
if cls is not None
120133
)
121134

122135

@@ -273,7 +286,7 @@ def __init__(
273286
self,
274287
headers: Union[
275288
dict[str, str],
276-
httpx.Headers,
289+
Any,
277290
'CIMultiDictProxy[str]',
278291
'CaseInsensitiveDict',
279292
],
@@ -282,7 +295,9 @@ def __init__(
282295
):
283296
if isinstance(headers, dict):
284297
self.headers = headers
285-
elif isinstance(headers, _HTTPX_HEADERS_TYPES):
298+
elif isinstance(headers, _HTTPX_HEADERS_TYPES) or hasattr(
299+
headers, 'get_list'
300+
):
286301
self.headers = {
287302
key: ', '.join(headers.get_list(key)) for key in headers.keys() # type: ignore[attr-defined]
288303
}
@@ -594,45 +609,135 @@ def retry_args(options: Optional[HttpRetryOptions]) -> _common.StringDict:
594609
}
595610

596611

597-
class SyncHttpxClient(httpx.Client):
598-
"""Sync httpx client."""
612+
if httpx is not None:
599613

600-
def __init__(self, **kwargs: Any) -> None:
601-
"""Initializes the httpx client."""
602-
kwargs.setdefault('follow_redirects', True)
603-
super().__init__(**kwargs)
614+
class SyncHttpxClient(httpx.Client):
615+
"""Sync httpx client."""
604616

605-
def __del__(self) -> None:
606-
"""Closes the httpx client."""
607-
try:
608-
if self.is_closed:
609-
return
610-
except Exception:
611-
pass
612-
try:
613-
self.close()
614-
except Exception:
615-
pass
617+
def __init__(self, **kwargs: Any) -> None:
618+
"""Initializes the httpx client."""
619+
kwargs.setdefault('follow_redirects', True)
620+
super().__init__(**kwargs)
616621

622+
def __del__(self) -> None:
623+
"""Closes the httpx client."""
624+
try:
625+
if self.is_closed:
626+
return
627+
except Exception:
628+
pass
629+
try:
630+
self.close()
631+
except Exception:
632+
pass
617633

618-
class AsyncHttpxClient(httpx.AsyncClient):
619-
"""Async httpx client."""
620634

621-
def __init__(self, **kwargs: Any) -> None:
622-
"""Initializes the httpx client."""
623-
kwargs.setdefault('follow_redirects', True)
624-
super().__init__(**kwargs)
635+
class AsyncHttpxClient(httpx.AsyncClient):
636+
"""Async httpx client."""
625637

626-
def __del__(self) -> None:
627-
try:
628-
if self.is_closed:
629-
return
630-
except Exception:
631-
pass
632-
try:
633-
asyncio.get_running_loop().create_task(self.aclose())
634-
except Exception:
635-
pass
638+
def __init__(self, **kwargs: Any) -> None:
639+
"""Initializes the httpx client."""
640+
kwargs.setdefault('follow_redirects', True)
641+
super().__init__(**kwargs)
642+
643+
def __del__(self) -> None:
644+
try:
645+
if self.is_closed:
646+
return
647+
except Exception:
648+
pass
649+
try:
650+
asyncio.get_running_loop().create_task(self.aclose())
651+
except Exception:
652+
pass
653+
654+
else:
655+
SyncHttpxClient = None # type: ignore[assignment,misc]
656+
AsyncHttpxClient = None # type: ignore[assignment,misc]
657+
658+
659+
if httpx2 is not None:
660+
661+
class SyncHttpx2Client(httpx2.Client):
662+
"""Sync httpx2 client."""
663+
664+
def __init__(self, **kwargs: Any) -> None:
665+
"""Initializes the httpx2 client."""
666+
kwargs.setdefault('follow_redirects', True)
667+
super().__init__(**kwargs)
668+
669+
def __del__(self) -> None:
670+
"""Closes the httpx2 client."""
671+
try:
672+
if self.is_closed:
673+
return
674+
except Exception:
675+
pass
676+
try:
677+
self.close()
678+
except Exception:
679+
pass
680+
681+
682+
class AsyncHttpx2Client(httpx2.AsyncClient):
683+
"""Async httpx2 client."""
684+
685+
def __init__(self, **kwargs: Any) -> None:
686+
"""Initializes the httpx2 client."""
687+
kwargs.setdefault('follow_redirects', True)
688+
super().__init__(**kwargs)
689+
690+
def __del__(self) -> None:
691+
try:
692+
if self.is_closed:
693+
return
694+
except Exception:
695+
pass
696+
try:
697+
asyncio.get_running_loop().create_task(self.aclose())
698+
except Exception:
699+
pass
700+
701+
else:
702+
SyncHttpx2Client = None # type: ignore[assignment,misc]
703+
AsyncHttpx2Client = None # type: ignore[assignment,misc]
704+
705+
706+
def _get_http_client_backend() -> str:
707+
"""Determines whether to use 'httpx' or 'httpx2' as the default HTTP client.
708+
709+
Can be controlled via the GOOGLE_GENAI_HTTP_CLIENT environment variable:
710+
- 'httpx2': Use httpx2 (raises ImportError if not installed).
711+
- 'httpx': Use httpx (raises ImportError if not installed).
712+
- 'auto' or unset: Default to httpx if installed, otherwise fall back to httpx2.
713+
"""
714+
env = os.environ.get('GOOGLE_GENAI_HTTP_CLIENT', '').lower().strip()
715+
if env == 'httpx2':
716+
if httpx2 is None:
717+
raise ImportError(
718+
'httpx2 is configured via GOOGLE_GENAI_HTTP_CLIENT=httpx2, '
719+
'but httpx2 is not installed.'
720+
)
721+
return 'httpx2'
722+
elif env == 'httpx':
723+
if httpx is None:
724+
raise ImportError(
725+
'httpx is configured via GOOGLE_GENAI_HTTP_CLIENT=httpx, '
726+
'but httpx is not installed.'
727+
)
728+
return 'httpx'
729+
elif env and env != 'auto':
730+
logger.warning(
731+
'Unrecognized GOOGLE_GENAI_HTTP_CLIENT=%r; falling back to default.', env
732+
)
733+
734+
if httpx is not None:
735+
return 'httpx'
736+
if httpx2 is not None:
737+
return 'httpx2'
738+
raise ImportError(
739+
'Neither httpx nor httpx2 is installed. Please install httpx2 or httpx.'
740+
)
636741

637742

638743
class BaseApiClient:
@@ -853,9 +958,18 @@ def __init__(
853958
if self._http_options.headers is not None:
854959
append_library_version_headers(self._http_options.headers)
855960

961+
backend = _get_http_client_backend()
962+
if backend == 'httpx2':
963+
sync_client_cls: type[Any] = SyncHttpx2Client
964+
async_client_cls: type[Any] = AsyncHttpx2Client
965+
else:
966+
sync_client_cls = SyncHttpxClient
967+
async_client_cls = AsyncHttpxClient
968+
856969
client_args, async_client_args = self._ensure_httpx_ssl_ctx(
857970
self._http_options,
858971
vertexai=bool(self.vertexai),
972+
client_cls=sync_client_cls,
859973
)
860974
self._async_httpx_client_args = async_client_args
861975
self._authorized_session: Optional['AuthorizedSession'] = None
@@ -865,19 +979,14 @@ def __init__(
865979
elif self._http_options.httpx_client:
866980
self._httpx_client = self._http_options.httpx_client
867981
else:
868-
self._httpx_client = SyncHttpxClient(**client_args)
982+
self._httpx_client = sync_client_cls(**client_args)
869983

870984
if self._use_google_auth_async():
871985
self._async_httpx_client = None
872986
elif self._http_options.httpx_async_client:
873987
self._async_httpx_client = self._http_options.httpx_async_client
874988
else:
875-
self._async_httpx_client = AsyncHttpxClient(**async_client_args)
876-
877-
if self._http_options.httpx_async_client:
878-
self._async_httpx_client = self._http_options.httpx_async_client
879-
else:
880-
self._async_httpx_client = AsyncHttpxClient(**async_client_args)
989+
self._async_httpx_client = async_client_cls(**async_client_args)
881990

882991
# Initialize the aiohttp client sessions.
883992
self._aiohttp_sessions: dict[Any, Any] = {}
@@ -1070,6 +1179,7 @@ def __del__(self, _warnings: Any = warnings) -> None:
10701179
def _ensure_httpx_ssl_ctx(
10711180
options: HttpOptions,
10721181
vertexai: bool = False,
1182+
client_cls: Optional[type[Any]] = None,
10731183
) -> Tuple[_common.StringDict, _common.StringDict]:
10741184
"""Ensures the SSL context is present in the HTTPX client args.
10751185
@@ -1078,6 +1188,7 @@ def _ensure_httpx_ssl_ctx(
10781188
Args:
10791189
options: The http options to check for SSL context.
10801190
vertexai: Whether Vertex AI is enabled.
1191+
client_cls: The client class to inspect parameters for (default: httpx.Client).
10811192
10821193
Returns:
10831194
A tuple of sync/async httpx client args.
@@ -1133,12 +1244,19 @@ def _maybe_set(
11331244
args[verify] = ctx
11341245
if 'timeout' not in args:
11351246
args['timeout'] = None
1136-
# Drop the args that isn't used by the httpx client.
1137-
copied_args = args.copy()
1138-
for key in copied_args.copy():
1139-
if key not in inspect.signature(httpx.Client.__init__).parameters:
1140-
del copied_args[key]
1141-
return copied_args
1247+
allowed_params = set()
1248+
if httpx is not None:
1249+
allowed_params.update(
1250+
inspect.signature(httpx.Client.__init__).parameters
1251+
)
1252+
if httpx2 is not None:
1253+
allowed_params.update(
1254+
inspect.signature(httpx2.Client.__init__).parameters
1255+
)
1256+
for key in list(args.keys()):
1257+
if key not in allowed_params:
1258+
del args[key]
1259+
return args
11421260

11431261
return (
11441262
_maybe_set(args, ctx),

0 commit comments

Comments
 (0)