langchain[patch]: Make BooleanOutputParser more robust to non-binary responses (#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:**  #11408 
- **Dependencies:** No
- **Twitter handle:** dokatox
pull/18107/head
dokato 4 months ago committed by GitHub
parent 3b08617a89
commit 5afb242161
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -19,13 +19,24 @@ class BooleanOutputParser(BaseOutputParser[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:

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

Loading…
Cancel
Save