Skip to content

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

UN-2653 [FIX] Fixed wrong parsing of nested json array values (#1457)

Deepak K52e394542204
diff --git a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
index 1094dc865..79e5a3dff 100644
--- a/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
+++ b/prompt-service/src/unstract/prompt_service/services/answer_prompt.py
@@ -253,12 +253,12 @@ class AnswerPromptService:
json_str="[" + answer, return_objects=True, ensure_ascii=False
)
- # Heuristic: if wrapping only added '[' and ']', len(b) - len(a) == 2 → original was valid, use 'a'
+ # Heuristic: if wrapping only added '[' and ']', len(b) - len(a) >= 2 → original was valid, use 'a'
# Otherwise, fallback to 'b' which likely fixed multiple items or invalid top-level structure
dump_a = json.dumps(a, ensure_ascii=False)
dump_b = json.dumps(b, ensure_ascii=False)
ARRAY_WRAP_DELTA = 2 # '[' and ']'
- parsed_data = a if len(dump_b) - len(dump_a) == ARRAY_WRAP_DELTA else b
+ parsed_data = a if len(dump_b) - len(dump_a) >= ARRAY_WRAP_DELTA else b
if isinstance(parsed_data, str):
err_msg = "Error parsing response (to json)\n" f"Candidate JSON: {answer}"