Home / File/ base.py — langchain Source File

base.py — langchain Source File

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

File python OutputParsing StreamingParsers 10 imports 1 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  92e0b9ea_b844_d40b_6560_e9c0b74a9278["base.py"]
  69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56["contextlib"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> 69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56
  cccbe73e_4644_7211_4d55_e8fb133a8014["abc"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> cccbe73e_4644_7211_4d55_e8fb133a8014
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> 91721f45_4909_e489_8c1f_084f8bd87145
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> d758344f_537f_649e_f467_b9d7442e86df
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  2971f9da_6393_a3e3_610e_ace3d35ee978["langchain_core.runnables.config"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> 2971f9da_6393_a3e3_610e_ace3d35ee978
  5b417886_56dd_6afa_13ab_a3cfc1dbcccd["langchain_core.prompt_values"]
  92e0b9ea_b844_d40b_6560_e9c0b74a9278 --> 5b417886_56dd_6afa_13ab_a3cfc1dbcccd
  style 92e0b9ea_b844_d40b_6560_e9c0b74a9278 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Base parser for language model outputs."""

from __future__ import annotations

import contextlib
from abc import ABC, abstractmethod
from typing import (
    TYPE_CHECKING,
    Any,
    Generic,
    TypeVar,
    cast,
)

from typing_extensions import override

from langchain_core.language_models import LanguageModelOutput
from langchain_core.messages import AnyMessage, BaseMessage
from langchain_core.outputs import ChatGeneration, Generation
from langchain_core.runnables import Runnable, RunnableConfig, RunnableSerializable
from langchain_core.runnables.config import run_in_executor

if TYPE_CHECKING:
    from langchain_core.prompt_values import PromptValue

T = TypeVar("T")
OutputParserLike = Runnable[LanguageModelOutput, T]


class BaseLLMOutputParser(ABC, Generic[T]):
    """Abstract base class for parsing the outputs of a model."""

    @abstractmethod
    def parse_result(self, result: list[Generation], *, partial: bool = False) -> T:
        """Parse a list of candidate model `Generation` objects into a specific format.

        Args:
            result: A list of `Generation` to be parsed.

                The `Generation` objects are assumed to be different candidate outputs
                for a single model input.
            partial: Whether to parse the output as a partial result.

                This is useful for parsers that can parse partial results.

        Returns:
            Structured output.
        """

    async def aparse_result(
        self, result: list[Generation], *, partial: bool = False
    ) -> T:
        """Parse a list of candidate model `Generation` objects into a specific format.

        Args:
            result: A list of `Generation` to be parsed.

                The Generations are assumed to be different candidate outputs for a
                single model input.
            partial: Whether to parse the output as a partial result.
// ... (289 more lines)

Domain

Subdomains

Functions

Dependencies

  • abc
  • contextlib
  • langchain_core.language_models
  • langchain_core.messages
  • langchain_core.outputs
  • langchain_core.prompt_values
  • langchain_core.runnables
  • langchain_core.runnables.config
  • typing
  • typing_extensions

Frequently Asked Questions

What does base.py do?
base.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What functions are defined in base.py?
base.py defines 1 function(s): langchain_core.
What does base.py depend on?
base.py imports 10 module(s): abc, contextlib, langchain_core.language_models, langchain_core.messages, langchain_core.outputs, langchain_core.prompt_values, langchain_core.runnables, langchain_core.runnables.config, and 2 more.
Where is base.py in the architecture?
base.py is located at libs/core/langchain_core/output_parsers/base.py (domain: OutputParsing, subdomain: StreamingParsers, directory: libs/core/langchain_core/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free