{"record":{"id":"2e8da0339e81d429","repo":"langchain-ai/langchain","slug":"booleanoutputparser-expected-output-value-to-eithe","errorCode":null,"errorMessage":"BooleanOutputParser expected output value to either be {self.true_val} or {self.false_val} (case-insensitive). Received {cleaned_text}.","messagePattern":"BooleanOutputParser expected output value to either be (.+?) or (.+?) \\(case-insensitive\\)\\. Received (.+?)\\.","errorType":"exception","errorClass":"OutputParserException","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/output_parsers/base.py","lineNumber":162,"sourceCode":"\n    Output parsers help structure language model responses.\n\n    Example:\n        ```python\n        # Implement a simple boolean output parser\n\n\n        class BooleanOutputParser(BaseOutputParser[bool]):\n            true_val: str = \"YES\"\n            false_val: str = \"NO\"\n\n            def parse(self, text: str) -> bool:\n                cleaned_text = text.strip().upper()\n                if cleaned_text not in (\n                    self.true_val.upper(),\n                    self.false_val.upper(),\n                ):\n                    raise OutputParserException(\n                        f\"BooleanOutputParser expected output value to either be \"\n                        f\"{self.true_val} or {self.false_val} (case-insensitive). \"\n                        f\"Received {cleaned_text}.\"\n                    )\n                return cleaned_text == self.true_val.upper()\n\n            @property\n            def _type(self) -> str:\n                return \"boolean_output_parser\"\n        ```\n    \"\"\"\n\n    @property\n    @override\n    def InputType(self) -> Any:\n        \"\"\"Return the input type for the parser.\"\"\"\n        return str | AnyMessage\n","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/output_parsers/base.py#L144-L180","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the prompt: explicitly instruct 'Answer with exactly YES or NO and nothing else' and include matching few-shot examples.","Use `llm.with_structured_output(bool)` or a `JsonOutputParser` with a boolean schema instead of free-text parsing.","Configure `true_val`/`false_val` if your model emits different tokens (e.g. `\"TRUE\"`/`\"FALSE\"`).","Catch OutputParserException and retry the call with an escalated 'answer only YES or NO' instruction."],"exampleFix":"// before\nprompt = \"Is {question} true?\"\nchain = prompt | llm | BooleanOutputParser()\n\n// after\nprompt = \"Is {question} true? Answer with exactly one word: YES or NO.\"\nchain = prompt | llm | BooleanOutputParser()\n// or structured:\nchain = prompt | llm.with_structured_output(bool)","handlingStrategy":"try-catch","validationCode":"cleaned = text.strip().upper()\nif cleaned not in (\"YES\", \"NO\"):\n    # route to repair/retry before the parser throws\n    text = repair_yes_no(text)  # e.g. map TRUE/FALSE, or re-ask the model","typeGuard":null,"tryCatchPattern":"from langchain_core.exceptions import OutputParserException\n\nfor attempt in range(2):\n    out = chain.invoke({\"question\": q})\n    try:\n        return bool_parser.parse(out)\n    except OutputParserException:\n        if attempt == 1:\n            raise\n        out = (llm | StrOutputParser()).invoke(f\"Answer only YES or NO: is this true? {out!r}\")","preventionTips":["State the exact allowed tokens in the prompt and show examples","Prefer with_structured_output(bool) for new code","Set true_val/false_val if your model's vocabulary differs"],"tags":["output-parsers","boolean","prompt-engineering","llm-output"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}