@@ -41,6 +41,31 @@ def _check_vertex_dependencies() -> None:
4141from llama_index .llms .google_genai import GoogleGenAI
4242
4343
44+ def _rewrite_request_for_gateway (
45+ request : httpx .Request , gateway_url : str
46+ ) -> httpx .Request :
47+ """Rewrite a request to redirect to the UiPath gateway."""
48+ url_str = str (request .url )
49+ if "generateContent" in url_str or "streamGenerateContent" in url_str :
50+ is_streaming = "streamGenerateContent" in url_str
51+ # Build headers with streaming flag and correct host
52+ headers = dict (request .headers )
53+ if is_streaming :
54+ headers ["X-UiPath-Streaming-Enabled" ] = "true"
55+ # Update host header to match the gateway URL
56+ gateway_url_parsed = httpx .URL (gateway_url )
57+ headers ["host" ] = gateway_url_parsed .host
58+ # Create new request with rewritten URL
59+ return httpx .Request (
60+ method = request .method ,
61+ url = gateway_url ,
62+ headers = headers ,
63+ content = request .content ,
64+ extensions = request .extensions ,
65+ )
66+ return request
67+
68+
4469class UiPathHttpxClient (httpx .Client ):
4570 """Custom httpx client that redirects generateContent requests to UiPath gateway."""
4671
@@ -57,27 +82,30 @@ def request(self, method: str, url: Any, **kwargs) -> httpx.Response:
5782
5883 def send (self , request : httpx .Request , ** kwargs ) -> httpx .Response :
5984 """Override send to redirect generateContent/streamGenerateContent to UiPath gateway."""
60- url_str = str (request .url )
61- if "generateContent" in url_str or "streamGenerateContent" in url_str :
62- is_streaming = "streamGenerateContent" in url_str
63- # Build headers with streaming flag and correct host
64- headers = dict (request .headers )
65- if is_streaming :
66- headers ["X-UiPath-Streaming-Enabled" ] = "true"
67- # Update host header to match the gateway URL
68- gateway_url_parsed = httpx .URL (self .gateway_url )
69- headers ["host" ] = gateway_url_parsed .host
70- # Create new request with rewritten URL
71- request = httpx .Request (
72- method = request .method ,
73- url = self .gateway_url ,
74- headers = headers ,
75- content = request .content ,
76- extensions = request .extensions ,
77- )
85+ request = _rewrite_request_for_gateway (request , self .gateway_url )
7886 return super ().send (request , ** kwargs )
7987
8088
89+ class UiPathHttpxAsyncClient (httpx .AsyncClient ):
90+ """Custom async httpx client that redirects generateContent requests to UiPath gateway."""
91+
92+ def __init__ (self , gateway_url : str , ** kwargs ):
93+ self .gateway_url = gateway_url
94+ super ().__init__ (** kwargs )
95+
96+ async def request (self , method : str , url : Any , ** kwargs ) -> httpx .Response :
97+ """Override request to redirect generateContent/streamGenerateContent to UiPath gateway."""
98+ url_str = str (url )
99+ if "generateContent" in url_str or "streamGenerateContent" in url_str :
100+ url = self .gateway_url
101+ return await super ().request (method , url , ** kwargs )
102+
103+ async def send (self , request : httpx .Request , ** kwargs ) -> httpx .Response :
104+ """Override send to redirect generateContent/streamGenerateContent to UiPath gateway."""
105+ request = _rewrite_request_for_gateway (request , self .gateway_url )
106+ return await super ().send (request , ** kwargs )
107+
108+
81109class UiPathVertex (GoogleGenAI ):
82110 """
83111 UiPath Vertex AI LLM that routes requests through UiPath's LLM Gateway.
@@ -144,16 +172,22 @@ def __init__(
144172 uipath_url = self ._build_base_url_static (model )
145173 headers = self ._build_headers_static (token )
146174
147- # Create custom httpx client that redirects requests to UiPath gateway
175+ # Create custom httpx clients that redirect requests to UiPath gateway
148176 custom_httpx_client = UiPathHttpxClient (
149177 gateway_url = uipath_url ,
150178 headers = headers ,
151179 follow_redirects = True ,
152180 )
181+ custom_httpx_async_client = UiPathHttpxAsyncClient (
182+ gateway_url = uipath_url ,
183+ headers = headers ,
184+ follow_redirects = True ,
185+ )
153186
154- # Configure HTTP options with our custom client
187+ # Configure HTTP options with our custom clients (sync and async)
155188 http_options = genai_types .HttpOptions (
156189 httpxClient = custom_httpx_client ,
190+ httpxAsyncClient = custom_httpx_async_client ,
157191 )
158192
159193 # Create google.genai client with custom httpx client
0 commit comments