Skip to content

Commit bc0fdd4

Browse files
authored
Add json-repair (#279)
* Add json-repair * Fix test Signed-off-by: Claudio Spiess <[email protected]> --------- Signed-off-by: Claudio Spiess <[email protected]>
1 parent 48fc01c commit bc0fdd4

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies = [
1515
"litellm>=1.57.3,<1.59.9",
1616
"termcolor~=2.0",
1717
"ipython~=8.0",
18+
"json-repair~=0.35",
1819
]
1920
authors = [
2021
{ name="Mandana Vaziri", email="[email protected]" },

src/pdl/pdl_interpreter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import Any, Generator, Optional, Sequence, TypeVar # noqa: E402
1818

1919
import httpx # noqa: E402
20+
import json_repair # noqa: E402
2021
import litellm # noqa: E402
2122
import yaml # noqa: E402
2223
from jinja2 import ( # noqa: E402
@@ -1682,12 +1683,15 @@ def process_include(
16821683
) from exc
16831684

16841685

1685-
def parse_result(parser: ParserType, text: str) -> Optional[dict[str, Any] | list[Any]]:
1686-
result: Optional[dict[str, Any] | list[Any]]
1686+
JSONReturnType = dict[str, Any] | list[Any] | str | float | int | bool | None
1687+
1688+
1689+
def parse_result(parser: ParserType, text: str) -> JSONReturnType:
1690+
result: JSONReturnType
16871691
match parser:
16881692
case "json":
16891693
try:
1690-
result = json.loads(text)
1694+
result = json_repair.loads(text) # type: ignore[reportAssignmentType]
16911695
except Exception as exc:
16921696
raise PDLRuntimeParserError(
16931697
f"Attempted to parse ill-formed JSON: {repr(exc)}"

tests/test_parser.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ def test_code_parser1():
105105

106106

107107
def test_json_parser():
108+
# Test json-repair,
109+
# see https://github.com/mangiucugna/json_repair/blob/main/tests/test_json_repair.py
110+
json_parser = """
111+
text: |
112+
The next 64 elements are:
113+
```json
114+
{ "key": "value" }
115+
```
116+
parser: json
117+
"""
118+
result = exec_str(json_parser)
119+
assert result == {"key": "value"}
120+
121+
json_parser = """
122+
text: |
123+
{'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}
124+
parser: json
125+
"""
126+
result = exec_str(json_parser)
127+
assert result == {"key": "string", "key2": False, "key3": None, "key4": "unquoted"}
128+
129+
130+
def test_jsonl_parser():
108131
jsonl_parser = """
109132
text: |
110133
{ "a": 1, "b": 2}

tests/test_runtime_errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ def test_jinja_syntax():
4040
)
4141

4242

43-
def test_parser_json():
43+
def test_parser_jsonl():
4444
prog_str = """
4545
text: "{ x: 1 + 1 }"
46-
parser: json
46+
parser: jsonl
4747
"""
4848
with pytest.raises(PDLRuntimeError) as exc:
4949
exec_str(prog_str)

0 commit comments

Comments
 (0)