Skip to content

Commit

Permalink
core: Fix output of convert_messages when called with BaseMessage.mod…
Browse files Browse the repository at this point in the history
…el_dump() (#29763)

- additional_kwargs was being nested twice
- example, response_metadata was placed inside additional_kwargs
  • Loading branch information
nfcampos authored Feb 12, 2025
1 parent f4e3e86 commit fe59f2c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
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.

0 comments on commit fe59f2c

Please sign in to comment.