💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
fix(workflow): add request timeout to ServiceTask (#1226)
Paco Cartones3a7b597d7886
diff --git a/src/python/txtai/workflow/task/service.py b/src/python/txtai/workflow/task/service.pyindex 12557fdd..73a67a48 100644--- a/src/python/txtai/workflow/task/service.py+++ b/src/python/txtai/workflow/task/service.py@@ -19,7 +19,7 @@ class ServiceTask(Task):Task to runs requests against remote service urls."""- def register(self, url=None, method=None, params=None, batch=True, extract=None):+ def register(self, url=None, method=None, params=None, batch=True, extract=None, timeout=30):"""Adds service parameters to task. Checks if required dependencies are installed.@@ -29,6 +29,7 @@ class ServiceTask(Task):params: default query parametersbatch: if True, all elements are passed in a single batch request, otherwise a service call is executed per elementextract: list of sections to extract from response+ timeout: request timeout in seconds, defaults to 30"""if not XML_TO_DICT:@@ -43,6 +44,9 @@ class ServiceTask(Task):# If True, all elements are passed in a single batch request, otherwise a service call is executed per elementself.batch = batch+ # Request timeout in seconds. Prevents a slow or unresponsive endpoint from blocking the workflow indefinitely.+ self.timeout = timeout+# Save sections to extract. Supports both a single string and a hierarchical list of sections.self.extract = extractif self.extract:@@ -83,9 +87,9 @@ class ServiceTask(Task):# Run requestif self.method and self.method.lower() == "get":- response = requests.get(self.url, params=params)+ response = requests.get(self.url, params=params, timeout=self.timeout)else:- response = requests.post(self.url, json=params)+ response = requests.post(self.url, json=params, timeout=self.timeout)# Parse data based on content-typemimetype = response.headers["Content-Type"].split(";")[0]diff --git a/test/python/testapi/testapiworkflow.py b/test/python/testapi/testapiworkflow.pyindex 875e9883..f731fff3 100644--- a/test/python/testapi/testapiworkflow.py+++ b/test/python/testapi/testapiworkflow.py@@ -5,6 +5,7 @@ Workflow API module testsimport jsonimport osimport tempfile+import timeimport unittestfrom http.server import HTTPServer, BaseHTTPRequestHandler@@ -12,6 +13,8 @@ from multiprocessing.pool import ThreadPoolfrom threading import Threadfrom unittest.mock import patch+import requests+from fastapi.testclient import TestClientfrom txtai.api import API, application@@ -91,6 +94,15 @@ workflow:extract: rowparams:text:++ slow:+ tasks:+ - task: service+ url: http://127.0.0.1:8001/slow+ method: get+ timeout: 1+ params:+ text:"""@@ -104,6 +116,10 @@ class RequestHandler(BaseHTTPRequestHandler):GET request handler."""+ if self.path.startswith("/slow"):+ # Sleep longer than the client timeout to force a read timeout+ time.sleep(2)+self.send_response(200)if self.path.startswith("/xml"):@@ -230,6 +246,15 @@ class TestWorkflow(unittest.TestCase):self.assertEqual(len(results), 1)self.assertEqual(len(results[0]), 1)+ def testServiceTimeout(self):+ """+ Test workflow with ServiceTask GET that bounds a slow endpoint via a timeout+ """++ text = "This is a test sentence. And another sentence to split."+ with self.assertRaises(requests.exceptions.Timeout):+ self.client.post("workflow", json={"name": "slow", "elements": [text]})+def testWorkflowLabels(self):"""Test workflow with labels via API