Home / Function/ configurable_alternatives() — langchain Function Reference

configurable_alternatives() — langchain Function Reference

Architecture documentation for the configurable_alternatives() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6ac29666_a427_452b_b23e_cf2d69be1771["configurable_alternatives()"]
  163fcac0_ddd4_a382_5a54_90fc39911022["RunnableSerializable"]
  6ac29666_a427_452b_b23e_cf2d69be1771 -->|defined in| 163fcac0_ddd4_a382_5a54_90fc39911022
  e2bea970_c5e9_9585_6fa1_e85a5a79ce5e["with_config()"]
  6ac29666_a427_452b_b23e_cf2d69be1771 -->|calls| e2bea970_c5e9_9585_6fa1_e85a5a79ce5e
  style 6ac29666_a427_452b_b23e_cf2d69be1771 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/base.py lines 2672–2730

    def configurable_alternatives(
        self,
        which: ConfigurableField,
        *,
        default_key: str = "default",
        prefix_keys: bool = False,
        **kwargs: Runnable[Input, Output] | Callable[[], Runnable[Input, Output]],
    ) -> RunnableSerializable[Input, Output]:
        """Configure alternatives for `Runnable` objects that can be set at runtime.

        Args:
            which: The `ConfigurableField` instance that will be used to select the
                alternative.
            default_key: The default key to use if no alternative is selected.
            prefix_keys: Whether to prefix the keys with the `ConfigurableField` id.
            **kwargs: A dictionary of keys to `Runnable` instances or callables that
                return `Runnable` instances.

        Returns:
            A new `Runnable` with the alternatives configured.

        !!! example

            ```python
            from langchain_anthropic import ChatAnthropic
            from langchain_core.runnables.utils import ConfigurableField
            from langchain_openai import ChatOpenAI

            model = ChatAnthropic(
                model_name="claude-sonnet-4-5-20250929"
            ).configurable_alternatives(
                ConfigurableField(id="llm"),
                default_key="anthropic",
                openai=ChatOpenAI(),
            )

            # uses the default model ChatAnthropic
            print(model.invoke("which organization created you?").content)

            # uses ChatOpenAI
            print(
                model.with_config(configurable={"llm": "openai"})
                .invoke("which organization created you?")
                .content
            )
            ```
        """
        # Import locally to prevent circular import
        from langchain_core.runnables.configurable import (  # noqa: PLC0415
            RunnableConfigurableAlternatives,
        )

        return RunnableConfigurableAlternatives(
            which=which,
            default=self,
            alternatives=kwargs,
            default_key=default_key,
            prefix_keys=prefix_keys,
        )

Domain

Subdomains

Frequently Asked Questions

What does configurable_alternatives() do?
configurable_alternatives() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is configurable_alternatives() defined?
configurable_alternatives() is defined in libs/core/langchain_core/runnables/base.py at line 2672.
What does configurable_alternatives() call?
configurable_alternatives() calls 1 function(s): with_config.

Analyze Your Own Codebase

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

Try Supermodel Free