Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: fix issue with runnable parallel schema being empty when children runnable input schemas use TypedDict's #28196

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libs/core/langchain_core/runnables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3593,6 +3593,11 @@ def get_input_schema(
== "object"
for s in self.steps__.values()
):
for step in self.steps__.values():
for k, v in step.get_input_schema(config).model_fields.items():
if v.annotation != Any and k == "root":
return super().get_input_schema(config)

# This is correct, but pydantic typings/mypy don't think so.
return create_model_v2( # type: ignore[call-overload]
self.get_name("Input"),
Expand Down
37 changes: 37 additions & 0 deletions libs/core/tests/unit_tests/runnables/test_runnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5465,3 +5465,40 @@ def add_ten(x: dict[str, int]) -> dict[str, int]:

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.
"""
from typing_extensions import TypedDict

from langchain_core.runnables import RunnableLambda, RunnableParallel

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.validate({"foo": "Y", "bar": "Z"}))
== "RunnableParallel<foo,other>Input(root={'foo': 'Y', 'bar': 'Z'})"
)
Loading