langchain-ai/langchain · error · OutputParserException

BooleanOutputParser expected output value to either be {self

Error message

BooleanOutputParser expected output value to either be {self.true_val} or {self.false_val} (case-insensitive). Received {cleaned_text}.

What it means

`BooleanOutputParser.parse` uppercases and strips the LLM output, then requires it to equal `self.true_val` (`YES` by default) or `self.false_val` (`NO` by default), case-insensitively. Anything else — explanations, punctuation, other yes/no words — raises OutputParserException. The parser is deliberately strict because a lenient guess would silently mislabel boolean results downstream.

Source

Thrown at libs/core/langchain_core/output_parsers/base.py:162

    Output parsers help structure language model responses.

    Example:
        ```python
        # Implement a simple boolean output parser


        class BooleanOutputParser(BaseOutputParser[bool]):
            true_val: str = "YES"
            false_val: str = "NO"

            def parse(self, text: str) -> bool:
                cleaned_text = text.strip().upper()
                if cleaned_text not in (
                    self.true_val.upper(),
                    self.false_val.upper(),
                ):
                    raise OutputParserException(
                        f"BooleanOutputParser expected output value to either be "
                        f"{self.true_val} or {self.false_val} (case-insensitive). "
                        f"Received {cleaned_text}."
                    )
                return cleaned_text == self.true_val.upper()

            @property
            def _type(self) -> str:
                return "boolean_output_parser"
        ```
    """

    @property
    @override
    def InputType(self) -> Any:
        """Return the input type for the parser."""
        return str | AnyMessage

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Fix the prompt: explicitly instruct 'Answer with exactly YES or NO and nothing else' and include matching few-shot examples.
  2. Use `llm.with_structured_output(bool)` or a `JsonOutputParser` with a boolean schema instead of free-text parsing.
  3. Configure `true_val`/`false_val` if your model emits different tokens (e.g. `"TRUE"`/`"FALSE"`).
  4. Catch OutputParserException and retry the call with an escalated 'answer only YES or NO' instruction.

Example fix

// before
prompt = "Is {question} true?"
chain = prompt | llm | BooleanOutputParser()

// after
prompt = "Is {question} true? Answer with exactly one word: YES or NO."
chain = prompt | llm | BooleanOutputParser()
// or structured:
chain = prompt | llm.with_structured_output(bool)
Defensive patterns

Strategy: try-catch

Validate before calling

cleaned = text.strip().upper()
if cleaned not in ("YES", "NO"):
    # route to repair/retry before the parser throws
    text = repair_yes_no(text)  # e.g. map TRUE/FALSE, or re-ask the model

Try / catch

from langchain_core.exceptions import OutputParserException

for attempt in range(2):
    out = chain.invoke({"question": q})
    try:
        return bool_parser.parse(out)
    except OutputParserException:
        if attempt == 1:
            raise
        out = (llm | StrOutputParser()).invoke(f"Answer only YES or NO: is this true? {out!r}")

Prevention

When it happens

Trigger: Model replies `"Yes."`, `"yes, because..."`, `"TRUE"`, `"Y"`, or a whole sentence when the chain expects exactly YES/NO; non-English models answering in their output language; chains where the prompt failed to constrain the output format.

Common situations: Using BooleanOutputParser without a prompt that forces constrained output; switching models to one that ignores format instructions; few-shot examples that don't demonstrate the exact YES/NO format.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/2e8da0339e81d429. Report an issue: GitHub.