Runnable that can be dynamically configured.
A RunnableConfigurableAlternatives should be initiated using the
configurable_alternatives method of a Runnable or can be
initiated directly as well.
Here is an example of using a RunnableConfigurableAlternatives that uses
alternative prompts to illustrate its functionality:
from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI
# This creates a RunnableConfigurableAlternatives for Prompt Runnable
# with two alternatives.
prompt = PromptTemplate.from_template(
"Tell me a joke about {topic}"
).configurable_alternatives(
ConfigurableField(id="prompt"),
default_key="joke",
poem=PromptTemplate.from_template("Write a short poem about {topic}"),
)
# When invoking the created RunnableSequence, you can pass in the
# value for your ConfigurableField's id which in this case will either be
# `joke` or `poem`.
chain = prompt | ChatOpenAI(model="gpt-5.4-mini")
# The `with_config` method brings in the desired Prompt Runnable in your
# Runnable Sequence.
chain.with_config(configurable={"prompt": "poem"}).invoke({"topic": "bears"})
Equivalently, you can initialize RunnableConfigurableAlternatives directly
and use in LCEL in the same way:
from langchain_core.runnables import ConfigurableField
from langchain_core.runnables.configurable import (
RunnableConfigurableAlternatives,
)
from langchain_openai import ChatOpenAI
prompt = RunnableConfigurableAlternatives(
which=ConfigurableField(id="prompt"),
default=PromptTemplate.from_template("Tell me a joke about {topic}"),
default_key="joke",
prefix_keys=False,
alternatives={
"poem": PromptTemplate.from_template("Write a short poem about {topic}")
},
)
chain = prompt | ChatOpenAI(model="gpt-5.4-mini")
chain.with_config(configurable={"prompt": "poem"}).invoke({"topic": "bears"})The ConfigurableField to use to choose between alternatives.
The alternatives to choose from.
The enum value to use for the default option.
Whether to prefix configurable fields of each alternative with a namespace of the form <which.id>==<alternative_key>, e.g. a key named "temperature" used by the alternative named "gpt3" becomes "model==gpt3/temperature".
Get the name of the Runnable.
Get a Pydantic model that can be used to validate input to the Runnable.
Get a JSON schema that represents the input to the Runnable.
Get a Pydantic model that can be used to validate output to the Runnable.
Get a JSON schema that represents the output of the Runnable.
The type of config this Runnable accepts specified as a Pydantic model.
Get a JSON schema that represents the config of the Runnable.
Return a graph representation of this Runnable.
Return a list of prompts used by this Runnable.
Pipe Runnable objects.
Pick keys from the output dict of this Runnable.
Assigns new fields to the dict output of this Runnable.
Transform a single input into an output.
Transform a single input into an output.
Default implementation runs invoke in parallel using a thread pool executor.
Run invoke in parallel on a list of inputs.
Default implementation runs ainvoke in parallel using asyncio.gather.
Run ainvoke in parallel on a list of inputs.
Default implementation of stream, which calls invoke.
Default implementation of astream, which calls ainvoke.
Stream all output from a Runnable, as reported to the callback system.
Generate a stream of events.
Transform inputs to outputs.
Transform inputs to outputs.
Bind arguments to a Runnable, returning a new Runnable.
Bind config to a Runnable, returning a new Runnable.
Bind lifecycle listeners to a Runnable, returning a new Runnable.
Bind async lifecycle listeners to a Runnable.
Bind input and output types to a Runnable, returning a new Runnable.
Create a new Runnable that retries the original Runnable on exceptions.
Return a new Runnable that maps a list of inputs to a list of outputs.
Add fallbacks to a Runnable, returning a new Runnable.
Create a BaseTool from a Runnable.