3333 get_uipath_token_header ,
3434)
3535from uipath_langchain ._utils ._sleep_policy import before_sleep_log
36+ from uipath_langchain .chat .http_client import build_uipath_headers , resolve_gateway_url
3637from uipath_langchain .runtime .errors import (
3738 LangGraphErrorCode ,
3839 LangGraphRuntimeError ,
@@ -78,8 +79,6 @@ class UiPathRequestMixin(BaseModel):
7879
7980 default_headers : Mapping [str , str ] | None = {
8081 "X-UiPath-Streaming-Enabled" : "false" ,
81- "X-UiPath-JobKey" : os .getenv ("UIPATH_JOB_KEY" , "" ),
82- "X-UiPath-ProcessKey" : os .getenv ("UIPATH_PROCESS_KEY" , "" ),
8382 }
8483 model_name : str | None = Field (
8584 default_factory = lambda : os .getenv (
@@ -154,6 +153,7 @@ class UiPathRequestMixin(BaseModel):
154153 max_delay : float = 60.0
155154
156155 _url : str | None = None
156+ _is_override : bool = False
157157 _auth_headers : dict [str , str ] | None = None
158158
159159 # required to instantiate AzureChatOpenAI subclasses
@@ -731,17 +731,26 @@ def _prepare_url(self, url: str) -> httpx.URL:
731731 def _build_headers (self , options , retries_taken : int = 0 ) -> httpx .Headers :
732732 return httpx .Headers (self .auth_headers )
733733
734+ def _resolve_url_and_override (self ) -> None :
735+ """Resolve ``_url`` and ``_is_override`` idempotently."""
736+ if self ._url :
737+ return
738+ try :
739+ self ._url , self ._is_override = resolve_gateway_url (self .endpoint )
740+ except ValueError :
741+ self ._url = (
742+ f"{ self .base_url } /{ self .org_id } /{ self .tenant_id } /{ self .endpoint } "
743+ )
744+ except NotImplementedError :
745+ pass
746+
734747 @property
735748 def url (self ) -> str :
749+ self ._resolve_url_and_override ()
736750 if not self ._url :
737- env_uipath_url = os .getenv ("UIPATH_URL" )
738-
739- if env_uipath_url :
740- self ._url = f"{ env_uipath_url .rstrip ('/' )} /{ self .endpoint } "
741- else :
742- self ._url = (
743- f"{ self .base_url } /{ self .org_id } /{ self .tenant_id } /{ self .endpoint } "
744- )
751+ raise NotImplementedError (
752+ "The endpoint property is not implemented for this class."
753+ )
745754 return self ._url
746755
747756 @property
@@ -753,17 +762,21 @@ def endpoint(self) -> str:
753762 @property
754763 def auth_headers (self ) -> dict [str , str ]:
755764 if not self ._auth_headers :
765+ self ._resolve_url_and_override ()
756766 self ._auth_headers = {
757767 ** self .default_headers , # type: ignore
758- "Authorization" : f"Bearer { self .access_token } " ,
759- "X-UiPath-LlmGateway-TimeoutSeconds" : str (self .default_request_timeout ),
760768 }
761- if self .agenthub_config :
762- self ._auth_headers ["X-UiPath-AgentHub-Config" ] = self .agenthub_config
763- if self .byo_connection_id :
764- self ._auth_headers ["X-UiPath-LlmGateway-ByoIsConnectionId" ] = (
765- self .byo_connection_id
769+ self ._auth_headers ["Authorization" ] = f"Bearer { self .access_token } "
770+ self ._auth_headers .update (
771+ build_uipath_headers (
772+ agenthub_config = self .agenthub_config ,
773+ byo_connection_id = self .byo_connection_id ,
774+ inject_routing = self ._is_override ,
766775 )
776+ )
777+ self ._auth_headers ["X-UiPath-LlmGateway-TimeoutSeconds" ] = str (
778+ self .default_request_timeout
779+ )
767780 if self .is_normalized and self .model_name :
768781 self ._auth_headers ["X-UiPath-LlmGateway-NormalizedApi-ModelName" ] = (
769782 self .model_name
0 commit comments