mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-17 07:26:16 +00:00
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. -->
This commit is contained in:
committed by
GitHub
parent
63a07f52df
commit
898362de81
@@ -115,12 +115,12 @@ class CommaSeparatedListOutputParser(ListOutputParser):
|
|||||||
def get_format_instructions(self) -> str:
|
def get_format_instructions(self) -> str:
|
||||||
return (
|
return (
|
||||||
"Your response should be a list of comma separated values, "
|
"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]:
|
def parse(self, text: str) -> List[str]:
|
||||||
"""Parse the output of an LLM call."""
|
"""Parse the output of an LLM call."""
|
||||||
return text.strip().split(", ")
|
return [part.strip() for part in text.split(",")]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _type(self) -> str:
|
def _type(self) -> str:
|
||||||
|
@@ -26,10 +26,29 @@ def test_single_item() -> None:
|
|||||||
assert list(parser.transform(iter([text]))) == [[a] for a in expected]
|
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:
|
def test_multiple_items() -> None:
|
||||||
"""Test that a string with multiple comma-separated items is parsed to a list."""
|
"""Test that a string with multiple comma-separated items is parsed to a list."""
|
||||||
parser = CommaSeparatedListOutputParser()
|
parser = CommaSeparatedListOutputParser()
|
||||||
text = "foo, bar, baz"
|
text = "foo,bar,baz"
|
||||||
expected = ["foo", "bar", "baz"]
|
expected = ["foo", "bar", "baz"]
|
||||||
|
|
||||||
assert parser.parse(text) == expected
|
assert parser.parse(text) == expected
|
||||||
|
Reference in New Issue
Block a user