Home / File/ regex_dict.py — langchain Source File

regex_dict.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  2390004a_5769_aa9c_c2a1_1285be90b9d4["regex_dict.py"]
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  2390004a_5769_aa9c_c2a1_1285be90b9d4 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  2390004a_5769_aa9c_c2a1_1285be90b9d4 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  style 2390004a_5769_aa9c_c2a1_1285be90b9d4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

import re

from langchain_core.output_parsers import BaseOutputParser


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

    regex_pattern: str = r"{}:\s?([^.'\n']*)\.?"
    """The regex pattern to use to parse the output."""
    output_key_to_format: dict[str, str]
    """The keys to use for the output."""
    no_update_value: str | None = None
    """The default key to use for the output."""

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

    def parse(self, text: str) -> dict[str, str]:
        """Parse the output of an LLM call."""
        result = {}
        for output_key, expected_format in self.output_key_to_format.items():
            specific_regex = self.regex_pattern.format(re.escape(expected_format))
            matches = re.findall(specific_regex, text)
            if not matches:
                msg = (
                    f"No match found for output key: {output_key} with expected format \
                        {expected_format} on text {text}"
                )
                raise ValueError(msg)
            if len(matches) > 1:
                msg = f"Multiple matches found for output key: {output_key} with \
                        expected format {expected_format} on text {text}"
                raise ValueError(msg)
            if self.no_update_value is not None and matches[0] == self.no_update_value:
                continue
            result[output_key] = matches[0]
        return result

Domain

Subdomains

Classes

Dependencies

  • langchain_core.output_parsers
  • re

Frequently Asked Questions

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