-
Notifications
You must be signed in to change notification settings - Fork 626
UN-2653 [FIX] Fixed issue in JSON structure parsing #1467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vishnuszipstack
merged 3 commits into
main
from
UN-2653-bug-rc-26-the-json-structure-collapses-when-enforce-type-is-json-in-unstract-but-the-same-works-fine-when-enforce-type-is-text
Aug 2, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
prompt-service/src/unstract/prompt_service/utils/json_repair_helper.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """JSON repair utility functions.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from json_repair import repair_json | ||
|
|
||
|
|
||
| def repair_json_with_best_structure(json_str: str) -> Any: | ||
| """Intelligently repair JSON string using the best parsing strategy. | ||
|
|
||
| This function attempts to parse JSON in two ways: | ||
| 1. As-is (could be valid object, array, or partial JSON) | ||
| 2. With array wrapping (useful for comma-separated objects) | ||
|
|
||
| It chooses the result based on structural integrity rather than string length. | ||
|
|
||
| Args: | ||
| json_str: The JSON string to repair | ||
|
|
||
| Returns: | ||
| The parsed JSON object with the best structure | ||
| """ | ||
| # Attempt parsing as-is | ||
| parsed_as_is = repair_json(json_str=json_str, return_objects=True, ensure_ascii=False) | ||
|
|
||
| # Attempt parsing with array wrap | ||
| parsed_with_wrap = repair_json( | ||
| json_str="[" + json_str, return_objects=True, ensure_ascii=False | ||
| ) | ||
|
Deepak-Kesavan marked this conversation as resolved.
|
||
|
|
||
| # If both results are strings, return the as-is result | ||
|
Deepak-Kesavan marked this conversation as resolved.
|
||
| if isinstance(parsed_as_is, str) and isinstance(parsed_with_wrap, str): | ||
| return parsed_as_is | ||
|
|
||
| # If only one is a string, return the non-string result | ||
| if isinstance(parsed_as_is, str): | ||
| return parsed_with_wrap | ||
| if isinstance(parsed_with_wrap, str): | ||
| return parsed_as_is | ||
|
|
||
| # Both are valid structures - choose based on structure analysis | ||
| # If parsed_with_wrap is a list with exactly one element that equals parsed_as_is, | ||
| # then the original was already valid and wrapping just added unnecessary array | ||
| if ( | ||
| isinstance(parsed_with_wrap, list) | ||
| and len(parsed_with_wrap) == 1 | ||
| and parsed_with_wrap[0] == parsed_as_is | ||
| ): | ||
| return parsed_as_is | ||
|
|
||
| # If parsed_as_is is a valid structure (dict or list), prefer it | ||
| # unless parsed_with_wrap provides a more complete structure | ||
| if isinstance(parsed_as_is, (dict, list)): | ||
| # Check if the wrapped version provides multiple objects that were | ||
| # incorrectly concatenated in the original (e.g., {},{},{}) | ||
| if isinstance(parsed_with_wrap, list) and len(parsed_with_wrap) > 1: | ||
| # The original likely had multiple comma-separated objects | ||
| return parsed_with_wrap | ||
| else: | ||
| # The original was already a valid structure | ||
| return parsed_as_is | ||
|
|
||
| # Default to wrapped version if we can't determine otherwise | ||
| return parsed_with_wrap | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.