Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

fix: Tool validation error enhancement, WF execution fix (#415)

Chandrasekharan Me8f8ff92e1f9
diff --git a/backend/sample.env b/backend/sample.env
index c5901856b..7c33854e2 100644
--- a/backend/sample.env
+++ b/backend/sample.env
@@ -89,9 +89,9 @@ PROMPT_PORT=3003
PROMPT_STUDIO_FILE_PATH=/app/prompt-studio-data
# Structure Tool
-STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.25"
+STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.26"
STRUCTURE_TOOL_IMAGE_NAME="unstract/tool-structure"
-STRUCTURE_TOOL_IMAGE_TAG="0.0.25"
+STRUCTURE_TOOL_IMAGE_TAG="0.0.26"
# Feature Flags
EVALUATION_SERVER_IP=localhost
diff --git a/backend/tool_instance/exceptions.py b/backend/tool_instance/exceptions.py
index 75c6d652e..69c2c26a5 100644
--- a/backend/tool_instance/exceptions.py
+++ b/backend/tool_instance/exceptions.py
@@ -38,4 +38,9 @@ class ToolInstantiationError(ToolInstanceBaseException):
class BadRequestException(ToolInstanceBaseException):
status_code = 400
- default_detail = "Invalid input"
+ default_detail = "Invalid input."
+
+
+class ToolSettingValidationError(APIException):
+ status_code = 400
+ default_detail = "Error while validating tool's setting."
diff --git a/backend/tool_instance/tool_instance_helper.py b/backend/tool_instance/tool_instance_helper.py
index 85c729c79..16000f7c2 100644
--- a/backend/tool_instance/tool_instance_helper.py
+++ b/backend/tool_instance/tool_instance_helper.py
@@ -1,7 +1,6 @@
import logging
import os
import uuid
-from json import JSONDecodeError
from typing import Any, Optional
from account.models import User
@@ -10,10 +9,10 @@ from adapter_processor.models import AdapterInstance
from connector.connector_instance_helper import ConnectorInstanceHelper
from django.core.exceptions import PermissionDenied
from django.core.exceptions import ValidationError as DjangoValidationError
-from jsonschema.exceptions import UnknownType
from jsonschema.exceptions import ValidationError as JSONValidationError
from prompt_studio.prompt_studio_registry.models import PromptStudioRegistry
from tool_instance.constants import JsonSchemaKey
+from tool_instance.exceptions import ToolSettingValidationError
from tool_instance.models import ToolInstance
from tool_instance.tool_processor import ToolProcessor
from unstract.adapters.enums import AdapterTypes
@@ -333,7 +332,7 @@ class ToolInstanceHelper:
@staticmethod
def validate_tool_settings(
user: User, tool_uid: str, tool_meta: dict[str, Any]
- ) -> tuple[bool, str]:
+ ) -> bool:
"""Function to validate Tools settings."""
# check if exported tool is valid for the user who created workflow
@@ -351,14 +350,22 @@ class ToolInstanceHelper:
)
try:
DefaultsGeneratingValidator(schema_json).validate(tool_meta)
- return True, ""
- except JSONDecodeError as e:
- return False, str(e)
except JSONValidationError as e:
- logger.error(e)
- return False, str(tool_name + ": " + e.schema["description"])
- except UnknownType as e:
- return False, str(e)
+ logger.error(e, stack_info=True, exc_info=True)
+ err_msg = e.message
+ # TODO: Support other JSON validation errors or consider following
+ # https://github.com/networknt/json-schema-validator/blob/master/doc/cust-msg.md
+ if e.validator == "required":
+ for validator_val in e.validator_value:
+ required_prop = e.schema.get("properties").get(validator_val)
+ required_display_name = required_prop.get("title")
+ err_msg = err_msg.replace(validator_val, required_display_name)
+ else:
+ logger.warning(f"Unformatted exception sent to user: {err_msg}")
+ raise ToolSettingValidationError(
+ f"Error validating tool settings for '{tool_name}': {err_msg}"
+ )
+ return True
@staticmethod
def validate_adapter_permissions(
diff --git a/backend/workflow_manager/workflow/exceptions.py b/backend/workflow_manager/workflow/exceptions.py
index fb6d5a1d9..39087ab8e 100644
--- a/backend/workflow_manager/workflow/exceptions.py
+++ b/backend/workflow_manager/workflow/exceptions.py
@@ -54,8 +54,3 @@ class InternalException(APIException):
class WorkflowExecutionNotExist(APIException):
status_code = 404
default_detail = "Workflow execution does not exist"
-
-
-class ToolValidationError(APIException):
- status_code = 400
- default_detail = "Tool validation error"
diff --git a/backend/workflow_manager/workflow/execution.py b/backend/workflow_manager/workflow/execution.py
index 9160deccb..b249e53dc 100644
--- a/backend/workflow_manager/workflow/execution.py
+++ b/backend/workflow_manager/workflow/execution.py
@@ -198,38 +198,40 @@ class WorkflowExecutionServiceHelper(WorkflowExecutionService):
if single_step:
execution_type = ExecutionType.STEP
- if self.compilation_result["success"] is True:
- if (
- self.execution_mode == WorkflowExecution.Mode.INSTANT
- or self.execution_mode == WorkflowExecution.Mode.QUEUE
- ):
- start_time = time.time()
- try:
- self.execute_workflow(execution_type=execution_type)
- end_time = time.time()
- execution_time = end_time - start_time
- except StopExecution as exception:
- end_time = time.time()
- execution_time = end_time - start_time
- logger.info(f"Execution {self.execution_id} stopped")
- raise exception
- except Exception as exception:
- end_time = time.time()
- execution_time = end_time - start_time
- message = str(exception)[:EXECUTION_ERROR_LENGTH]
- logger.info(
- f"Execution {self.execution_id} in {execution_time}s, "
- f" Error {exception}"
- )
- raise WorkflowExecutionError(message) from exception
- else:
- error_message = f"Unknown Execution Method {self.execution_mode}"
- raise WorkflowExecutionError(error_message)
+ if self.compilation_result["success"] is False:
+ error_message = (
+ f"Errors while compiling workflow "
+ f"{self.compilation_result['problems'][0]}"
+ )
+ raise WorkflowExecutionError(error_message)
- else:
- error_message = f"Errors while compiling workflow {self.compilation_result['problems'][0]}" # noqa
+ if self.execution_mode not in (
+ WorkflowExecution.Mode.INSTANT,
+ WorkflowExecution.Mode.QUEUE,
+ ):
+ error_message = f"Unknown Execution Method {self.execution_mode}"
raise WorkflowExecutionError(error_message)
+ start_time = time.time()
+ try:
+ self.execute_workflow(execution_type=execution_type)
+ end_time = time.time()
+ execution_time = end_time - start_time
+ except StopExecution as exception:
+ end_time = time.time()
+ execution_time = end_time - start_time
+ logger.info(f"Execution {self.execution_id} stopped")
+ raise exception
+ except Exception as exception:
+ end_time = time.time()
+ execution_time = end_time - start_time
+ message = str(exception)[:EXECUTION_ERROR_LENGTH]
+ logger.info(
+ f"Execution {self.execution_id} in {execution_time}s, "
+ f" Error {exception}"
+ )
+ raise WorkflowExecutionError(message) from exception
+
def publish_initial_workflow_logs(self, total_files: int) -> None:
"""Publishes the initial logs for the workflow.
diff --git a/backend/workflow_manager/workflow/workflow_helper.py b/backend/workflow_manager/workflow/workflow_helper.py
index 0849b07ef..8b94d8edd 100644
--- a/backend/workflow_manager/workflow/workflow_helper.py
+++ b/backend/workflow_manager/workflow/workflow_helper.py
@@ -37,7 +37,6 @@ from workflow_manager.workflow.enums import ExecutionStatus, SchemaEntity, Schem
from workflow_manager.workflow.exceptions import (
InvalidRequest,
TaskDoesNotExistError,
- ToolValidationError,
WorkflowDoesNotExistError,
WorkflowExecutionNotExist,
)
@@ -217,13 +216,11 @@ class WorkflowHelper:
tool_instances: list[ToolInstance],
) -> None:
for tool in tool_instances:
- valid, message = ToolInstanceHelper.validate_tool_settings(
+ ToolInstanceHelper.validate_tool_settings(
user=tool.workflow.created_by,
tool_uid=tool.tool_id,
tool_meta=tool.metadata,
)
- if not valid:
- raise ToolValidationError(message)
@staticmethod
def run_workflow(
diff --git a/platform-service/src/unstract/platform_service/main.py b/platform-service/src/unstract/platform_service/main.py
index 0ec994776..a1b0884e8 100644
--- a/platform-service/src/unstract/platform_service/main.py
+++ b/platform-service/src/unstract/platform_service/main.py
@@ -131,7 +131,9 @@ def validate_bearer_token(token: Optional[str]) -> bool:
return False
except Exception as e:
- app.logger.error(f"Error while validating bearer token: {e}")
+ app.logger.error(
+ f"Error while validating bearer token: {e}", stack_info=True, exc_info=True
+ )
return False
return True
diff --git a/prompt-service/src/unstract/prompt_service/authentication_middleware.py b/prompt-service/src/unstract/prompt_service/authentication_middleware.py
index 0ad5c6aaa..b9f237971 100644
--- a/prompt-service/src/unstract/prompt_service/authentication_middleware.py
+++ b/prompt-service/src/unstract/prompt_service/authentication_middleware.py
@@ -38,7 +38,11 @@ class AuthenticationMiddleware:
return False
except Exception as e:
- current_app.logger.error(f"Error while validating bearer token: {e}")
+ current_app.logger.error(
+ f"Error while validating bearer token: {e}",
+ stack_info=True,
+ exc_info=True,
+ )
return False
return True
diff --git a/x2text-service/app/authentication_middleware.py b/x2text-service/app/authentication_middleware.py
index 0f03ce0d1..7b9f36e51 100644
--- a/x2text-service/app/authentication_middleware.py
+++ b/x2text-service/app/authentication_middleware.py
@@ -49,7 +49,11 @@ class AuthenticationMiddleware:
return False
except Exception as e:
- current_app.logger.error(f"Error while validating bearer token: {e}")
+ current_app.logger.error(
+ f"Error while validating bearer token: {e}",
+ stack_info=True,
+ exc_info=True,
+ )
return False
return True