Skip to content

Commit ab5b440

Browse files
RunnanJiaclaude
andauthored
fix: inject X-UiPath-License-RefId in transport layer [AE-1109] (#709)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 55aec54 commit ab5b440

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/uipath_langchain/chat/openai.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@
1717

1818
logger = logging.getLogger(__name__)
1919

20+
# Module-level storage for the current license ref ID.
21+
# Set by uipath-agents when a model_run span starts, read by the
22+
# transport to inject X-UiPath-License-RefId on LLM calls.
23+
# A plain global is used because the LangChain callback (which sets the
24+
# value) and the httpx transport (which reads it) run on different
25+
# threads, so neither ContextVar nor threading.local work.
26+
_current_license_ref_id: str | None = None
27+
28+
29+
def set_license_ref_id(value: str | None) -> None:
30+
"""Set the license ref ID for injection on LLM requests."""
31+
global _current_license_ref_id
32+
_current_license_ref_id = value
33+
34+
35+
def _get_license_ref_id() -> str | None:
36+
"""Read the current license ref ID."""
37+
return _current_license_ref_id
38+
2039

2140
def _rewrite_openai_url(
2241
original_url: str, params: httpx.QueryParams
@@ -45,6 +64,13 @@ def _rewrite_openai_url(
4564
return httpx.URL(new_url_str)
4665

4766

67+
def _inject_license_ref_id(request: httpx.Request) -> None:
68+
"""Inject X-UiPath-License-RefId header if a model_run span is active."""
69+
license_ref_id = _get_license_ref_id()
70+
if license_ref_id:
71+
request.headers["X-UiPath-License-RefId"] = license_ref_id
72+
73+
4874
class UiPathURLRewriteTransport(httpx.AsyncHTTPTransport):
4975
def __init__(self, verify: bool = True, **kwargs):
5076
super().__init__(verify=verify, **kwargs)
@@ -53,6 +79,7 @@ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
5379
new_url = _rewrite_openai_url(str(request.url), request.url.params)
5480
if new_url:
5581
request.url = new_url
82+
_inject_license_ref_id(request)
5683

5784
return await super().handle_async_request(request)
5885

@@ -65,6 +92,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
6592
new_url = _rewrite_openai_url(str(request.url), request.url.params)
6693
if new_url:
6794
request.url = new_url
95+
_inject_license_ref_id(request)
6896

6997
return super().handle_request(request)
7098

0 commit comments

Comments
 (0)