feat: Add JSON parsers infrastructure (Phase 1)

- Create json_utils.py with parse_json_dict() and parse_json_list() functions
- Add comprehensive unit tests (49 tests, all passing)
- Add deprecation warnings to string_utils.py for pipe-delimited parsing
- Follows PRD Phase 1: Parser Infrastructure

This is the first step in replacing pipe-delimited LLM output format with
structured JSON format as per PRD_STANDARDIZE_LIST_FORMATS.md
This commit is contained in:
ppanchigar
2026-02-02 16:44:29 -06:00
parent 5d1024e9dc
commit f633922381
3 changed files with 633 additions and 0 deletions
+26
View File
@@ -35,6 +35,12 @@ def extract_text_from_delimiters(
Returns:
- str: The extracted answer or "N/A" if no match is found.
.. deprecated:: 2026.02
Using Delimiter.PIPE with this function is deprecated and will be removed
in a future version. Prompts should return JSON format instead of
pipe-delimited strings. Use `json_utils.parse_json_list()` or
`json_utils.parse_json_dict()` for parsing JSON responses.
"""
if not isinstance(raw_output, str):
raise TypeError(
@@ -50,6 +56,13 @@ def extract_text_from_delimiters(
# Define a pattern based on the delimiter type
if delimiter == Delimiter.PIPE:
warnings.warn(
"Delimiter.PIPE is deprecated for LLM output parsing. "
"Prompts should return JSON format instead. "
"Use json_utils.parse_json_list() or json_utils.parse_json_dict() instead.",
DeprecationWarning,
stacklevel=2,
)
pattern = PIPE_PATTERN
elif delimiter == Delimiter.BACKTICK:
pattern = BACKTICK_PATTERN
@@ -191,7 +204,20 @@ def universal_json_load(s: str):
Extract and parse all highest-level valid JSON objects from a string.
Returns the last valid top-level JSON object found (dict, list, etc).
Raises ValueError if no valid JSON objects are found. Does not clean up syntax.
.. deprecated:: 2026.02
This function is deprecated and will be removed in a future version.
Use `json_utils.parse_json_dict()` for dictionary outputs or
`json_utils.parse_json_list()` for list outputs instead.
The new parsers provide type-specific parsing with better error handling.
"""
warnings.warn(
"universal_json_load is deprecated and will be removed in a future version. "
"Use json_utils.parse_json_dict() or json_utils.parse_json_list() instead.",
DeprecationWarning,
stacklevel=2,
)
if not isinstance(s, str):
raise TypeError(f"Expected string input, got {type(s).__name__}")
found = []