mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-01 10:54:15 +00:00
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
This commit is contained in:
parent
3b08617a89
commit
5afb242161
@ -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…
Reference in New Issue
Block a user