Skip to content
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

Add json-repair #279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"litellm>=1.54",
"termcolor~=2.0",
"ipython~=8.0",
"json-repair~=0.35",
]
authors = [
{ name="Mandana Vaziri", email="[email protected]" },
Expand Down
10 changes: 7 additions & 3 deletions src/pdl/pdl_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Any, Generator, Optional, Sequence, TypeVar # noqa: E402

import httpx # noqa: E402
import json_repair # noqa: E402
import litellm # noqa: E402
import yaml # noqa: E402
from jinja2 import ( # noqa: E402
Expand Down Expand Up @@ -1530,12 +1531,15 @@ def step_include(
) from exc


def parse_result(parser: ParserType, text: str) -> Optional[dict[str, Any] | list[Any]]:
result: Optional[dict[str, Any] | list[Any]]
JSONReturnType = dict[str, Any] | list[Any] | str | float | int | bool | None


def parse_result(parser: ParserType, text: str) -> JSONReturnType:
result: JSONReturnType
match parser:
case "json":
try:
result = json.loads(text)
result = json_repair.loads(text) # type: ignore[reportAssignmentType]
except Exception as exc:
raise PDLRuntimeParserError(
f"Attempted to parse ill-formed JSON: {repr(exc)}"
Expand Down
23 changes: 23 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ def test_code_parser1():


def test_json_parser():
# Test json-repair,
# see https://github.com/mangiucugna/json_repair/blob/main/tests/test_json_repair.py
json_parser = """
text: |
The next 64 elements are:
```json
{ "key": "value" }
```
parser: json
"""
result = exec_str(json_parser)
assert result == {"key": "value"}

json_parser = """
text: |
{'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}
parser: json
"""
result = exec_str(json_parser)
assert result == {"key": "string", "key2": False, "key3": None, "key4": "unquoted"}


def test_jsonl_parser():
jsonl_parser = """
text: |
{ "a": 1, "b": 2}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_runtime_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def test_jinja_syntax():
)


def test_parser_json():
def test_parser_jsonl():
prog_str = """
text: "{ x: 1 + 1 }"
parser: json
parser: jsonl
"""
with pytest.raises(PDLRuntimeError) as exc:
exec_str(prog_str)
Expand Down
Loading