Skip to content

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

UN-3113 [FEAT] Add custom data support in Prompt Studio (#1719)

Jagadeeshe25029df5be7
diff --git a/backend/prompt_studio/prompt_studio_core_v2/constants.py b/backend/prompt_studio/prompt_studio_core_v2/constants.py
index 645b67431..c1ab14d38 100644
--- a/backend/prompt_studio/prompt_studio_core_v2/constants.py
+++ b/backend/prompt_studio/prompt_studio_core_v2/constants.py
@@ -103,6 +103,7 @@ class ToolStudioPromptKeys:
REQUIRED = "required"
EXECUTION_SOURCE = "execution_source"
LINE_ITEM = "line-item"
+ CUSTOM_DATA = "custom_data"
# Webhook postprocessing settings
ENABLE_POSTPROCESSING_WEBHOOK = "enable_postprocessing_webhook"
POSTPROCESSING_WEBHOOK_URL = "postprocessing_webhook_url"
diff --git a/backend/prompt_studio/prompt_studio_core_v2/migrations/0006_add_custom_data_to_customtool.py b/backend/prompt_studio/prompt_studio_core_v2/migrations/0006_add_custom_data_to_customtool.py
new file mode 100644
index 000000000..07ce6aed3
--- /dev/null
+++ b/backend/prompt_studio/prompt_studio_core_v2/migrations/0006_add_custom_data_to_customtool.py
@@ -0,0 +1,21 @@
+# Generated by Django 4.2.1 on 2025-12-21 20:06
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("prompt_studio_core_v2", "0005_customtool_enable_word_confidence"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="customtool",
+ name="custom_data",
+ field=models.JSONField(
+ blank=True,
+ db_comment="Custom data for variable replacement in prompts using {{custom_data.key}} syntax",
+ null=True,
+ ),
+ ),
+ ]
diff --git a/backend/prompt_studio/prompt_studio_core_v2/models.py b/backend/prompt_studio/prompt_studio_core_v2/models.py
index c2ca4ac7b..e125b6e50 100644
--- a/backend/prompt_studio/prompt_studio_core_v2/models.py
+++ b/backend/prompt_studio/prompt_studio_core_v2/models.py
@@ -142,6 +142,11 @@ class CustomTool(DefaultOrganizationMixin, BaseModel):
db_comment="Flag to enable or disable word-level confidence (depends on enable_highlight)",
default=False,
)
+ custom_data = models.JSONField(
+ null=True,
+ blank=True,
+ db_comment="Custom data for variable replacement in prompts using {{custom_data.key}} syntax",
+ )
# Introduced field to establish M2M relation between users and custom_tool.
# This will introduce intermediary table which relates both the models.
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.py
index 1f869cdb4..991adbcfc 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
@@ -991,6 +991,7 @@ class PromptStudioHelper:
TSPKeys.FILE_PATH: doc_path,
Common.LOG_EVENTS_ID: StateStore.get(Common.LOG_EVENTS_ID),
TSPKeys.EXECUTION_SOURCE: ExecutionSource.IDE.value,
+ TSPKeys.CUSTOM_DATA: tool.custom_data,
}
try:
@@ -1284,6 +1285,7 @@ class PromptStudioHelper:
TSPKeys.FILE_PATH: file_path,
Common.LOG_EVENTS_ID: StateStore.get(Common.LOG_EVENTS_ID),
TSPKeys.EXECUTION_SOURCE: ExecutionSource.IDE.value,
+ TSPKeys.CUSTOM_DATA: tool.custom_data,
}
responder = PromptTool(
diff --git a/backend/prompt_studio/prompt_studio_core_v2/prompt_variable_service.py b/backend/prompt_studio/prompt_studio_core_v2/prompt_variable_service.py
index 243cb10f3..008ba078c 100644
--- a/backend/prompt_studio/prompt_studio_core_v2/prompt_variable_service.py
+++ b/backend/prompt_studio/prompt_studio_core_v2/prompt_variable_service.py
@@ -12,12 +12,17 @@ from prompt_studio.prompt_studio_v2.models import ToolStudioPrompt
class VariableType(str, Enum):
STATIC = "STATIC"
DYNAMIC = "DYNAMIC"
+ CUSTOM_DATA = "CUSTOM_DATA"
class VariableConstants:
VARIABLE_REGEX = "{{(.+?)}}"
DYNAMIC_VARIABLE_DATA_REGEX = r"\[(.*?)\]"
- DYNAMIC_VARIABLE_URL_REGEX = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))" # noqa: E501
+ DYNAMIC_VARIABLE_URL_REGEX = (
+ r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»"
+ "'']))"
+ ) # noqa: E501
+ CUSTOM_DATA_VARIABLE_REGEX = r"custom_data\.([a-zA-Z0-9_.]+)"
class PromptStudioVariableService:
@@ -45,11 +50,18 @@ class PromptStudioVariableService:
@staticmethod
def identify_variable_type(variable: str) -> VariableType:
variable_type: VariableType
- pattern = re.compile(VariableConstants.DYNAMIC_VARIABLE_URL_REGEX)
- if re.findall(pattern, variable):
- variable_type = VariableType.DYNAMIC
+
+ # Check for custom_data variable type first
+ custom_data_pattern = re.compile(VariableConstants.CUSTOM_DATA_VARIABLE_REGEX)
+ if re.findall(custom_data_pattern, variable):
+ variable_type = VariableType.CUSTOM_DATA
else:
- variable_type = VariableType.STATIC
+ # Check for dynamic variable type
+ dynamic_pattern = re.compile(VariableConstants.DYNAMIC_VARIABLE_URL_REGEX)
+ if re.findall(dynamic_pattern, variable):
+ variable_type = VariableType.DYNAMIC
+ else:
+ variable_type = VariableType.STATIC
return variable_type
@staticmethod
@@ -71,6 +83,9 @@ class PromptStudioVariableService:
variable_type: VariableType = (
PromptStudioVariableService.identify_variable_type(variable=variable)
)
+ # Skip custom_data variables - they are handled by the prompt-service
+ if variable_type == VariableType.CUSTOM_DATA:
+ continue
if variable_type == VariableType.STATIC:
variable_output_map[variable] = (
PromptStudioVariableService.fetch_variable_outputs(
diff --git a/frontend/src/components/custom-tools/custom-data-settings/CustomDataSettings.css b/frontend/src/components/custom-tools/custom-data-settings/CustomDataSettings.css
new file mode 100644
index 000000000..c296f4142
--- /dev/null
+++ b/frontend/src/components/custom-tools/custom-data-settings/CustomDataSettings.css
@@ -0,0 +1,74 @@
+.custom-data-description {
+ margin-bottom: 16px;
+}
+
+.custom-data-error {
+ margin-bottom: 16px;
+}
+
+.custom-data-editor-container {
+ border: 1px solid #d9d9d9;
+ border-radius: 6px;
+ overflow: hidden;
+ margin-bottom: 16px;
+}
+
+.custom-data-variables-section {
+ background-color: #fafafa;
+ border: 1px solid #e8e8e8;
+ border-radius: 6px;
+ padding: 16px;
+ margin-bottom: 16px;
+}
+
+.custom-data-variables-header {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ margin-bottom: 12px;
+}
+
+.custom-data-warning-tag {
+ margin-left: auto;
+}
+
+.custom-data-variables-list {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+
+.custom-data-variable-item {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 4px 8px;
+ background-color: #fff;
+ border-radius: 4px;
+}
+
+.custom-data-icon-success {
+ color: #52c41a;
+}
+
+.custom-data-icon-warning {
+ color: #faad14;
+}
+
+.custom-data-variable-undefined {
+ color: #faad14;
+}
+
+.custom-data-variable-status {
+ font-size: 12px;
+ margin-left: 4px;
+}
+
+.custom-data-no-variables {
+ background-color: #fafafa;
+ border: 1px solid #e8e8e8;
+ border-radius: 6px;
+ padding: 16px;
+ margin-bottom: 16px;
+ text-align: center;
+}
diff --git a/frontend/src/components/custom-tools/custom-data-settings/CustomDataSettings.jsx b/frontend/src/components/custom-tools/custom-data-settings/CustomDataSettings.jsx
new file mode 100644
index 000000000..7bcd13924
--- /dev/null
+++ b/frontend/src/components/custom-tools/custom-data-settings/CustomDataSettings.jsx
@@ -0,0 +1,304 @@
+import { useEffect, useState, useMemo } from "react";
+import { Typography, Space, Alert, Tag } from "antd";
+import {
+ CheckCircleOutlined,
+ WarningOutlined,
+ InfoCircleOutlined,
+} from "@ant-design/icons";
+import Editor from "@monaco-editor/react";
+
+import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
+import { useAlertStore } from "../../../store/alert-store";
+import { useCustomToolStore } from "../../../store/custom-tool-store";
+import { useSessionStore } from "../../../store/session-store";
+import { useExceptionHandler } from "../../../hooks/useExceptionHandler";
+import { CustomButton } from "../../widgets/custom-button/CustomButton";
+import SpaceWrapper from "../../widgets/space-wrapper/SpaceWrapper";
+import { promptType } from "../../../helpers/GetStaticData";
+import "./CustomDataSettings.css";
+
+// Regex to match {{custom_data.xyz}} variables in prompts
+const CUSTOM_DATA_VARIABLE_REGEX = /\{\{custom_data\.([a-zA-Z0-9_.]+)\}\}/g;
+
+// Helper function to extract all custom_data variables from text
+const extractCustomDataVariables = (text) => {
+ const variables = [];
+ if (!text) return variables;
+
+ const matches = text.matchAll(CUSTOM_DATA_VARIABLE_REGEX);
+ for (const match of matches) {
+ variables.push(match[1]);
+ }
+ return variables;
+};
+
+// Check if a nested variable path exists in the data
+const checkVariableDefined = (data, variablePath) => {
+ const parts = variablePath.split(".");
+ let current = data;
+
+ for (const part of parts) {
+ if (current === null || current === undefined) {
+ return false;
+ }
+ if (typeof current !== "object" || !(part in current)) {
+ return false;
+ }
+ current = current[part];
+ }
+
+ return current !== undefined;
+};
+
+function CustomDataSettings() {
+ const [jsonValue, setJsonValue] = useState("");
+ const [jsonError, setJsonError] = useState(null);
+ const [isLoading, setIsLoading] = useState(false);
+ const [hasChanges, setHasChanges] = useState(false);
+ const { sessionDetails } = useSessionStore();
+ const { details, isPublicSource, updateCustomTool } = useCustomToolStore();
+ const { setAlertDetails } = useAlertStore();
+ const axiosPrivate = useAxiosPrivate();
+ const handleException = useExceptionHandler();
+
+ // Initialize editor with current custom_data
+ useEffect(() => {
+ const customData = details?.custom_data;
+ if (customData && Object.keys(customData).length > 0) {
+ setJsonValue(JSON.stringify(customData, null, 2));
+ } else {
+ setJsonValue("{\n \n}");
+ }
+ setHasChanges(false);
+ }, [details?.custom_data]);
+
+ // Extract variables from active prompts
+ const extractedVariables = useMemo(() => {
+ const prompts = details?.prompts || [];
+ const variables = new Set();
+
+ prompts.forEach((prompt) => {
+ // Only scan active prompts (excluding notes)
+ // Use !== false to include prompts where active is true or undefined
+ if (
+ prompt?.active !== false &&
+ prompt?.prompt_type !== promptType.notes
+ ) {
+ const promptText = prompt?.prompt || "";
+ const foundVariables = extractCustomDataVariables(promptText);
+ foundVariables.forEach((v) => variables.add(v));
+ }
+ });
+
+ return Array.from(variables);
+ }, [details?.prompts]);
+
+ // Parse current JSON and check which variables are defined
+ const variableStatus = useMemo(() => {
+ let parsedData = {};
+ try {
+ if (jsonValue && jsonValue.trim()) {
+ parsedData = JSON.parse(jsonValue);
+ }
+ } catch {
+ // If JSON is invalid, treat all as undefined
+ }
+
+ return extractedVariables.map((variable) => {
+ const isDefined = checkVariableDefined(parsedData, variable);
+ return { variable, isDefined };
+ });
+ }, [extractedVariables, jsonValue]);
+
+ const handleEditorChange = (value) => {
+ setJsonValue(value || "");
+ setHasChanges(true);
+
+ // Validate JSON
+ try {
+ if (value && value.trim()) {
+ JSON.parse(value);
+ }
+ setJsonError(null);
+ } catch (e) {
+ setJsonError(e.message);
+ }
+ };
+
+ const handleSave = () => {
+ // Validate JSON before saving
+ let customData = null;
+ try {
+ if (jsonValue && jsonValue.trim() && jsonValue.trim() !== "{}") {
+ customData = JSON.parse(jsonValue);
+ if (typeof customData !== "object" || Array.isArray(customData)) {
+ setJsonError("Custom data must be a JSON object");
+ return;
+ }
+ }
+ } catch (e) {
+ setJsonError(e.message);
+ return;
+ }
+
+ const body = {
+ custom_data: customData,
+ };
+
+ const requestOptions = {
+ method: "PATCH",
+ url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/${details?.tool_id}/`,
+ headers: {
+ "X-CSRFToken": sessionDetails?.csrfToken,
+ "Content-Type": "application/json",
+ },
+ data: body,
+ };
+
+ setIsLoading(true);
+ axiosPrivate(requestOptions)
+ .then((res) => {
+ setAlertDetails({
+ type: "success",
+ content: "Custom data saved successfully",
+ });
+ setHasChanges(false);
+ // Update the store with the new details
+ const updatedDetails = res?.data;
+ if (updatedDetails) {
+ updateCustomTool({ details: updatedDetails });
+ }
+ })
+ .catch((err) => {
+ setAlertDetails(handleException(err, "Failed to save custom data"));
+ })
+ .finally(() => {
+ setIsLoading(false);
+ });
+ };
+
+ const undefinedCount = variableStatus.filter((v) => !v.isDefined).length;
+
+ return (
+ <div className="settings-body-pad-top">
+ <SpaceWrapper>
+ <div>
+ <Typography.Text className="add-cus-tool-header">
+ Custom Data
+ </Typography.Text>
+ </div>
+
+ <div className="custom-data-description">
+ <Typography.Text type="secondary">
+ Define test data that can be referenced in prompts using{" "}
+ <Typography.Text code>{"{{custom_data.key}}"}</Typography.Text>{" "}
+ syntax. Supports nested access like{" "}
+ <Typography.Text code>
+ {"{{custom_data.user.name}}"}
+ </Typography.Text>
+ .
+ </Typography.Text>
+ </div>
+
+ {jsonError && (
+ <Alert
+ message="Invalid JSON"
+ description={jsonError}
+ type="error"
+ showIcon
+ className="custom-data-error"
+ />
+ )}
+
+ <div className="custom-data-editor-container">
+ <Editor
+ height="300px"
+ language="json"
+ value={jsonValue}
+ onChange={handleEditorChange}
+ options={{
+ minimap: { enabled: false },
+ scrollBeyondLastLine: false,
+ fontSize: 14,
+ lineNumbers: "on",
+ tabSize: 2,
+ automaticLayout: true,
+ readOnly: isPublicSource,
+ wordWrap: "on",
+ }}
+ theme="vs-dark"
+ />
+ </div>
+
+ {extractedVariables.length > 0 && (
+ <div className="custom-data-variables-section">
+ <div className="custom-data-variables-header">
+ <InfoCircleOutlined />
+ <Typography.Text strong>
+ Variables Referenced in Active Prompts
+ </Typography.Text>
+ {undefinedCount > 0 && (
+ <Tag color="warning" className="custom-data-warning-tag">
+ {undefinedCount} undefined
+ </Tag>
+ )}
+ </div>
+ <div className="custom-data-variables-list">
+ {variableStatus.map(({ variable, isDefined }) => (
+ <div key={variable} className="custom-data-variable-item">
+ {isDefined ? (
+ <CheckCircleOutlined className="custom-data-icon-success" />
+ ) : (
+ <WarningOutlined className="custom-data-icon-warning" />
+ )}
+ <Typography.Text
+ code
+ className={
+ isDefined ? "" : "custom-data-variable-undefined"
+ }
+ >
+ custom_data.{variable}
+ </Typography.Text>
+ <Typography.Text
+ type={isDefined ? "success" : "warning"}
+ className="custom-data-variable-status"
+ >
+ {isDefined ? "(defined)" : "(not defined)"}
+ </Typography.Text>
+ </div>
+ ))}
+ </div>
+ </div>
+ )}
+
+ {extractedVariables.length === 0 && (
+ <div className="custom-data-no-variables">
+ <Typography.Text type="secondary">
+ No custom_data variables found in active prompts. Add variables
+ like{" "}
+ <Typography.Text code>
+ {"{{custom_data.your_key}}"}
+ </Typography.Text>{" "}
+ to your prompts to use this feature.
+ </Typography.Text>
+ </div>
+ )}
+
+ <div className="display-flex-right">
+ <Space>
+ <CustomButton
+ type="primary"
+ onClick={handleSave}
+ loading={isLoading}
+ disabled={isPublicSource || !!jsonError || !hasChanges}
+ >
+ Save
+ </CustomButton>
+ </Space>
+ </div>
+ </SpaceWrapper>
+ </div>
+ );
+}
+
+export { CustomDataSettings };
diff --git a/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx b/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx
index cc16a13f1..0978bd3e2 100644
--- a/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx
+++ b/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx
@@ -153,7 +153,23 @@ function DocumentParser({
};
return axiosPrivate(requestOptions)
- .then((res) => res)
+ .then((res) => {
+ // Update the store with the modified prompt
+ const updatedPrompt = res?.data;
+ if (updatedPrompt) {
+ const modifiedDetails = { ...details };
+ const modifiedPrompts = [...(modifiedDetails?.prompts || [])];
+ const promptIndex = modifiedPrompts.findIndex(
+ (item) => item?.prompt_id === promptId
+ );
+ if (promptIndex !== -1) {
+ modifiedPrompts[promptIndex] = updatedPrompt;
+ modifiedDetails["prompts"] = modifiedPrompts;
+ updateCustomTool({ details: modifiedDetails });
+ }
+ }
+ return res;
+ })
.catch((err) => {
setAlertDetails(handleException(err, "Failed to update"));
});
diff --git a/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx b/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx
index a3f41300f..4c3a1eac8 100644
--- a/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx
+++ b/frontend/src/components/custom-tools/settings-modal/SettingsModal.jsx
@@ -3,6 +3,7 @@ import PropTypes from "prop-types";
import { useEffect, useState } from "react";
import {
CodeOutlined,
+ DatabaseOutlined,
DiffOutlined,
FileTextOutlined,
MessageOutlined,
@@ -13,6 +14,7 @@ import { getMenuItem } from "../../../helpers/GetStaticData";
import { ManageLlmProfiles } from "../manage-llm-profiles/ManageLlmProfiles";
import { CustomSynonyms } from "../custom-synonyms/CustomSynonyms";
import { PreAndPostAmbleModal } from "../pre-and-post-amble-modal/PreAndPostAmbleModal";
+import { CustomDataSettings } from "../custom-data-settings/CustomDataSettings";
import "./SettingsModal.css";
@@ -38,6 +40,7 @@ function SettingsModal({ open, setOpen, handleUpdateTool }) {
useEffect(() => {
const items = [
getMenuItem("LLM Profiles", 1, <CodeOutlined />),
+ getMenuItem("Custom Data", 9, <DatabaseOutlined />),
getMenuItem("Grammar", 5, <MessageOutlined />),
getMenuItem("Preamble", 6, <DiffOutlined />),
getMenuItem("Postamble", 7, <DiffOutlined />),
@@ -58,6 +61,7 @@ function SettingsModal({ open, setOpen, handleUpdateTool }) {
handleUpdateTool={handleUpdateTool}
/>
),
+ 9: <CustomDataSettings />,
};
let position = 1;
diff --git a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py
index da7753080..fdf0deaae 100644
--- a/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py
+++ b/prompt-service/src/unstract/prompt_service/controllers/answer_prompt.py
@@ -87,6 +87,8 @@ def prompt_processor() -> Any:
util = PromptServiceBaseTool(platform_key=platform_key)
index = Index(tool=util, run_id=run_id, capture_metrics=True)
if VariableReplacementService.is_variables_present(prompt_text=prompt_text):
+ # Determine if this is from IDE (Prompt Studio) or API deployment
+ is_ide = execution_source == "ide"
prompt_text = VariableReplacementService.replace_variables_in_prompt(
prompt=output,
structured_output=structured_output,
@@ -95,6 +97,7 @@ def prompt_processor() -> Any:
prompt_name=prompt_name,
doc_name=doc_name,
custom_data=custom_data,
+ is_ide=is_ide,
)
app.logger.info(f"[{tool_id}] Executing prompt: '{prompt_name}'")
diff --git a/prompt-service/src/unstract/prompt_service/exceptions.py b/prompt-service/src/unstract/prompt_service/exceptions.py
index 15f2c19fa..98634ed5f 100644
--- a/prompt-service/src/unstract/prompt_service/exceptions.py
+++ b/prompt-service/src/unstract/prompt_service/exceptions.py
@@ -30,3 +30,27 @@ class RetrievalError(APIError):
class ExtractionError(APIError):
DEFAULT_MESSAGE = "Error while extracting from a document"
+
+
+class UnprocessableEntity(APIError):
+ code = 422
+ message = "Unprocessable Entity"
+
+
+class CustomDataError(APIError):
+ """Custom exception raised for errors with custom_data variables."""
+
+ code = 400
+
+ def __init__(self, variable: str, reason: str, is_ide: bool = True):
+ if is_ide:
+ help_text = "Please define this key in Prompt Studio Settings > Custom Data."
+ else:
+ help_text = (
+ "Please include this key in the 'custom_data' field of your API request."
+ )
+ variable_display = "{{custom_data." + variable + "}}"
+ message = (
+ f"Custom data error for variable '{variable_display}': {reason} {help_text}"
+ )
+ super().__init__(message=message)
diff --git a/prompt-service/src/unstract/prompt_service/helpers/variable_replacement.py b/prompt-service/src/unstract/prompt_service/helpers/variable_replacement.py
index dc6e3f43a..90673928b 100644
--- a/prompt-service/src/unstract/prompt_service/helpers/variable_replacement.py
+++ b/prompt-service/src/unstract/prompt_service/helpers/variable_replacement.py
@@ -6,6 +6,7 @@ from typing import Any
from flask import current_app as app
from unstract.prompt_service.constants import VariableConstants, VariableType
+from unstract.prompt_service.exceptions import CustomDataError
from unstract.prompt_service.utils.request import HTTPMethod, make_http_request
@@ -101,7 +102,10 @@ class VariableReplacementHelper:
@staticmethod
def replace_custom_data_variable(
- prompt: str, variable: str, custom_data: dict[str, Any]
+ prompt: str,
+ variable: str,
+ custom_data: dict[str, Any],
+ is_ide: bool = True,
) -> str:
"""Replace custom_data variable in prompt.
@@ -109,38 +113,39 @@ class VariableReplacementHelper:
prompt: The prompt containing variables
variable: The variable to replace (e.g., "custom_data.name")
custom_data: The custom_data data dictionary
+ is_ide: Whether this is running from Prompt Studio IDE (affects error messages)
Returns:
prompt with variable replaced
"""
- if not custom_data:
- error_msg = f"Custom data is empty. Unable to replace variable {variable}"
- app.logger.error(error_msg)
- raise ValueError(error_msg)
-
# Extract the path from custom_data.path.to.value
custom_data_match = re.search(
VariableConstants.CUSTOM_DATA_VARIABLE_REGEX, variable
)
if not custom_data_match:
- error_msg = f"Invalid custom_data variable format: {variable}"
- app.logger.error(error_msg)
- raise ValueError(error_msg)
+ error_msg = "Invalid variable format."
+ app.logger.error(f"{error_msg}: {variable}")
+ raise CustomDataError(variable=variable, reason=error_msg, is_ide=is_ide)
path_str = custom_data_match.group(1)
path_parts = path_str.split(".")
+ if not custom_data:
+ error_msg = "Custom data is not configured."
+ app.logger.error(error_msg)
+ raise CustomDataError(variable=path_str, reason=error_msg, is_ide=is_ide)
+
# Navigate through the nested dictionary
try:
value = custom_data
for part in path_parts:
value = value[part]
except (KeyError, TypeError) as e:
- error_msg = (
- f"Path {path_str} not found in custom_data for variable {variable}"
- )
+ error_msg = f"Key '{path_str}' not found in custom data."
app.logger.error(error_msg)
- raise ValueError(error_msg) from e
+ raise CustomDataError(
+ variable=path_str, reason=error_msg, is_ide=is_ide
+ ) from e
# Format the value and replace in prompt
formatted_value = VariableReplacementHelper.handle_json_and_str_types(value)
diff --git a/prompt-service/src/unstract/prompt_service/services/variable_replacement.py b/prompt-service/src/unstract/prompt_service/services/variable_replacement.py
index 75c6d2dd1..41ba342ac 100644
--- a/prompt-service/src/unstract/prompt_service/services/variable_replacement.py
+++ b/prompt-service/src/unstract/prompt_service/services/variable_replacement.py
@@ -35,6 +35,7 @@ class VariableReplacementService:
prompt_name: str,
doc_name: str,
custom_data: dict[str, Any] = None,
+ is_ide: bool = True,
) -> str:
"""Replaces variables in prompt.
@@ -70,6 +71,7 @@ class VariableReplacementService:
prompt_text=prompt[PSKeys.PROMPT],
variable_map=variable_map,
custom_data=custom_data,
+ is_ide=is_ide,
)
except KeyError:
# Executed incase of structured tool and
@@ -78,6 +80,7 @@ class VariableReplacementService:
prompt_text=prompt_text,
variable_map=structured_output,
custom_data=custom_data,
+ is_ide=is_ide,
)
finally:
publish_log(
@@ -98,6 +101,7 @@ class VariableReplacementService:
prompt_text: str,
variable_map: dict[str, Any],
custom_data: dict[str, Any] = None,
+ is_ide: bool = True,
) -> str:
variables: list[str] = VariableReplacementHelper.extract_variables_from_prompt(
prompt_text=prompt_text
@@ -120,10 +124,11 @@ class VariableReplacementService:
structured_output=variable_map,
)
- elif variable_type == VariableType.CUSTOM_DATA and custom_data:
+ elif variable_type == VariableType.CUSTOM_DATA:
prompt_text = VariableReplacementHelper.replace_custom_data_variable(
prompt=prompt_text,
variable=variable,
- custom_data=custom_data,
+ custom_data=custom_data or {},
+ is_ide=is_ide,
)
return prompt_text