Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain: Make BooleanOutputParser more robust to non-binary responses #17810

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 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,16 @@ 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:
dokato marked this conversation as resolved.
Show resolved Hide resolved
return True
elif self.false_val.upper() in cleaned_upper_text:
return False
else:
raise ValueError(
f"BooleanOutputParser expected output value to either be "
f"{self.true_val} or {self.false_val}. Received {cleaned_text}."
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