core[patch]: improve comma separated list output parser to handle non-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.
-->
pull/20269/head^2
Anish Chakraborty 3 weeks ago committed by GitHub
parent 63a07f52df
commit 898362de81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -115,12 +115,12 @@ class CommaSeparatedListOutputParser(ListOutputParser):
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:

@ -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

Loading…
Cancel
Save