Home / File/ fallbacks.py — langchain Source File

fallbacks.py — langchain Source File

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

File python CoreAbstractions Serialization 11 imports 3 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  e5174863_7a16_0a1b_3df0_7385f9898aa9["fallbacks.py"]
  a327e534_84f6_5308_58ca_5727d5eda0cf["asyncio"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> a327e534_84f6_5308_58ca_5727d5eda0cf
  614e7b9f_ed51_0780_749c_ff40b74963fc["inspect"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> 614e7b9f_ed51_0780_749c_ff40b74963fc
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  c990f2d7_9509_7cea_ca95_51ad57dbe5c6["functools"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> c990f2d7_9509_7cea_ca95_51ad57dbe5c6
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> 91721f45_4909_e489_8c1f_084f8bd87145
  e8ec017e_6c91_4b34_675f_2a96c5aa9be6["langchain_core.callbacks.manager"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> e8ec017e_6c91_4b34_675f_2a96c5aa9be6
  c764ccae_0d75_abec_7c23_6d5d1949a7ba["langchain_core.runnables.base"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> c764ccae_0d75_abec_7c23_6d5d1949a7ba
  2971f9da_6393_a3e3_610e_ace3d35ee978["langchain_core.runnables.config"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> 2971f9da_6393_a3e3_610e_ace3d35ee978
  81c04601_d095_a27d_4af1_55e771bb2b6b["langchain_core.runnables.utils"]
  e5174863_7a16_0a1b_3df0_7385f9898aa9 --> 81c04601_d095_a27d_4af1_55e771bb2b6b
  style e5174863_7a16_0a1b_3df0_7385f9898aa9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""`Runnable` that can fallback to other `Runnable` objects if it fails."""

import asyncio
import inspect
import typing
from collections.abc import AsyncIterator, Iterator, Sequence
from functools import wraps
from typing import TYPE_CHECKING, Any, cast

from pydantic import BaseModel, ConfigDict
from typing_extensions import override

from langchain_core.callbacks.manager import AsyncCallbackManager, CallbackManager
from langchain_core.runnables.base import Runnable, RunnableSerializable
from langchain_core.runnables.config import (
    RunnableConfig,
    ensure_config,
    get_async_callback_manager_for_config,
    get_callback_manager_for_config,
    get_config_list,
    patch_config,
    set_config_context,
)
from langchain_core.runnables.utils import (
    ConfigurableFieldSpec,
    Input,
    Output,
    coro_with_context,
    get_unique_config_specs,
)

if TYPE_CHECKING:
    from langchain_core.callbacks.manager import AsyncCallbackManagerForChainRun


class RunnableWithFallbacks(RunnableSerializable[Input, Output]):
    """`Runnable` that can fallback to other `Runnable` objects if it fails.

    External APIs (e.g., APIs for a language model) may at times experience
    degraded performance or even downtime.

    In these cases, it can be useful to have a fallback `Runnable` that can be
    used in place of the original `Runnable` (e.g., fallback to another LLM provider).

    Fallbacks can be defined at the level of a single `Runnable`, or at the level
    of a chain of `Runnable`s. Fallbacks are tried in order until one succeeds or
    all fail.

    While you can instantiate a `RunnableWithFallbacks` directly, it is usually
    more convenient to use the `with_fallbacks` method on a `Runnable`.

    Example:
        ```python
        from langchain_core.chat_models.openai import ChatOpenAI
        from langchain_core.chat_models.anthropic import ChatAnthropic

        model = ChatAnthropic(model="claude-3-haiku-20240307").with_fallbacks(
            [ChatOpenAI(model="gpt-3.5-turbo-0125")]
        )
        # Will usually use ChatAnthropic, but fallback to ChatOpenAI
// ... (605 more lines)

Subdomains

Dependencies

  • asyncio
  • collections.abc
  • functools
  • inspect
  • langchain_core.callbacks.manager
  • langchain_core.runnables.base
  • langchain_core.runnables.config
  • langchain_core.runnables.utils
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does fallbacks.py do?
fallbacks.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in fallbacks.py?
fallbacks.py defines 3 function(s): _is_runnable_type, _returns_runnable, langchain_core.
What does fallbacks.py depend on?
fallbacks.py imports 11 module(s): asyncio, collections.abc, functools, inspect, langchain_core.callbacks.manager, langchain_core.runnables.base, langchain_core.runnables.config, langchain_core.runnables.utils, and 3 more.
Where is fallbacks.py in the architecture?
fallbacks.py is located at libs/core/langchain_core/runnables/fallbacks.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/runnables).

Analyze Your Own Codebase

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

Try Supermodel Free