-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpx_client.py
More file actions
319 lines (272 loc) · 12.5 KB
/
Copy pathhttpx_client.py
File metadata and controls
319 lines (272 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""UiPath-configured HTTPX clients with retry, logging, streaming support and others.
This module provides customized httpx Client and AsyncClient subclasses that include:
- Default UiPath LLM Gateway headers (timeout, full 4xx response)
- Automatic retry logic with configurable backoff
- Request/response logging with timing information
- Streaming header injection (X-UiPath-Streaming-Enabled)
- Optional URL freezing to prevent vendor SDK URL mutations
Example:
>>> from uipath_llm_client.httpx_client import UiPathHttpxClient
>>> from uipath_llm_client.settings import UiPathAPIConfig
>>>
>>> client = UiPathHttpxClient(
... base_url="https://cloud.uipath.com/org/tenant/llmgateway_/",
... model_name="gpt-4o",
... api_config=UiPathAPIConfig(...),
... max_retries=3,
... )
>>> response = client.post("/chat/completions", json={"messages": [...]})
"""
import logging
from collections.abc import Callable, Mapping
from typing import Any
from httpx import (
URL,
AsyncBaseTransport,
AsyncClient,
BaseTransport,
Client,
Headers,
Request,
Response,
)
from httpx._types import HeaderTypes
from uipath_llm_client.settings import (
UiPathAPIConfig,
)
from uipath_llm_client.utils.exceptions import patch_raise_for_status
from uipath_llm_client.utils.logging import LoggingConfig
from uipath_llm_client.utils.retry import (
RetryableAsyncHTTPTransport,
RetryableHTTPTransport,
RetryConfig,
)
from uipath_llm_client.utils.ssl_config import get_httpx_client_kwargs
def build_routing_headers(
*,
model_name: str | None = None,
byo_connection_id: str | None = None,
api_config: UiPathAPIConfig | None = None,
) -> Mapping[str, str]:
"""Build UiPath LLM Gateway routing headers based on configuration.
Args:
api_config: UiPath API configuration.
model_name: LLM model name (required for normalized API).
byo_connection_id: Bring Your Own connection ID.
Returns:
Headers mapping for routing requests through the gateway.
"""
headers: dict[str, str] = {}
if api_config is not None:
if api_config.client_type == "normalized" and model_name is not None:
headers["X-UiPath-LlmGateway-NormalizedApi-ModelName"] = model_name
elif api_config.client_type == "passthrough" and api_config.api_type == "completions":
if api_config.api_flavor is not None:
headers["X-UiPath-LlmGateway-ApiFlavor"] = api_config.api_flavor
if api_config.api_version is not None:
headers["X-UiPath-LlmGateway-ApiVersion"] = api_config.api_version
if byo_connection_id is not None:
headers["X-UiPath-LlmGateway-ByoConnectionId"] = byo_connection_id
return headers
class UiPathHttpxClient(Client):
"""Synchronous HTTP client configured for UiPath LLM services.
Extends httpx.Client with:
- Default UiPath headers (server timeout, full 4xx responses)
- Automatic retry on transient failures (429, 5xx)
- Request/response duration logging
- Streaming header injection (X-UiPath-Streaming-Enabled)
- Optional URL freezing to prevent vendor SDK mutations
Headers are merged in order: default headers -> api_config headers -> user headers.
Later headers override earlier ones with the same key.
Attributes:
model_name: The LLM model name (for logging purposes).
api_config: UiPath API configuration settings.
"""
_streaming_header: str = "X-UiPath-Streaming-Enabled"
_default_headers: Mapping[str, str] = {
"X-UiPath-LLMGateway-TimeoutSeconds": "30", # server side timeout, default is 10, maximum is 300
"X-UiPath-LLMGateway-AllowFull4xxResponse": "true", # allow full 4xx responses (default is false)
}
def __init__(
self,
*,
model_name: str | None = None,
byo_connection_id: str | None = None,
api_config: UiPathAPIConfig | None = None,
max_retries: int | None = None,
retry_config: RetryConfig | None = None,
logger: logging.Logger | None = None,
**kwargs: Any,
):
"""Initialize the UiPath HTTP client.
Args:
model_name: LLM model name for logging context.
byo_connection_id: Bring Your Own connection ID for custom model deployments.
api_config: UiPath API configuration (api_type, vendor_type, etc.).
Provides additional headers via build_headers() and controls URL
freezing via freeze_base_url attribute.
max_retries: Maximum retry attempts for failed requests. Defaults to 1.
retry_config: Custom retry configuration (backoff, retryable status codes).
logger: Logger instance for request/response logging.
**kwargs: Additional arguments passed to httpx.Client (e.g., base_url,
timeout, auth, headers, transport, event_hooks).
"""
self.model_name = model_name
self.byo_connection_id = byo_connection_id
self.api_config = api_config
# Extract httpx.Client params that we need to modify
headers: HeaderTypes | None = kwargs.pop("headers", None)
transport: BaseTransport | None = kwargs.pop("transport", None)
event_hooks: Mapping[str, list[Callable[..., Any]]] | None = kwargs.pop("event_hooks", None)
# Merge headers: default -> api_config -> user provided
merged_headers = Headers(self._default_headers)
merged_headers.update(
build_routing_headers(
api_config=api_config, model_name=model_name, byo_connection_id=byo_connection_id
)
)
if headers is not None:
merged_headers.update(headers)
self._freeze_base_url = self.api_config is not None and self.api_config.freeze_base_url
# Setup retry transport if not provided
if transport is None:
transport = RetryableHTTPTransport(
max_retries=max_retries or 1,
retry_config=retry_config,
logger=logger,
)
# Setup logging hooks
logging_config = LoggingConfig(
logger=logger,
model_name=model_name,
api_config=api_config,
)
if event_hooks is None:
event_hooks = {
"request": [],
"response": [],
}
event_hooks["request"].append(logging_config.log_request_duration)
event_hooks["response"].append(logging_config.log_response_duration)
event_hooks["response"].append(logging_config.log_error)
# setup ssl context
kwargs.update(get_httpx_client_kwargs())
super().__init__(
headers=merged_headers, transport=transport, event_hooks=event_hooks, **kwargs
)
def send(self, request: Request, *, stream: bool = False, **kwargs: Any) -> Response:
"""Send an HTTP request with UiPath-specific modifications.
Injects the streaming header and optionally freezes the URL before sending.
Args:
request: The HTTP request to send.
stream: Whether to stream the response body.
**kwargs: Additional arguments passed to the parent send method.
Returns:
Response with patched raise_for_status() that raises UiPath exceptions.
"""
if self._freeze_base_url:
request.url = URL(str(self.base_url).rstrip("/"))
request.headers[self._streaming_header] = str(stream).lower()
response = super().send(request, stream=stream, **kwargs)
return patch_raise_for_status(response)
class UiPathHttpxAsyncClient(AsyncClient):
"""Asynchronous HTTP client configured for UiPath LLM services.
Extends httpx.AsyncClient with:
- Default UiPath headers (server timeout, full 4xx responses)
- Automatic retry on transient failures (429, 5xx)
- Request/response duration logging
- Streaming header injection (X-UiPath-Streaming-Enabled)
- Optional URL freezing to prevent vendor SDK mutations
Headers are merged in order: default headers -> api_config headers -> user headers.
Later headers override earlier ones with the same key.
Attributes:
model_name: The LLM model name (for logging purposes).
api_config: UiPath API configuration settings.
"""
_streaming_header: str = "X-UiPath-Streaming-Enabled"
_default_headers: Mapping[str, str] = {
"X-UiPath-LLMGateway-TimeoutSeconds": "30", # server side timeout, default is 10, maximum is 300
"X-UiPath-LLMGateway-AllowFull4xxResponse": "true", # allow full 4xx responses (default is false)
}
def __init__(
self,
*,
model_name: str | None = None,
byo_connection_id: str | None = None,
api_config: UiPathAPIConfig | None = None,
max_retries: int | None = None,
retry_config: RetryConfig | None = None,
logger: logging.Logger | None = None,
**kwargs: Any,
):
"""Initialize the UiPath async HTTP client.
Args:
model_name: LLM model name for logging context.
byo_connection_id: Bring Your Own connection ID for custom model deployments.
api_config: UiPath API configuration (api_type, vendor_type, etc.).
Provides additional headers via build_headers() and controls URL
freezing via freeze_base_url attribute.
max_retries: Maximum retry attempts for failed requests. Defaults to 1.
retry_config: Custom retry configuration (backoff, retryable status codes).
logger: Logger instance for request/response logging.
**kwargs: Additional arguments passed to httpx.AsyncClient (e.g., base_url,
timeout, auth, headers, transport, event_hooks).
"""
self.model_name = model_name
self.byo_connection_id = byo_connection_id
self.api_config = api_config
# Extract httpx.AsyncClient params that we need to modify
headers: HeaderTypes | None = kwargs.pop("headers", None)
transport: AsyncBaseTransport | None = kwargs.pop("transport", None)
event_hooks: Mapping[str, list[Callable[..., Any]]] | None = kwargs.pop("event_hooks", None)
# Merge headers: default -> api_config -> user provided
merged_headers = Headers(self._default_headers)
merged_headers.update(
build_routing_headers(
api_config=api_config, model_name=model_name, byo_connection_id=byo_connection_id
)
)
if headers is not None:
merged_headers.update(headers)
self._freeze_base_url = self.api_config is not None and self.api_config.freeze_base_url
# Setup retry transport if not provided
if transport is None:
transport = RetryableAsyncHTTPTransport(
max_retries=max_retries or 1,
retry_config=retry_config,
logger=logger,
)
# Setup logging hooks
logging_config = LoggingConfig(
logger=logger,
model_name=model_name,
api_config=api_config,
)
if event_hooks is None:
event_hooks = {
"request": [],
"response": [],
}
event_hooks["request"].append(logging_config.alog_request_duration)
event_hooks["response"].append(logging_config.alog_response_duration)
event_hooks["response"].append(logging_config.alog_error)
# setup ssl context
kwargs.update(get_httpx_client_kwargs())
super().__init__(
headers=merged_headers, transport=transport, event_hooks=event_hooks, **kwargs
)
async def send(self, request: Request, *, stream: bool = False, **kwargs: Any) -> Response:
"""Send an HTTP request asynchronously with UiPath-specific modifications.
Injects the streaming header and optionally freezes the URL before sending.
Args:
request: The HTTP request to send.
stream: Whether to stream the response body.
**kwargs: Additional arguments passed to the parent send method.
Returns:
Response with patched raise_for_status() that raises UiPath exceptions.
"""
if self._freeze_base_url:
request.url = URL(str(self.base_url).rstrip("/"))
request.headers[self._streaming_header] = str(stream).lower()
response = await super().send(request, stream=stream, **kwargs)
return patch_raise_for_status(response)