RunnableConfigurableAlternatives Class — langchain Architecture
Architecture documentation for the RunnableConfigurableAlternatives class in configurable.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 7a487f57_8242_5050_16bd_d495e3d892c6["RunnableConfigurableAlternatives"] cf4a5dd2_0ab2_70a5_ef48_4a834231c413["RunnableSerializable"] 7a487f57_8242_5050_16bd_d495e3d892c6 -->|extends| cf4a5dd2_0ab2_70a5_ef48_4a834231c413 4022a4a7_2f12_918e_95f7_87d452c5dc9e["Runnable"] 7a487f57_8242_5050_16bd_d495e3d892c6 -->|extends| 4022a4a7_2f12_918e_95f7_87d452c5dc9e 3e4f3163_58be_ee5e_f841_ae0bd0577190["configurable.py"] 7a487f57_8242_5050_16bd_d495e3d892c6 -->|defined in| 3e4f3163_58be_ee5e_f841_ae0bd0577190 573172fd_cfaa_25dc_85e0_93df3c28864d["config_specs()"] 7a487f57_8242_5050_16bd_d495e3d892c6 -->|method| 573172fd_cfaa_25dc_85e0_93df3c28864d cfe595a2_177b_7103_fb5c_7a8ab45b9051["configurable_fields()"] 7a487f57_8242_5050_16bd_d495e3d892c6 -->|method| cfe595a2_177b_7103_fb5c_7a8ab45b9051 c12a4697_205a_5c93_6ef5_bbaf7294df27["_prepare()"] 7a487f57_8242_5050_16bd_d495e3d892c6 -->|method| c12a4697_205a_5c93_6ef5_bbaf7294df27
Relationship Graph
Source Code
libs/core/langchain_core/runnables/configurable.py lines 475–638
class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
"""`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:
```python
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-3.5-turbo-0125", temperature=0)
# 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:
```python
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-3.5-turbo-0125", temperature=0)
chain.with_config(configurable={"prompt": "poem"}).invoke({"topic": "bears"})
```
"""
which: ConfigurableField
"""The `ConfigurableField` to use to choose between alternatives."""
alternatives: dict[
str,
Runnable[Input, Output] | Callable[[], Runnable[Input, Output]],
]
"""The alternatives to choose from."""
default_key: str = "default"
"""The enum value to use for the default option."""
prefix_keys: bool
"""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".
"""
@property
@override
def config_specs(self) -> list[ConfigurableFieldSpec]:
with _enums_for_spec_lock:
if which_enum := _enums_for_spec.get(self.which):
Extends
Source
Frequently Asked Questions
What is the RunnableConfigurableAlternatives class?
RunnableConfigurableAlternatives is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/configurable.py.
Where is RunnableConfigurableAlternatives defined?
RunnableConfigurableAlternatives is defined in libs/core/langchain_core/runnables/configurable.py at line 475.
What does RunnableConfigurableAlternatives extend?
RunnableConfigurableAlternatives extends RunnableSerializable, Runnable.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free