Skip to content

Commit 5822a1b

Browse files
fix: added retry mehanism to integration tools
1 parent 04b9237 commit 5822a1b

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/uipath_langchain/agent/tools/integration_tool.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
from langchain.tools import BaseTool
77
from langchain_core.messages import ToolCall
88
from langchain_core.tools import StructuredTool
9+
from tenacity import (
10+
retry,
11+
retry_if_exception,
12+
stop_after_attempt,
13+
wait_exponential_jitter,
14+
)
915
from uipath.agent.models.agent import AgentIntegrationToolResourceConfig
1016
from uipath.eval.mocks import mockable
1117
from uipath.platform import UiPath
1218
from uipath.platform.connections import ActivityMetadata, ActivityParameterLocationInfo
19+
from uipath.platform.errors import EnrichedException
1320
from uipath.runtime.errors import UiPathErrorCategory
14-
1521
from uipath_langchain.agent.exceptions import AgentStartupError, AgentStartupErrorCode
1622
from uipath_langchain.agent.react.jsonschema_pydantic_converter import create_model
1723
from uipath_langchain.agent.react.types import AgentGraphState
@@ -165,6 +171,18 @@ def create_integration_tool(
165171

166172
sdk = UiPath()
167173

174+
def _is_429(exc: BaseException) -> bool:
175+
return (
176+
isinstance(exc, EnrichedException)
177+
and getattr(exc, "status_code", None) == 429
178+
)
179+
180+
@retry(
181+
retry=retry_if_exception(_is_429),
182+
wait=wait_exponential_jitter(initial=10, max=120, jitter=5),
183+
stop=stop_after_attempt(5),
184+
reraise=True,
185+
)
168186
@mockable(
169187
name=resource.name,
170188
description=resource.description,
@@ -173,15 +191,11 @@ def create_integration_tool(
173191
example_calls=resource.properties.example_calls,
174192
)
175193
async def integration_tool_fn(**kwargs: Any):
176-
try:
177-
result = await sdk.connections.invoke_activity_async(
178-
activity_metadata=activity_metadata,
179-
connection_id=connection_id,
180-
activity_input=sanitize_dict_for_serialization(kwargs),
181-
)
182-
except Exception:
183-
raise
184-
194+
result = await sdk.connections.invoke_activity_async(
195+
activity_metadata=activity_metadata,
196+
connection_id=connection_id,
197+
activity_input=sanitize_dict_for_serialization(kwargs),
198+
)
185199
return result
186200

187201
async def integration_tool_wrapper(

0 commit comments

Comments
 (0)