Skip to content

The agent engineering platform.

Ensure that configurable fields with enums support deduplication

Nuno Camposcf448a63144b
diff --git a/libs/langchain/langchain/schema/runnable/configurable.py b/libs/langchain/langchain/schema/runnable/configurable.py
index 7c246e6e01..bf45e4c004 100644
--- a/libs/langchain/langchain/schema/runnable/configurable.py
+++ b/libs/langchain/langchain/schema/runnable/configurable.py
@@ -14,6 +14,7 @@ from typing import (
Union,
cast,
)
+from weakref import WeakValueDictionary
from langchain.pydantic_v1 import BaseModel
from langchain.schema.runnable.base import Runnable, RunnableSerializable
@@ -262,6 +263,14 @@ class StrEnum(str, enum.Enum):
pass
+_enums_for_spec: WeakValueDictionary[
+ Union[
+ ConfigurableFieldSingleOption, ConfigurableFieldMultiOption, ConfigurableField
+ ],
+ Type[StrEnum],
+] = WeakValueDictionary()
+
+
class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
which: ConfigurableField
@@ -271,10 +280,14 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
@property
def config_specs(self) -> Sequence[ConfigurableFieldSpec]:
- which_enum = StrEnum( # type: ignore[call-overload]
- self.which.name or self.which.id,
- ((v, v) for v in list(self.alternatives.keys()) + [self.default_key]),
- )
+ if which_enum := _enums_for_spec.get(self.which):
+ pass
+ else:
+ which_enum = StrEnum( # type: ignore[call-overload]
+ self.which.name or self.which.id,
+ ((v, v) for v in list(self.alternatives.keys()) + [self.default_key]),
+ )
+ _enums_for_spec[self.which] = cast(Type[StrEnum], which_enum)
return [
ConfigurableFieldSpec(
id=self.which.id,
@@ -312,10 +325,14 @@ def make_options_spec(
spec: Union[ConfigurableFieldSingleOption, ConfigurableFieldMultiOption],
description: Optional[str],
) -> ConfigurableFieldSpec:
- enum = StrEnum( # type: ignore[call-overload]
- spec.name or spec.id,
- ((v, v) for v in list(spec.options.keys())),
- )
+ if enum := _enums_for_spec.get(spec):
+ pass
+ else:
+ enum = StrEnum( # type: ignore[call-overload]
+ spec.name or spec.id,
+ ((v, v) for v in list(spec.options.keys())),
+ )
+ _enums_for_spec[spec] = cast(Type[StrEnum], enum)
if isinstance(spec, ConfigurableFieldSingleOption):
return ConfigurableFieldSpec(
id=spec.id,
diff --git a/libs/langchain/langchain/schema/runnable/utils.py b/libs/langchain/langchain/schema/runnable/utils.py
index 284cf8cc23..a5655b243d 100644
--- a/libs/langchain/langchain/schema/runnable/utils.py
+++ b/libs/langchain/langchain/schema/runnable/utils.py
@@ -250,6 +250,9 @@ class ConfigurableField(NamedTuple):
description: Optional[str] = None
annotation: Optional[Any] = None
+ def __hash__(self) -> int:
+ return hash((self.id, self.annotation))
+
class ConfigurableFieldSingleOption(NamedTuple):
"""A field that can be configured by the user with a default value."""
@@ -261,6 +264,9 @@ class ConfigurableFieldSingleOption(NamedTuple):
name: Optional[str] = None
description: Optional[str] = None
+ def __hash__(self) -> int:
+ return hash((self.id, tuple(self.options.items()), self.default))
+
class ConfigurableFieldMultiOption(NamedTuple):
"""A field that can be configured by the user with multiple default values."""
@@ -272,6 +278,9 @@ class ConfigurableFieldMultiOption(NamedTuple):
name: Optional[str] = None
description: Optional[str] = None
+ def __hash__(self) -> int:
+ return hash((self.id, tuple(self.options.items()), tuple(self.default)))
+
AnyConfigurableField = Union[
ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption
@@ -298,6 +307,20 @@ def get_unique_config_specs(
for id, dupes in grouped:
first = next(dupes)
others = list(dupes)
+ if len(others) > 0:
+ print(
+ first,
+ others,
+ [
+ (
+ o.id == first.id,
+ o.annotation == first.annotation,
+ o.default == first.default,
+ o.name == first.name,
+ )
+ for o in others
+ ],
+ )
if len(others) == 0:
unique.append(first)
elif all(o == first for o in others):
diff --git a/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py b/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py
index 87a558db64..3f2189342d 100644
--- a/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py
+++ b/libs/langchain/tests/unit_tests/schema/runnable/test_runnable.py
@@ -941,7 +941,8 @@ def test_configurable_fields_example() -> None:
)
)
- chain_configurable = prompt | fake_llm
+ # deduplication of configurable fields
+ chain_configurable = prompt | fake_llm | (lambda x: {"name": x}) | prompt | fake_llm
assert chain_configurable.invoke({"name": "John"}) == "a"