LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
fix: Error handling improved in tool sandbox and other minor fixes (#1276)
Chandrasekharan M6c33d83633eb
diff --git a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.pyindex 218e38452..e04202e8a 100644--- a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py@@ -182,7 +182,7 @@ def prompt_processor() -> Any:RunLevel.RUN,"Unable to obtain LLM / embedding / vectorDB",)- return APIError(message=msg)+ raise APIError(message=msg)if output[PSKeys.TYPE] == PSKeys.LINE_ITEM:try:diff --git a/unstract/core/src/unstract/core/flask/logging.py b/unstract/core/src/unstract/core/flask/logging.pyindex 20c5437a8..950ad54d6 100644--- a/unstract/core/src/unstract/core/flask/logging.py+++ b/unstract/core/src/unstract/core/flask/logging.py@@ -13,11 +13,11 @@ class RequestIDFilter(logging.Filter):return True-def setup_logging(log_level: str):+def setup_logging(log_level: int):"""Sets up logger for Flask based servicesArgs:- log_level (str): Log level to use. Can be one of- INFO, DEBUG, WARNING, ERROR+ log_level (int): Log level to use. Can be one of+ logging.INFO, logging.DEBUG, logging.WARNING, logging.ERROR"""# Determine if OpenTelemetry trace context should be included in logsotel_trace_context = (diff --git a/unstract/tool-sandbox/src/unstract/tool_sandbox/helper.py b/unstract/tool-sandbox/src/unstract/tool_sandbox/helper.pyindex ee39f3ca1..7d1f4bc96 100644--- a/unstract/tool-sandbox/src/unstract/tool_sandbox/helper.py+++ b/unstract/tool-sandbox/src/unstract/tool_sandbox/helper.py@@ -4,6 +4,8 @@ import osfrom typing import Anyimport requests+from requests import Response+from requests.exceptions import ConnectionError, RequestExceptionfrom unstract.core.utilities import UnstractUtilsfrom unstract.tool_sandbox.constants import UnstractRunner@@ -11,6 +13,10 @@ from unstract.tool_sandbox.constants import UnstractRunnerlogger = logging.getLogger(__name__)+class ToolSanboxError(Exception):+ pass++class ToolSandboxHelper:def __init__(self,@@ -96,20 +102,26 @@ class ToolSandboxHelper:file_execution_id, image_name, image_tag, settings, retry_count)- response = requests.post(url, headers=headers, json=data)- result: dict[str, Any] | None = None- if response.status_code == 200:- result = response.json()- elif response.status_code == 404:- logger.error(- f"Error while calling tool {image_name}: "- f"for tool instance status code {response.status_code}"- )- else:- logger.error(- f"Error while calling tool {image_name} reason: {response.reason}"- )- return result+ response: Response = Response()+ try:+ response = requests.post(url, headers=headers, json=data)+ response.raise_for_status()+ except ConnectionError as connect_err:+ msg = "Unable to connect to unstract-runner."+ logger.error(f"{msg}\n{connect_err}")+ raise ToolSanboxError(msg) from connect_err+ except RequestException as e:+ error_message = str(e)+ content_type = response.headers.get("Content-Type", "").lower()+ if "application/json" in content_type:+ response_json = response.json()+ if "error" in response_json:+ error_message = response_json["error"]+ elif response.text:+ error_message = response.text+ logger.error(f"Error from runner: {error_message}")+ raise ToolSanboxError(error_message) from e+ return response.json()def create_tool_request_data(self,diff --git a/unstract/workflow-execution/src/unstract/workflow_execution/tools_utils.py b/unstract/workflow-execution/src/unstract/workflow_execution/tools_utils.pyindex 51aae4dcd..feffce305 100644--- a/unstract/workflow-execution/src/unstract/workflow_execution/tools_utils.py+++ b/unstract/workflow-execution/src/unstract/workflow_execution/tools_utils.py@@ -177,7 +177,7 @@ class ToolsUtils:self,file_execution_id: str,tool_sandbox: ToolSandbox,- ) -> Any:+ ) -> dict[str, Any] | None:return self.run_tool_with_retry(file_execution_id, tool_sandbox)def run_tool_with_retry(@@ -185,24 +185,25 @@ class ToolsUtils:file_execution_id: str,tool_sandbox: ToolSandbox,max_retries: int = ToolExecution.MAXIMUM_RETRY,- ) -> Any:- error: dict[str, Any] | None = None+ ) -> dict[str, Any] | None:for retry_count in range(max_retries):try:response = tool_sandbox.run_tool(file_execution_id, retry_count)if response:return responselogger.warning(- f"ToolExecutionException - Retrying "- f"({retry_count + 1}/{max_retries})"+ f"ToolExecutionException - Retrying ({retry_count + 1}/{max_retries})")except Exception as e:- logger.warning(- f"Exception - Retrying ({retry_count + 1}/{max_retries}): "- f"{str(e)}"- )-- logger.warning(f"Operation failed after {max_retries} retries, error: {error}")+ if retry_count < max_retries - 1:+ logger.warning(+ f"Exception - Retrying ({retry_count + 1}/{max_retries}): {str(e)}"+ )+ else:+ logger.warning(+ f"Operation failed after '{max_retries}' retries, error: {e}"+ )+ raise ereturn Nonedef get_tool_environment_variables(self) -> dict[str, Any]: