Home / File/ regex.py — langchain Source File

regex.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  ab3b196d_7ab2_81f1_0c38_feb56461e3e0["regex.py"]
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  ab3b196d_7ab2_81f1_0c38_feb56461e3e0 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  ab3b196d_7ab2_81f1_0c38_feb56461e3e0 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  ab3b196d_7ab2_81f1_0c38_feb56461e3e0 --> 91721f45_4909_e489_8c1f_084f8bd87145
  style ab3b196d_7ab2_81f1_0c38_feb56461e3e0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

import re

from langchain_core.output_parsers import BaseOutputParser
from typing_extensions import override


class RegexParser(BaseOutputParser[dict[str, str]]):
    """Parse the output of an LLM call using a regex."""

    @classmethod
    @override
    def is_lc_serializable(cls) -> bool:
        return True

    regex: str
    """The regex to use to parse the output."""
    output_keys: list[str]
    """The keys to use for the output."""
    default_output_key: str | None = None
    """The default key to use for the output."""

    @property
    def _type(self) -> str:
        """Return the type key."""
        return "regex_parser"

    def parse(self, text: str) -> dict[str, str]:
        """Parse the output of an LLM call."""
        match = re.search(self.regex, text)
        if match:
            return {key: match.group(i + 1) for i, key in enumerate(self.output_keys)}
        if self.default_output_key is None:
            msg = f"Could not parse output: {text}"
            raise ValueError(msg)
        return {
            key: text if key == self.default_output_key else ""
            for key in self.output_keys
        }

Domain

Subdomains

Classes

Dependencies

  • langchain_core.output_parsers
  • re
  • typing_extensions

Frequently Asked Questions

What does regex.py do?
regex.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What does regex.py depend on?
regex.py imports 3 module(s): langchain_core.output_parsers, re, typing_extensions.
Where is regex.py in the architecture?
regex.py is located at libs/langchain/langchain_classic/output_parsers/regex.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