Skip to content
Merged
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
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Bob lives at the following address:
## Parsing the output of a block

As we saw in the previous section, it is possible to use the `parser: json` setting to parse the result of a block as a JSON.
Other possible values for `parser` are `yaml`, `jsonl`, or `regex`.
Other possible values for `parser` are `yaml`, `jsonl`, `regex`, or `csv`.

The following example extracts using a regular expression parser the code between triple backtick generated by a model:

Expand Down
3 changes: 2 additions & 1 deletion src/pdl/pdl-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3998,7 +3998,8 @@
"enum": [
"json",
"jsonl",
"yaml"
"yaml",
"csv"
],
"type": "string"
},
Expand Down
2 changes: 1 addition & 1 deletion src/pdl/pdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class RegexParser(Parser):


ParserType = TypeAliasType(
"ParserType", Union[Literal["json", "jsonl", "yaml"], PdlParser, RegexParser]
"ParserType", Union[Literal["json", "jsonl", "yaml", "csv"], PdlParser, RegexParser]
)
"""Different parsers."""
OptionalParserType = TypeAliasType("OptionalParserType", Optional[ParserType])
Expand Down
14 changes: 14 additions & 0 deletions src/pdl/pdl_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=import-outside-toplevel
import csv
import json
import re
import shlex
Expand All @@ -13,6 +14,7 @@
from abc import ABC, abstractmethod
from concurrent.futures import ThreadPoolExecutor
from functools import partial, reduce
from io import StringIO
from itertools import count
from os import getenv
from pathlib import Path
Expand Down Expand Up @@ -2647,6 +2649,18 @@ def parse_result(parser: ParserType, text: str) -> JSONReturnType:
raise PDLRuntimeParserError(
f"Attempted to parse ill-formed YAML: {repr(exc)}"
) from exc
case "csv":
try:
result = []
reader = csv.reader(StringIO(text))
for row in reader:
result.append(row)
except KeyboardInterrupt as exc:
raise exc from exc
except Exception as exc:
raise PDLRuntimeParserError(
f"Attempted to parse ill-formed CSV: {repr(exc)}"
) from exc
case PdlParser():
assert False, "TODO"
case RegexParser(mode="search" | "match" | "fullmatch"):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,19 @@ def test_parser_case2():
"""
result = exec_str(prog)
assert result == ["1", "2", "3", "4"]


def test_parser_csv():
csv_parser = """
text: |
1,Apple,Red
2,Orange,Orange
3,Banana,Yellow
parser: csv
"""
result = exec_str(csv_parser)
assert result == [
["1", "Apple", "Red"],
["2", "Orange", "Orange"],
["3", "Banana", "Yellow"],
]