Skip to content

Commit

Permalink
core[patch]: improve comma separated list output parser to handle non…
Browse files Browse the repository at this point in the history
…-space separated list (#20434)

- **Description:** Changes
`lanchain_core.output_parsers.CommaSeparatedListOutputParser` to handle
`,` as a delimiter alongside the previous implementation which used `, `
as delimiter.
- **Issue:** Started noticing that some results returned by LLMs were
not getting parsed correctly when the output contained `,` instead of `,
`.
  - **Dependencies:** No
  - **Twitter handle:** not active on twitter.


<!---
If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, hwchase17.
-->
  • Loading branch information
anish749 authored and hinthornw committed Apr 26, 2024
1 parent b8b9c25 commit 77774c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions libs/core/langchain_core/output_parsers/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ def get_lc_namespace(cls) -> List[str]:
def get_format_instructions(self) -> str:
return (
"Your response should be a list of comma separated values, "
"eg: `foo, bar, baz`"
"eg: `foo, bar, baz` or `foo,bar,baz`"
)

def parse(self, text: str) -> List[str]:
"""Parse the output of an LLM call."""
return text.strip().split(", ")
return [part.strip() for part in text.split(",")]

@property
def _type(self) -> str:
Expand Down
21 changes: 20 additions & 1 deletion libs/core/tests/unit_tests/output_parsers/test_list_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,29 @@ def test_single_item() -> None:
assert list(parser.transform(iter([text]))) == [[a] for a in expected]


def test_multiple_items_with_spaces() -> None:
"""Test that a string with multiple comma-separated items
with spaces is parsed to a list."""
parser = CommaSeparatedListOutputParser()
text = "foo, bar, baz"
expected = ["foo", "bar", "baz"]

assert parser.parse(text) == expected
assert add(parser.transform(t for t in text)) == expected
assert list(parser.transform(t for t in text)) == [[a] for a in expected]
assert list(parser.transform(t for t in text.splitlines(keepends=True))) == [
[a] for a in expected
]
assert list(
parser.transform(" " + t if i > 0 else t for i, t in enumerate(text.split(" ")))
) == [[a] for a in expected]
assert list(parser.transform(iter([text]))) == [[a] for a in expected]


def test_multiple_items() -> None:
"""Test that a string with multiple comma-separated items is parsed to a list."""
parser = CommaSeparatedListOutputParser()
text = "foo, bar, baz"
text = "foo,bar,baz"
expected = ["foo", "bar", "baz"]

assert parser.parse(text) == expected
Expand Down

0 comments on commit 77774c0

Please sign in to comment.