Skip to content

Commit

Permalink
langchain[patch]: Make BooleanOutputParser more robust to non-binary …
Browse files Browse the repository at this point in the history
…responses (langchain-ai#17810)

- **Description:** I encountered this error when I tried to use
LLMChainFilter. Even if the message slightly differs, like `Not relevant
(NO)` this results in an error. It has been reported already here:
https://github.com/langchain-ai/langchain/issues/. This change hopefully
makes it more robust.
- **Issue:**  langchain-ai#11408 
- **Dependencies:** No
- **Twitter handle:** dokatox
  • Loading branch information
dokato authored and al1p-R committed Feb 27, 2024
1 parent f3a2e8b commit 5dcafe9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
21 changes: 16 additions & 5 deletions libs/langchain/langchain/output_parsers/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ def parse(self, text: str) -> bool:
boolean
"""
cleaned_text = text.strip()
if cleaned_text.upper() not in (self.true_val.upper(), self.false_val.upper()):
cleaned_upper_text = text.strip().upper()
if (
self.true_val.upper() in cleaned_upper_text
and self.false_val.upper() in cleaned_upper_text
):
raise ValueError(
f"BooleanOutputParser expected output value to either be "
f"{self.true_val} or {self.false_val}. Received {cleaned_text}."
f"Ambiguous response. Both {self.true_val} and {self.false_val} in "
f"received: {text}."
)
elif self.true_val.upper() in cleaned_upper_text:
return True
elif self.false_val.upper() in cleaned_upper_text:
return False
else:
raise ValueError(
f"BooleanOutputParser expected output value to include either "
f"{self.true_val} or {self.false_val}. Received {text}."
)
return cleaned_text.upper() == self.true_val.upper()

@property
def _type(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def test_boolean_output_parser_parse() -> None:
result = parser.parse("no")
assert result is False

# Test valid input
result = parser.parse("Not relevant (NO)")
assert result is False

# Test ambiguous input
try:
parser.parse("yes and no")
assert False, "Should have raised ValueError"
except ValueError:
pass

# Test invalid input
try:
parser.parse("INVALID")
Expand Down

0 comments on commit 5dcafe9

Please sign in to comment.