Skip to content

Commit 012804d

Browse files
yinghsienwucopybara-github
authored andcommitted
feat: Support injecting httpx2 client.
Closes #2680 PiperOrigin-RevId: 963182265
1 parent 66bfe95 commit 012804d

8 files changed

Lines changed: 265 additions & 31 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
pass
8181

8282

83+
try:
84+
import httpx2
85+
except ImportError:
86+
httpx2 = None # type: ignore[assignment]
87+
88+
8389
if TYPE_CHECKING:
8490
from multidict import CIMultiDictProxy
8591

@@ -93,6 +99,27 @@
9399

94100
_MULTI_REGIONAL_LOCATIONS = {'us', 'eu'}
95101

102+
# httpx2 (https://github.com/pydantic/httpx2) is a drop-in fork of httpx under a
103+
# separate import namespace, so its classes are not instances of the httpx
104+
# equivalents. Widen the runtime type checks to accept either when httpx2 is
105+
# installed.
106+
_HTTPX_RESPONSE_TYPES = (
107+
(httpx.Response,) if httpx2 is None else (httpx.Response, httpx2.Response)
108+
)
109+
_HTTPX_HEADERS_TYPES = (
110+
(httpx.Headers,) if httpx2 is None else (httpx.Headers, httpx2.Headers)
111+
)
112+
_HTTPX_TRANSIENT_EXC = (
113+
(httpx.TimeoutException, httpx.ConnectError)
114+
if httpx2 is None
115+
else (
116+
httpx.TimeoutException,
117+
httpx.ConnectError,
118+
httpx2.TimeoutException,
119+
httpx2.ConnectError,
120+
)
121+
)
122+
96123

97124
class EphemeralTokenAPIKeyError(ValueError):
98125
"""Error raised when the API key is invalid."""
@@ -256,9 +283,9 @@ def __init__(
256283
):
257284
if isinstance(headers, dict):
258285
self.headers = headers
259-
elif isinstance(headers, httpx.Headers):
286+
elif isinstance(headers, _HTTPX_HEADERS_TYPES):
260287
self.headers = {
261-
key: ', '.join(headers.get_list(key)) for key in headers.keys()
288+
key: ', '.join(headers.get_list(key)) for key in headers.keys() # type: ignore[attr-defined]
262289
}
263290
elif isinstance(headers, CaseInsensitiveDict):
264291
self.headers = {key: value for key, value in headers.items()}
@@ -339,7 +366,7 @@ def _copy_to_dict(self, response_payload: dict[str, object]) -> None:
339366
def _iter_response_stream(self) -> Iterator[str]:
340367
"""Iterates over chunks retrieved from the API."""
341368
if not (
342-
isinstance(self.response_stream, httpx.Response)
369+
isinstance(self.response_stream, _HTTPX_RESPONSE_TYPES)
343370
or isinstance(self.response_stream, requests.Response)
344371
):
345372
raise TypeError(
@@ -350,7 +377,7 @@ def _iter_response_stream(self) -> Iterator[str]:
350377
chunk = ''
351378
balance = 0
352379
data_buffer: list[str] = []
353-
if isinstance(self.response_stream, httpx.Response):
380+
if isinstance(self.response_stream, _HTTPX_RESPONSE_TYPES):
354381
response_stream = self.response_stream.iter_lines()
355382
else:
356383
response_stream = self.response_stream.iter_lines(decode_unicode=True)
@@ -389,7 +416,9 @@ def _iter_response_stream(self) -> Iterator[str]:
389416

390417
async def _aiter_response_stream(self) -> AsyncIterator[str]:
391418
"""Asynchronously iterates over chunks retrieved from the API."""
392-
is_valid_response = isinstance(self.response_stream, httpx.Response) or (
419+
is_valid_response = isinstance(
420+
self.response_stream, _HTTPX_RESPONSE_TYPES
421+
) or (
393422
has_aiohttp and isinstance(self.response_stream, aiohttp.ClientResponse)
394423
)
395424
if not is_valid_response:
@@ -403,9 +432,10 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
403432
balance = 0
404433
data_buffer: list[str] = []
405434
# httpx.Response has a dedicated async line iterator.
406-
if isinstance(self.response_stream, httpx.Response):
435+
if isinstance(self.response_stream, _HTTPX_RESPONSE_TYPES):
407436
try:
408-
async for line in self.response_stream.aiter_lines():
437+
response_stream: Any = self.response_stream
438+
async for line in response_stream.aiter_lines():
409439
if not line:
410440
if data_buffer:
411441
yield '\n'.join(data_buffer)
@@ -437,7 +467,7 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
437467
yield '\n'.join(data_buffer)
438468
finally:
439469
# Close the response and release the connection.
440-
await self.response_stream.aclose()
470+
await response_stream.aclose()
441471

442472
# aiohttp.ClientResponse uses a content stream that we read line by line.
443473
elif has_aiohttp and isinstance(
@@ -540,7 +570,7 @@ def retry_args(options: Optional[HttpRetryOptions]) -> _common.StringDict:
540570
retriable_codes = options.http_status_codes or _RETRY_HTTP_STATUS_CODES
541571
retry = tenacity.retry_if_exception(
542572
lambda e: (isinstance(e, errors.APIError) and e.code in retriable_codes)
543-
or isinstance(e, (httpx.TimeoutException, httpx.ConnectError)),
573+
or isinstance(e, _HTTPX_TRANSIENT_EXC),
544574
)
545575
wait = tenacity.wait_exponential_jitter(
546576
initial=options.initial_delay or _RETRY_INITIAL_DELAY,
@@ -1468,7 +1498,7 @@ def _request_once(
14681498
headers=http_request.headers,
14691499
timeout=http_request.timeout,
14701500
)
1471-
response = self._httpx_client.send(httpx_request, stream=stream) # type: ignore[union-attr]
1501+
response = self._httpx_client.send(httpx_request, stream=stream) # type: ignore[union-attr, arg-type]
14721502
errors.APIError.raise_for_response(response)
14731503
return HttpResponse(
14741504
response.headers, response if stream else [response.text]
@@ -1582,7 +1612,7 @@ async def _async_request_once(
15821612
timeout=http_request.timeout,
15831613
)
15841614
client_response = await self._async_httpx_client.send( # type: ignore[union-attr]
1585-
httpx_request,
1615+
httpx_request, # type: ignore[arg-type]
15861616
stream=stream,
15871617
)
15881618
await errors.APIError.raise_for_async_response(client_response)

‎google/genai/_extra_utils.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,11 @@ async def get_function_response_parts_async(
389389
mcp_tool_response = await func.call_tool(
390390
types.FunctionCall(name=func_name, args=args)
391391
)
392-
if mcp_tool_response.isError:
392+
if getattr(
393+
mcp_tool_response,
394+
'is_error',
395+
getattr(mcp_tool_response, 'isError', False),
396+
):
393397
func_response = {'error': mcp_tool_response}
394398
else:
395399
func_response = {'result': mcp_tool_response}

‎google/genai/_mcp_utils.py‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"""Utils for working with MCP tools."""
1717
import contextlib
1818
import httpx
19+
20+
try:
21+
import httpx2
22+
except ImportError:
23+
httpx2 = None # type: ignore[assignment]
1924
import sys
2025

2126
from importlib.metadata import PackageNotFoundError, version
@@ -51,7 +56,11 @@ def mcp_to_gemini_tool(tool: McpTool) -> types.Tool:
5156
"description": tool.description,
5257
"parameters": types.Schema.from_json_schema(
5358
json_schema=types.JSONSchema(
54-
**_filter_to_supported_schema(tool.inputSchema)
59+
**_filter_to_supported_schema(
60+
getattr(
61+
tool, "input_schema", getattr(tool, "inputSchema", {})
62+
)
63+
)
5564
)
5665
),
5766
}]
@@ -61,13 +70,13 @@ def mcp_to_gemini_tool(tool: McpTool) -> types.Tool:
6170
def agent_platform_to_gemini_tool(tool: McpTool) -> types.Tool:
6271
"""Translates an Agent Platform tool to a Google GenAI tool."""
6372
return types.Tool(
64-
function_declarations=[
65-
{
66-
"name": tool.name,
67-
"description": tool.description,
68-
"parameters_json_schema": tool.inputSchema,
69-
}
70-
]
73+
function_declarations=[{
74+
"name": tool.name,
75+
"description": tool.description,
76+
"parameters_json_schema": getattr(
77+
tool, "input_schema", getattr(tool, "inputSchema", {})
78+
),
79+
}]
7180
)
7281

7382

@@ -201,14 +210,19 @@ async def _connect_agent_platform_mcp(api_client: Any, toolset_name: str) -> typ
201210

202211
set_mcp_usage_header(headers)
203212

204-
http_client = httpx.AsyncClient(headers=headers, timeout=None)
213+
http_client: Any
214+
if httpx2 is not None:
215+
http_client = httpx2.AsyncClient(headers=headers, timeout=None)
216+
else:
217+
http_client = httpx.AsyncClient(headers=headers, timeout=None)
205218

206219
try:
207220
async with http_client:
208221
async with streamable_http_client(
209222
url=mcp_url, http_client=http_client
210223
) as streams:
211-
read_stream, write_stream, _ = streams
224+
read_stream = streams[0]
225+
write_stream = streams[1]
212226
async with McpClientSession(read_stream, write_stream) as session:
213227
await session.initialize()
214228
try:

‎google/genai/errors.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
import httpx
2020
import json
2121
import requests
22+
23+
try:
24+
import httpx2
25+
except ImportError:
26+
httpx2 = None # type: ignore[assignment]
27+
2228
from . import _common
2329

2430

@@ -28,6 +34,15 @@
2834
from google.auth.aio.transport.aiohttp import Response as AsyncAuthorizedSessionResponse
2935

3036

37+
# httpx2 (https://github.com/pydantic/httpx2) is a drop-in fork of httpx under a
38+
# separate import namespace, so its Response is not an instance of
39+
# httpx.Response. Widen the runtime type checks to accept either when httpx2 is
40+
# installed.
41+
_HTTPX_RESPONSE_TYPES = (
42+
(httpx.Response,) if httpx2 is None else (httpx.Response, httpx2.Response)
43+
)
44+
45+
3146
class APIError(Exception):
3247
"""General errors raised by the GenAI API."""
3348
code: int
@@ -129,7 +144,7 @@ def raise_for_response(
129144
if response.status_code == 200:
130145
return
131146

132-
if isinstance(response, httpx.Response):
147+
if isinstance(response, _HTTPX_RESPONSE_TYPES):
133148
try:
134149
response.read()
135150
response_json = response.json()
@@ -198,7 +213,7 @@ async def raise_for_async_response(
198213
],
199214
) -> None:
200215
"""Raises an error with detailed error message if the response has an error status."""
201-
if isinstance(response, httpx.Response):
216+
if isinstance(response, _HTTPX_RESPONSE_TYPES):
202217
if response.status_code == 200:
203218
return
204219
try:

0 commit comments

Comments
 (0)