Home / File/ boolean.py — langchain Source File

boolean.py — langchain Source File

Architecture documentation for boolean.py, a python file in the langchain codebase. 2 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  27db5a2b_caca_e6cb_92f1_a74e1316573e["boolean.py"]
  b7996424_637b_0b54_6edf_2e18e9c1a8bf["re"]
  27db5a2b_caca_e6cb_92f1_a74e1316573e --> b7996424_637b_0b54_6edf_2e18e9c1a8bf
  628cbc5d_711f_ac0c_2f53_db992d48d7da["langchain_core.output_parsers"]
  27db5a2b_caca_e6cb_92f1_a74e1316573e --> 628cbc5d_711f_ac0c_2f53_db992d48d7da
  style 27db5a2b_caca_e6cb_92f1_a74e1316573e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import re

from langchain_core.output_parsers import BaseOutputParser


class BooleanOutputParser(BaseOutputParser[bool]):
    """Parse the output of an LLM call to a boolean."""

    true_val: str = "YES"
    """The string value that should be parsed as True."""
    false_val: str = "NO"
    """The string value that should be parsed as False."""

    def parse(self, text: str) -> bool:
        """Parse the output of an LLM call to a boolean.

        Args:
            text: output of a language model

        Returns:
            boolean
        """
        regexp = rf"\b({self.true_val}|{self.false_val})\b"

        truthy = {
            val.upper()
            for val in re.findall(regexp, text, flags=re.IGNORECASE | re.MULTILINE)
        }
        if self.true_val.upper() in truthy:
            if self.false_val.upper() in truthy:
                msg = (
                    f"Ambiguous response. Both {self.true_val} and {self.false_val} "
                    f"in received: {text}."
                )
                raise ValueError(msg)
            return True
        if self.false_val.upper() in truthy:
            if self.true_val.upper() in truthy:
                msg = (
                    f"Ambiguous response. Both {self.true_val} and {self.false_val} "
                    f"in received: {text}."
                )
                raise ValueError(msg)
            return False
        msg = (
            f"BooleanOutputParser expected output value to include either "
            f"{self.true_val} or {self.false_val}. Received {text}."
        )
        raise ValueError(msg)

    @property
    def _type(self) -> str:
        """Snake-case string identifier for an output parser type."""
        return "boolean_output_parser"

Domain

Subdomains

Dependencies

  • langchain_core.output_parsers
  • re

Frequently Asked Questions

What does boolean.py do?
boolean.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What does boolean.py depend on?
boolean.py imports 2 module(s): langchain_core.output_parsers, re.
Where is boolean.py in the architecture?
boolean.py is located at libs/langchain/langchain_classic/output_parsers/boolean.py (domain: OutputParsing, subdomain: StreamingParsers, directory: libs/langchain/langchain_classic/output_parsers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free