Skip to content

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

Added global error handler for prompt-service (#255)

Deepak K5e4c8d067d13
diff --git a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
index 8c9bccb7e..0f137abec 100644
--- a/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
+++ b/backend/prompt_studio/prompt_studio_core/prompt_studio_helper.py
@@ -782,7 +782,7 @@ class PromptStudioHelper:
answer = responder.single_pass_extraction(payload)
# TODO: Make use of dataclasses
if answer["status"] == "ERROR":
- error_message = answer.get("error", "")
+ error_message = answer.get("error", None)
raise AnswerFetchError(error_message)
output_response = json.loads(answer["structure_output"])
return output_response
diff --git a/prompt-service/src/unstract/prompt_service/main.py b/prompt-service/src/unstract/prompt_service/main.py
index 63bd294a9..c345802da 100644
--- a/prompt-service/src/unstract/prompt_service/main.py
+++ b/prompt-service/src/unstract/prompt_service/main.py
@@ -1,10 +1,9 @@
-import json
import logging
from enum import Enum
from typing import Any, Optional
import peewee
-from flask import Flask, request
+from flask import Flask, json, request
from llama_index import VectorStoreIndex
from llama_index.llms import LLM
from llama_index.vector_stores.types import ExactMatchFilter, MetadataFilters
@@ -23,6 +22,7 @@ from unstract.sdk.utils.service_context import (
ServiceContext as UNServiceContext,
)
from unstract.sdk.vector_db import ToolVectorDB
+from werkzeug.exceptions import HTTPException
from unstract.core.pubsub_helper import LogPublisher
@@ -770,6 +770,21 @@ def enable_single_pass_extraction() -> None:
enable_single_pass_extraction()
[email protected](HTTPException)
+def handle_exception(e):
+ """Return JSON instead of HTML for HTTP errors."""
+ response = e.get_response()
+ response.data = json.dumps(
+ {
+ "code": e.code,
+ "name": e.name,
+ "error": e.description,
+ }
+ )
+ response.content_type = "application/json"
+ return response
+
+
if __name__ == "__main__":
# Start the server
app.run(host="0.0.0.0", port=5003)