LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-1819 [FIX] - Surface status codes from third-party clients in services to UI (#1702)
Praveen Kumar3adef8fe7d22
diff --git a/backend/backend/exceptions.py b/backend/backend/exceptions.pyindex 8159771c1..caf4df575 100644--- a/backend/backend/exceptions.py+++ b/backend/backend/exceptions.py@@ -4,11 +4,12 @@ from rest_framework.exceptions import APIExceptionfrom rest_framework.response import Responsefrom rest_framework.views import exception_handler-from unstract.connectors.exceptions import ConnectorBaseException+from unstract.connectors.exceptions import ConnectorBaseException, ConnectorErrorclass UnstractBaseException(APIException):default_detail = "Error occurred"+ default_status_code = 500def __init__(self,@@ -28,6 +29,12 @@ class UnstractBaseException(APIException):super().__init__(detail=detail, **kwargs)self._core_err = core_err+ # Preserve status code from ConnectorError if available+ if isinstance(core_err, ConnectorError) and core_err.status_code is not None:+ self.status_code = core_err.status_code+ else:+ self.status_code = self.default_status_code+class LLMHelperError(Exception):pass@@ -51,4 +58,4 @@ class UnstractFSException(UnstractBaseException):"""default_detail = "Error testing connection. "- status_code = 500+ default_status_code = 500diff --git a/backend/connector_processor/exceptions.py b/backend/connector_processor/exceptions.pyindex 36508038c..2deb36469 100644--- a/backend/connector_processor/exceptions.py+++ b/backend/connector_processor/exceptions.py@@ -28,4 +28,6 @@ class TestConnectorInputError(UnstractBaseException):def __init__(self, core_err: ConnectorError) -> None:super().__init__(detail=core_err.message, core_err=core_err)self.default_detail = core_err.message- self.status_code = 400+ # Preserve status code from connector error if available, otherwise default to 400+ if not core_err.status_code:+ self.status_code = 400diff --git a/backend/workflow_manager/endpoint_v2/database_utils.py b/backend/workflow_manager/endpoint_v2/database_utils.pyindex 7f0aeb7ab..a89594e18 100644--- a/backend/workflow_manager/endpoint_v2/database_utils.py+++ b/backend/workflow_manager/endpoint_v2/database_utils.py@@ -59,7 +59,7 @@ class DatabaseUtils:try:return conn_cls.get_information_schema(table_name=table_name)except ConnectorError as e:- raise UnstractDBException(detail=e.message) from e+ raise UnstractDBException(detail=e.message, status_code=e.status_code) from e@staticmethoddef get_sql_values_for_query(diff --git a/backend/workflow_manager/endpoint_v2/exceptions.py b/backend/workflow_manager/endpoint_v2/exceptions.pyindex dc2ac95c3..b8664e458 100644--- a/backend/workflow_manager/endpoint_v2/exceptions.py+++ b/backend/workflow_manager/endpoint_v2/exceptions.py@@ -96,17 +96,21 @@ class BigQueryTableNotFound(APIException):class UnstractDBException(APIException):default_detail = "Error creating/inserting to database. "- def __init__(self, detail: str = default_detail) -> None:- status_code = 500- super().__init__(detail=detail, code=status_code)+ def __init__(+ self, detail: str = default_detail, status_code: int | None = None+ ) -> None:+ self.status_code = status_code or 500+ super().__init__(detail=detail, code=self.status_code)class UnstractQueueException(APIException):default_detail = "Error creating/inserting to Queue. "- def __init__(self, detail: str = default_detail) -> None:- status_code = 500- super().__init__(detail=detail, code=status_code)+ def __init__(+ self, detail: str = default_detail, status_code: int | None = None+ ) -> None:+ self.status_code = status_code or 500+ super().__init__(detail=detail, code=self.status_code)class SourceFileOrInfilePathNotFound(APIException):diff --git a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.pyindex 1ff223eeb..da7753080 100644--- a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py@@ -181,7 +181,9 @@ def prompt_processor() -> Any:RunLevel.RUN,"Unable to obtain LLM / embedding / vectorDB",)- raise APIError(message=msg)+ # Preserve the status code from the SDK error+ status_code = getattr(e, "status_code", None) or 500+ raise APIError(message=msg, code=status_code) from eif output[PSKeys.TYPE] == PSKeys.TABLE:adapter_parent_data = ToolAdapter.get_adapter_config(util, output[PSKeys.LLM])diff --git a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py b/prompt-service/src/unstract/prompt_service/services/answer_prompt.pyindex df2c0daed..9f8cbf9c2 100644--- a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py@@ -263,13 +263,13 @@ class AnswerPromptService:word_confidence_data)return answer- # TODO: Catch and handle specific exception hereexcept SdkRateLimitError as e:raise RateLimitError(f"Rate limit error. {str(e)}") from eexcept SdkError as e:logger.error(f"Error fetching response for prompt: {e}.")- # TODO: Publish this error as a FE update- raise APIError(str(e)) from e+ # Preserve the status code from the SDK error for proper HTTP response+ status_code = getattr(e, "status_code", None) or 500+ raise APIError(message=str(e), code=status_code) from e@staticmethoddef extract_table(diff --git a/prompt-service/src/unstract/prompt_service/utils/request.py b/prompt-service/src/unstract/prompt_service/utils/request.pyindex 4c94506e7..8a5a82d46 100644--- a/prompt-service/src/unstract/prompt_service/utils/request.py+++ b/prompt-service/src/unstract/prompt_service/utils/request.py@@ -43,7 +43,14 @@ def make_http_request(return return_valexcept RequestException as e:app.logger.error(f"HTTP request error: {e}")- raise APIError(f"Error occured while invoking POST API Variable : {str(e)}")+ # Extract status code from requests exception if available+ status_code = None+ if getattr(e, "response", None) is not None:+ status_code = getattr(e.response, "status_code", None)+ raise APIError(+ f"HTTP {verb.value} request to {url} failed: {e!s}",+ code=status_code or 500,+ ) from eexcept Exception as e:app.logger.error(f"An unexpected error occurred: {e}")raise ediff --git a/unstract/connectors/src/unstract/connectors/exceptions.py b/unstract/connectors/src/unstract/connectors/exceptions.pyindex d3433a0cc..3b9c683e5 100644--- a/unstract/connectors/src/unstract/connectors/exceptions.py+++ b/unstract/connectors/src/unstract/connectors/exceptions.py@@ -23,11 +23,13 @@ class ConnectorError(ConnectorBaseException):message: str,*args: Any,treat_as_user_message: bool = False,+ status_code: int | None = None,**kwargs: Any,) -> None:user_message = message if treat_as_user_message else Nonesuper().__init__(*args, user_message=user_message, **kwargs)self.message = message+ self.status_code = status_codedef __str__(self) -> str:return f"{self.message}"diff --git a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.pyindex ded2f9ce0..519fe1394 100644--- a/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py+++ b/unstract/connectors/src/unstract/connectors/filesystems/azure_cloud_storage/exceptions.py@@ -26,7 +26,9 @@ def parse_azure_error(e: Exception) -> ConnectorError:f"Authentication failed. Please check your connection credentials. \n"f"Error: \n```\n{client_error}\n```")- return ConnectorError(error_message)+ # ClientAuthenticationError typically indicates 401 Unauthorized+ status_code = getattr(e, "status_code", None) or 401+ return ConnectorError(error_message, status_code=status_code)elif isinstance(e, AzureException.ServiceRequestError):client_error = e.message if hasattr(e, "message") else str(e)error_message += (@@ -39,7 +41,9 @@ def parse_azure_error(e: Exception) -> ConnectorError:f"Azure service returned an error response. \n"f"Error: \n```\n{client_error}\n```")- return AzureHttpError(error_message)+ # Preserve the HTTP status code from Azure's response+ status_code = getattr(e, "status_code", None)+ return AzureHttpError(error_message, status_code=status_code)else:error_message += (f"Error from Azure Cloud Storage. \n" f"Error: \n```\n{str(e)}\n```"