Skip to content
Merged
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
7 changes: 7 additions & 0 deletions libs/core/langchain_core/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,13 @@ class Person(BaseModel):
messages.append(
AIMessage(content="", additional_kwargs={"tool_calls": openai_tool_calls})
)
if tool_outputs is not None and len(tool_outputs) != len(openai_tool_calls):
msg = (
f"The number of tool_outputs ({len(tool_outputs)}) must match the number "
f"of tool_calls ({len(openai_tool_calls)}). Got {len(tool_outputs)} "
f"output(s) for {len(openai_tool_calls)} tool call(s)."
)
raise ValueError(msg)
tool_outputs = tool_outputs or ["You have correctly called this tool."] * len(
openai_tool_calls
)
Expand Down
18 changes: 18 additions & 0 deletions libs/core/tests/unit_tests/utils/test_function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,24 @@ def test_tool_outputs() -> None:
assert not response.tool_calls


def test_tool_outputs_fewer_than_tool_calls_raises() -> None:
with pytest.raises(ValueError, match="must match"):
tool_example_to_messages(
input="Extract both values",
tool_calls=[FakeCall(data="a"), FakeCall(data="b")],
tool_outputs=["only one output"],
)


def test_tool_outputs_more_than_tool_calls_raises() -> None:
with pytest.raises(ValueError, match="must match"):
tool_example_to_messages(
input="Extract one value",
tool_calls=[FakeCall(data="a")],
tool_outputs=["output1", "extra output"],
)


@pytest.mark.parametrize(
"typed_dict",
[ExtensionsTypedDict, TypingTypedDict],
Expand Down
Loading