Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(core): give trigger list and payload requests a 60s timeout (#8081)

João Moura6b9e62180ec6
diff --git a/lib/cli/tests/test_plus_api.py b/lib/cli/tests/test_plus_api.py
index 0a3b721da..eb5269152 100644
--- a/lib/cli/tests/test_plus_api.py
+++ b/lib/cli/tests/test_plus_api.py
@@ -1,8 +1,14 @@
+from collections.abc import Callable
import os
import unittest
from unittest.mock import ANY, MagicMock, patch
from crewai_cli.plus_api import PlusAPI
+import httpx
+import pytest
+
+
+_RealHttpxClient = httpx.Client
class TestPlusAPI(unittest.TestCase):
@@ -494,3 +500,53 @@ def test_get_agent_with_org_uuid(mock_settings_class, mock_make_request):
assert "X-Crewai-Organization-Id" in api.headers
assert api.headers["X-Crewai-Organization-Id"] == org_uuid
assert response == mock_response
+
+
+def _requests_sent_by(call: Callable[[PlusAPI], object]) -> list[httpx.Request]:
+ """Run ``call`` against a PlusAPI whose HTTP client answers locally."""
+ sent: list[httpx.Request] = []
+
+ def answer(request: httpx.Request) -> httpx.Response:
+ sent.append(request)
+ return httpx.Response(200, json={})
+
+ transport = httpx.MockTransport(answer)
+ with patch(
+ "crewai_core.plus_api.httpx.Client",
+ side_effect=lambda **kwargs: _RealHttpxClient(transport=transport, **kwargs),
+ ):
+ call(PlusAPI("test_api_key"))
+ return sent
+
+
+ ("call", "path"),
+ [
+ (
+ lambda api: api.get_triggers(),
+ "/crewai_plus/api/v1/integrations/apps",
+ ),
+ (
+ lambda api: api.get_trigger_payload("hubspot", "contact_created"),
+ "/crewai_plus/api/v1/integrations/hubspot/contact_created/payload",
+ ),
+ ],
+ ids=["get_triggers", "get_trigger_payload"],
+)
+def test_trigger_requests_wait_60_seconds_only_for_the_response(call, path):
+ (request,) = _requests_sent_by(call)
+
+ assert request.url.path == path
+ assert request.extensions["timeout"] == {
+ "connect": 5.0,
+ "read": 60.0,
+ "write": 5.0,
+ "pool": 5.0,
+ }
+
+
+def test_requests_without_their_own_timeout_keep_the_httpx_default():
+ (request,) = _requests_sent_by(lambda api: api.get_agent("an_agent"))
+
+ assert request.url.path == "/crewai_plus/api/v1/agents/an_agent"
+ assert request.extensions["timeout"]["read"] == 5.0
diff --git a/lib/crewai-core/src/crewai_core/plus_api.py b/lib/crewai-core/src/crewai_core/plus_api.py
index 28c8968d6..97af76042 100644
--- a/lib/crewai-core/src/crewai_core/plus_api.py
+++ b/lib/crewai-core/src/crewai_core/plus_api.py
@@ -134,11 +134,16 @@ Headers = TypedDict(
)
+# The integrations apps listing takes ~11s on a real organization. Only the read
+# waits longer, so an unreachable or saturated service still fails at httpx's 5s.
+_TRIGGER_TIMEOUT = httpx.Timeout(5.0, read=60.0)
+
+
class RequestKwargs(TypedDict):
headers: dict[str, str]
json: NotRequired[Any]
params: NotRequired[dict[str, str]]
- timeout: NotRequired[float]
+ timeout: NotRequired[float | httpx.Timeout]
class PlusAPI:
@@ -188,7 +193,7 @@ class PlusAPI:
*,
json: Any = None,
params: dict[str, str] | None = None,
- timeout: float | None = None,
+ timeout: float | httpx.Timeout | None = None,
verify: bool = True,
) -> httpx.Response:
url = urljoin(self.base_url, endpoint)
@@ -533,10 +538,14 @@ class PlusAPI:
def get_triggers(self) -> httpx.Response:
"""Get all available triggers from integrations."""
- return self._make_request("GET", f"{self.INTEGRATIONS_RESOURCE}/apps")
+ return self._make_request(
+ "GET", f"{self.INTEGRATIONS_RESOURCE}/apps", timeout=_TRIGGER_TIMEOUT
+ )
def get_trigger_payload(self, app_slug: str, trigger_slug: str) -> httpx.Response:
"""Get sample payload for a specific trigger."""
return self._make_request(
- "GET", f"{self.INTEGRATIONS_RESOURCE}/{app_slug}/{trigger_slug}/payload"
+ "GET",
+ f"{self.INTEGRATIONS_RESOURCE}/{app_slug}/{trigger_slug}/payload",
+ timeout=_TRIGGER_TIMEOUT,
)