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 output of convert_messages when called with BaseMessage.model_dump() #29763

Merged
merged 1 commit into from
Feb 12, 2025
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
2 changes: 1 addition & 1 deletion libs/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_watch:
-u LANGCHAIN_API_KEY \
-u LANGSMITH_TRACING \
-u LANGCHAIN_PROJECT \
uv run --group test ptw --snapshot-update --now . --disable-socket --allow-unix-socket -- -vv $(TEST_FILE)
uv run --group test ptw --snapshot-update --now . --disable-socket --allow-unix-socket -vv -- $(TEST_FILE)

test_profile:
uv run --group test pytest -vv tests/unit_tests/ --profile-svg
Expand Down
7 changes: 7 additions & 0 deletions libs/core/langchain_core/messages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ def _create_message_from_message_type(
if tool_call_id is not None:
kwargs["tool_call_id"] = tool_call_id
if additional_kwargs:
if response_metadata := additional_kwargs.pop("response_metadata", None):
kwargs["response_metadata"] = response_metadata
kwargs["additional_kwargs"] = additional_kwargs # type: ignore[assignment]
additional_kwargs.update(additional_kwargs.pop("additional_kwargs", {}))
if id is not None:
kwargs["id"] = id
if tool_calls is not None:
Expand All @@ -258,8 +261,12 @@ def _create_message_from_message_type(
else:
kwargs["tool_calls"].append(tool_call)
if message_type in ("human", "user"):
if example := kwargs.get("additional_kwargs", {}).pop("example", False):
kwargs["example"] = example
message: BaseMessage = HumanMessage(content=content, **kwargs)
elif message_type in ("ai", "assistant"):
if example := kwargs.get("additional_kwargs", {}).pop("example", False):
kwargs["example"] = example
message = AIMessage(content=content, **kwargs)
elif message_type in ("system", "developer"):
if message_type == "developer":
Expand Down
16 changes: 16 additions & 0 deletions libs/core/tests/unit_tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,15 @@ def test_convert_to_messages() -> None:
"artifact": {"foo": 123},
},
{"role": "remove", "id": "message_to_remove", "content": ""},
{
"content": "Now the turn for Larry to ask a question about the book!",
"additional_kwargs": {"metadata": {"speaker_name": "Presenter"}},
"response_metadata": {},
"type": "human",
"name": None,
"id": "1",
"example": False,
},
]
)
expected = [
Expand All @@ -762,6 +771,13 @@ def test_convert_to_messages() -> None:
ToolMessage(tool_call_id="tool_id", content="Hi!"),
ToolMessage(tool_call_id="tool_id2", content="Bye!", artifact={"foo": 123}),
RemoveMessage(id="message_to_remove"),
HumanMessage(
content="Now the turn for Larry to ask a question about the book!",
additional_kwargs={"metadata": {"speaker_name": "Presenter"}},
response_metadata={},
id="1",
example=False,
),
]
assert expected == actual

Expand Down
8 changes: 4 additions & 4 deletions libs/core/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading