8080 pass
8181
8282
83+ try :
84+ import httpx2
85+ except ImportError :
86+ httpx2 = None # type: ignore[assignment]
87+
88+
8389if TYPE_CHECKING :
8490 from multidict import CIMultiDictProxy
8591
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
97124class 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 )
0 commit comments