Home / File/ combining.py — langchain Source File

combining.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  012baf05_a3c2_4ba2_f15a_5d6471c083bf["combining.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  012baf05_a3c2_4ba2_f15a_5d6471c083bf --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  012baf05_a3c2_4ba2_f15a_5d6471c083bf --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  f4d905c6_a2b2_eb8f_be9b_7808b72f6a16["langchain_core.utils"]
  012baf05_a3c2_4ba2_f15a_5d6471c083bf --> f4d905c6_a2b2_eb8f_be9b_7808b72f6a16
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  012baf05_a3c2_4ba2_f15a_5d6471c083bf --> 91721f45_4909_e489_8c1f_084f8bd87145
  style 012baf05_a3c2_4ba2_f15a_5d6471c083bf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from typing import Any

from langchain_core.output_parsers import BaseOutputParser
from langchain_core.utils import pre_init
from typing_extensions import override

_MIN_PARSERS = 2


class CombiningOutputParser(BaseOutputParser[dict[str, Any]]):
    """Combine multiple output parsers into one."""

    parsers: list[BaseOutputParser]

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

    @pre_init
    def validate_parsers(cls, values: dict[str, Any]) -> dict[str, Any]:
        """Validate the parsers."""
        parsers = values["parsers"]
        if len(parsers) < _MIN_PARSERS:
            msg = "Must have at least two parsers"
            raise ValueError(msg)
        for parser in parsers:
            if parser._type == "combining":  # noqa: SLF001
                msg = "Cannot nest combining parsers"
                raise ValueError(msg)
            if parser._type == "list":  # noqa: SLF001
                msg = "Cannot combine list parsers"
                raise ValueError(msg)
        return values

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

    def get_format_instructions(self) -> str:
        """Instructions on how the LLM output should be formatted."""
        initial = f"For your first output: {self.parsers[0].get_format_instructions()}"
        subsequent = "\n".join(
            f"Complete that output fully. Then produce another output, separated by two newline characters: {p.get_format_instructions()}"  # noqa: E501
            for p in self.parsers[1:]
        )
        return f"{initial}\n{subsequent}"

    def parse(self, text: str) -> dict[str, Any]:
        """Parse the output of an LLM call."""
        texts = text.split("\n\n")
        output = {}
        for txt, parser in zip(texts, self.parsers, strict=False):
            output.update(parser.parse(txt.strip()))
        return output

Domain

Subdomains

Dependencies

  • langchain_core.output_parsers
  • langchain_core.utils
  • typing
  • typing_extensions

Frequently Asked Questions

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