Skip to content

The agent engineering platform.

fix(core): fix `Runnable` parallel schema being empty when children runnable input schemas use `TypedDict` (#28196)

williamzhu54c955b53aedb2
diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py
index 8c60cb3912..71fb4978cc 100644
--- a/libs/core/langchain_core/runnables/base.py
+++ b/libs/core/langchain_core/runnables/base.py
@@ -3707,6 +3707,12 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
== "object"
for s in self.steps__.values()
):
+ for step in self.steps__.values():
+ fields = step.get_input_schema(config).model_fields
+ root_field = fields.get("root")
+ if root_field is not None and root_field.annotation != Any:
+ return super().get_input_schema(config)
+
# This is correct, but pydantic typings/mypy don't think so.
return create_model_v2(
self.get_name("Input"),
diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py
index 22300f4188..4e82358d9b 100644
--- a/libs/core/tests/unit_tests/runnables/test_runnable.py
+++ b/libs/core/tests/unit_tests/runnables/test_runnable.py
@@ -5725,3 +5725,37 @@ def test_runnable_assign() -> None:
result = runnable_assign.invoke({"input": 5})
assert result == {"input": 5, "add_step": {"added": 15}}
+
+
+def test_runnable_typed_dict_schema() -> None:
+ """Testing that the schema is generated properly(not empty) when using TypedDict.
+
+ subclasses to annotate the arguments of a RunnableParallel children.
+ """
+
+ class Foo(TypedDict):
+ foo: str
+
+ class InputData(Foo):
+ bar: str
+
+ def forward_foo(input_data: InputData) -> str:
+ return input_data["foo"]
+
+ def transform_input(input_data: InputData) -> dict[str, str]:
+ foo = input_data["foo"]
+ bar = input_data["bar"]
+
+ return {"transformed": foo + bar}
+
+ foo_runnable = RunnableLambda(forward_foo)
+ other_runnable = RunnableLambda(transform_input)
+
+ parallel = RunnableParallel(
+ foo=foo_runnable,
+ other=other_runnable,
+ )
+ assert (
+ repr(parallel.input_schema.model_validate({"foo": "Y", "bar": "Z"}))
+ == "RunnableParallel<foo,other>Input(root={'foo': 'Y', 'bar': 'Z'})"
+ )