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] Revert cell type merge - Table (#1297)
harini-venkataraman319e9e3a3214
diff --git a/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.pyindex 5e45837ed..49da34b92 100644--- a/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py+++ b/backend/prompt_studio/prompt_studio_core_v2/prompt_studio_helper.py@@ -980,7 +980,7 @@ class PromptStudioHelper:tool_id: str,output: dict[str, Any],) -> dict[str, Any]:- if prompt.enforce_type == TSPKeys.LINE_ITEM:+ if prompt.enforce_type == TSPKeys.TABLE or prompt.enforce_type == TSPKeys.RECORD:extract_doc_path: str = (PromptStudioHelper._get_extract_or_summary_document_path(org_id, user_id, tool_id, doc_name, TSPKeys.EXTRACTdiff --git a/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json b/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.jsonindex 39265bece..81d76bfb8 100644--- a/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json+++ b/backend/prompt_studio/prompt_studio_core_v2/static/select_choices.json@@ -14,7 +14,8 @@"date":"date","boolean":"boolean","json":"json",- "line_item":"line-item"+ "line_item":"line-item",+ "table":"table"},"output_processing":{"DEFAULT":"Default"diff --git a/backend/prompt_studio/prompt_studio_v2/migrations/0011_revert_cell_type_merge.py b/backend/prompt_studio/prompt_studio_v2/migrations/0011_revert_cell_type_merge.pynew file mode 100644index 000000000..b810b289b--- /dev/null+++ b/backend/prompt_studio/prompt_studio_v2/migrations/0011_revert_cell_type_merge.py@@ -0,0 +1,40 @@+# Generated by Django 4.2.1 on 2025-05-09 14:26++from django.db import migrations+++def revert_enforce_type_updates(apps, schema_editor):+ prompt = apps.get_model("prompt_studio_v2", "ToolStudioPrompt")++ # Revert json -> line-item for records where is_line_item is True+ prompt.objects.filter(enforce_type="json", has_line_item_history=True).update(+ enforce_type="line-item"+ )++ # Revert line-item -> table+ prompt.objects.filter(enforce_type="line-item").update(enforce_type="table")+++def re_apply_enforce_type_updates(apps, schema_editor):+ prompt = apps.get_model("prompt_studio_v2", "ToolStudioPrompt")++ # Apply line-item -> json for records with history+ prompt.objects.filter(enforce_type="line-item", has_line_item_history=True).update(+ enforce_type="json"+ )++ # Apply table -> line-item for all+ prompt.objects.filter(enforce_type="table").update(enforce_type="line-item")+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_studio_v2", "0010_update_enforce_type_text"),+ ]++ operations = [+ migrations.RunPython(+ code=revert_enforce_type_updates,+ reverse_code=re_apply_enforce_type_updates,+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_v2/migrations/0012_alter_toolstudioprompt_enforce_type.py b/backend/prompt_studio/prompt_studio_v2/migrations/0012_alter_toolstudioprompt_enforce_type.pynew file mode 100644index 000000000..711f02de3--- /dev/null+++ b/backend/prompt_studio/prompt_studio_v2/migrations/0012_alter_toolstudioprompt_enforce_type.py@@ -0,0 +1,34 @@+# Generated by Django 4.2.1 on 2025-05-09 14:46++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("prompt_studio_v2", "0011_revert_cell_type_merge"),+ ]++ operations = [+ migrations.AlterField(+ model_name="toolstudioprompt",+ name="enforce_type",+ field=models.TextField(+ blank=True,+ choices=[+ ("text", "Response sent as Text"),+ ("number", "Response sent as number"),+ ("email", "Response sent as email"),+ ("date", "Response sent as date"),+ ("boolean", "Response sent as boolean"),+ ("json", "Response sent as json"),+ (+ "line-item",+ "Response sent as line-item which is large a JSON output. If extraction stopped due to token limitation, we try to continue extraction from where it stopped",+ ),+ ("table", "Response sent as json"),+ ],+ db_comment="Field to store the type in which the response to be returned.",+ default="text",+ ),+ ),+ ]diff --git a/backend/prompt_studio/prompt_studio_v2/models.py b/backend/prompt_studio/prompt_studio_v2/models.pyindex 402e79722..5468ceefd 100644--- a/backend/prompt_studio/prompt_studio_v2/models.py+++ b/backend/prompt_studio/prompt_studio_v2/models.py@@ -30,6 +30,7 @@ class ToolStudioPrompt(BaseModel):"we try to continue extraction from where it stopped"),)+ TABLE = "table", "Response sent as json"class PromptType(models.TextChoices):PROMPT = "PROMPT", "Response sent as Text"diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsxindex bb05367a4..cfb4ab6cd 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx@@ -19,7 +19,7 @@ import { useCustomToolStore } from "../../../store/custom-tool-store";import { Header } from "./Header";import { OutputForIndex } from "./OutputForIndex";import { PromptOutput } from "./PromptOutput";-import { LINE_ITEM_ENFORCE_TYPE } from "./constants";+import { TABLE } from "./constants";let TableExtractionSettingsBtn;try {@@ -269,13 +269,12 @@ function PromptCardItems({)}</Space><Space>- {enforceType === LINE_ITEM_ENFORCE_TYPE &&- TableExtractionSettingsBtn && (- <TableExtractionSettingsBtn- promptId={promptDetails?.prompt_id}- enforceType={enforceType}- />- )}+ {enforceType === TABLE && TableExtractionSettingsBtn && (+ <TableExtractionSettingsBtn+ promptId={promptDetails?.prompt_id}+ enforceType={enforceType}+ />+ )}<SelectclassName="prompt-card-select-type"size="small"diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsxindex 71431e036..68ac5b4bb 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx@@ -29,7 +29,7 @@ import {import { TokenUsage } from "../token-usage/TokenUsage";import { useWindowDimensions } from "../../../hooks/useWindowDimensions";import { useCustomToolStore } from "../../../store/custom-tool-store";-import { LINE_ITEM_ENFORCE_TYPE } from "./constants";+import { TABLE } from "./constants";import { CopyPromptOutputBtn } from "./CopyPromptOutputBtn";import { useAlertStore } from "../../../store/alert-store";import { PromptOutputExpandBtn } from "./PromptOutputExpandBtn";@@ -82,7 +82,7 @@ function PromptOutput({} = useCustomToolStore();const { setAlertDetails } = useAlertStore();const { generatePromptOutputKey } = usePromptOutput();- const isTableExtraction = enforceType === LINE_ITEM_ENFORCE_TYPE;+ const isTableExtraction = enforceType === TABLE;const noHighlightEnforceType = !["table", "record"].includes(enforceType);const tooltipContent = (adapterConf) => (<div>diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsxindex 2f264283f..72b92bd0f 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx@@ -2,7 +2,7 @@ import { Col, Image, Modal, Row, Typography } from "antd";import PropTypes from "prop-types";import { DisplayPromptResult } from "./DisplayPromptResult";-import { LINE_ITEM_ENFORCE_TYPE } from "./constants";+import { TABLE } from "./constants";import SpaceWrapper from "../../widgets/space-wrapper/SpaceWrapper";import { useCustomToolStore } from "../../../store/custom-tool-store";import usePromptOutput from "../../../hooks/usePromptOutput";@@ -81,7 +81,7 @@ function PromptOutputsModal({)}</div><div className="flex-1 overflow-y-auto pad-top-10">- {enforceType === LINE_ITEM_ENFORCE_TYPE && TableOutput ? (+ {enforceType === TABLE && TableOutput ? (<TableOutputoutput={promptOutputData?.output}pagination={10}diff --git a/frontend/src/components/custom-tools/prompt-card/constants.js b/frontend/src/components/custom-tools/prompt-card/constants.jsindex cc6c7253d..63b7f0db5 100644--- a/frontend/src/components/custom-tools/prompt-card/constants.js+++ b/frontend/src/components/custom-tools/prompt-card/constants.js@@ -1,4 +1,5 @@const LINE_ITEM_ENFORCE_TYPE = "line-item";+const TABLE = "table";const handleUpdateStatus = (isUpdate, promptId, value, setUpdateStatus) => {if (!isUpdate) {@@ -10,4 +11,4 @@ const handleUpdateStatus = (isUpdate, promptId, value, setUpdateStatus) => {});};-export { handleUpdateStatus, LINE_ITEM_ENFORCE_TYPE };+export { handleUpdateStatus, TABLE, LINE_ITEM_ENFORCE_TYPE };diff --git a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.pyindex 57a533c4c..c0fb39bbf 100644--- a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py@@ -81,17 +81,6 @@ def prompt_processor() -> Any:app.logger.info(f"[{tool_id}] chunk size: {chunk_size}")util = PromptServiceBaseTool(platform_key=platform_key)index = Index(tool=util, run_id=run_id, capture_metrics=True)- # To support backward compatability for cell types- # line-item, table and record.- if execution_source == PSKeys.TOOL:- if output[PSKeys.TYPE] == PSKeys.LINE_ITEM:- if PSKeys.TABLE_SETTINGS not in output:- output[PSKeys.TYPE] = PSKeys.JSON- if (output[PSKeys.TYPE] == PSKeys.TABLE) or (- output[PSKeys.TYPE] == PSKeys.RECORD- ):- output[PSKeys.TYPE] = PSKeys.LINE_ITEM-if VariableReplacementService.is_variables_present(prompt_text=prompt_text):prompt_text = VariableReplacementService.replace_variables_in_prompt(prompt=output,@@ -182,9 +171,9 @@ def prompt_processor() -> Any:)raise APIError(message=msg)- if output[PSKeys.TYPE] == PSKeys.LINE_ITEM:+ if output[PSKeys.TYPE] == PSKeys.TABLE:try:- structured_output = AnswerPromptService.extract_line_item(+ structured_output = AnswerPromptService.extract_table(output=output,structured_output=structured_output,llm=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 0edef84d4..191195221 100644--- a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py+++ b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py@@ -181,7 +181,7 @@ class AnswerPromptService:raise APIError(str(e)) from e@staticmethod- def extract_line_item(+ def extract_table(output: dict[str, Any],structured_output: dict[str, Any],llm: LLM,