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] Replacing variables in API Payload & exception handling (#611)
harini-venkataramane511ae809731
diff --git a/backend/prompt_studio/prompt_studio_core/exceptions.py b/backend/prompt_studio/prompt_studio_core/exceptions.pyindex d8056a756..a3f337b6c 100644--- a/backend/prompt_studio/prompt_studio_core/exceptions.py+++ b/backend/prompt_studio/prompt_studio_core/exceptions.py@@ -1,3 +1,5 @@+from typing import Optional+from prompt_studio.prompt_profile_manager.constants import ProfileManagerKeysfrom prompt_studio.prompt_studio_core.constants import ToolStudioErrorsfrom rest_framework.exceptions import APIException@@ -86,3 +88,10 @@ class PromptNotRun(APIException):"it can be used as a variable in another prompt. ""Please execute the prompt first and try again.")++ def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):+ if detail is not None:+ self.detail = detail+ if code is not None:+ self.code = code+ super().__init__(detail, code)diff --git a/backend/prompt_studio/prompt_studio_core/prompt_variable_service.py b/backend/prompt_studio/prompt_studio_core/prompt_variable_service.pyindex 902dcefce..cc028aa78 100644--- a/backend/prompt_studio/prompt_studio_core/prompt_variable_service.py+++ b/backend/prompt_studio/prompt_studio_core/prompt_variable_service.py@@ -26,14 +26,19 @@ class PromptStudioVariableService:variable_prompt: ToolStudioPrompt = ToolStudioPrompt.objects.get(prompt_key=variable, tool_id=tool_id)- output = PromptStudioOutputManager.objects.get(- prompt_id=variable_prompt.prompt_id,- document_manager=doc_id,- tool_id=variable_prompt.tool_id,- profile_manager=variable_prompt.profile_manager,- )- if not output:- raise PromptNotRun()+ try:+ output = PromptStudioOutputManager.objects.get(+ prompt_id=variable_prompt.prompt_id,+ document_manager=doc_id,+ tool_id=variable_prompt.tool_id,+ profile_manager=variable_prompt.profile_manager,+ )+ except PromptStudioOutputManager.DoesNotExist:+ raise PromptNotRun(+ f"The prompt : {variable} must be executed before "+ "it can be used as a variable in another prompt. "+ "Please execute the prompt first and try again."+ )return output.output@staticmethoddiff --git a/prompt-service/src/unstract/prompt_service/variable_extractor/base.py b/prompt-service/src/unstract/prompt_service/variable_extractor/base.pyindex f1cb85348..99b234e90 100644--- a/prompt-service/src/unstract/prompt_service/variable_extractor/base.py+++ b/prompt-service/src/unstract/prompt_service/variable_extractor/base.py@@ -20,6 +20,6 @@ class VariableExtractor:if variable_type == VariableType.DYNAMIC:prompt = VariableService.replace_dynamic_variable(- prompt=prompt, variable=variable+ prompt=prompt, variable=variable, structured_output=variable_map)return promptdiff --git a/prompt-service/src/unstract/prompt_service/variable_extractor/prompt_variable_service.py b/prompt-service/src/unstract/prompt_service/variable_extractor/prompt_variable_service.pyindex 94f089c98..29611ef9f 100644--- a/prompt-service/src/unstract/prompt_service/variable_extractor/prompt_variable_service.py+++ b/prompt-service/src/unstract/prompt_service/variable_extractor/prompt_variable_service.py@@ -45,7 +45,7 @@ class VariableService:@staticmethoddef replace_generic_string_value(prompt: str, variable: str, value: str) -> str:- replaced_prompt = re.sub(variable, value, prompt)+ replaced_prompt = prompt.replace(variable, value)return replaced_prompt@staticmethod@@ -59,10 +59,19 @@ class VariableService:return variable_type@staticmethod- def replace_dynamic_variable(prompt: str, variable: str) -> str:+ def replace_dynamic_variable(+ prompt: str, variable: str, structured_output: dict[str, Any]+ ) -> str:url = re.search(VariableConstants.DYNAMIC_VARIABLE_URL_REGEX, variable).group(0)data = re.findall(VariableConstants.DYNAMIC_VARIABLE_DATA_REGEX, variable)[0]- api_response = VariableService.fetch_dynamic_variable_value(url=url, data=data)+ output_value = VariableService.check_static_variable_run_status(+ structure_output=structured_output, variable=data+ )+ if not output_value:+ return prompt+ api_response = VariableService.fetch_dynamic_variable_value(+ url=url, data=output_value+ )static_variable_marker_string = "".join(["{{", variable, "}}"])replaced_prompt: str = VariableService.replace_generic_string_value(prompt=prompt, variable=static_variable_marker_string, value=api_response