Skip to content

Commit

Permalink
fix: Handle response metadata in merge_messages_runs (#28453)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Dec 2, 2024
1 parent 60021e5 commit ecee41a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/core/langchain_core/messages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ def merge_message_runs(
else:
last_chunk = _msg_to_chunk(last)
curr_chunk = _msg_to_chunk(curr)
if curr_chunk.response_metadata:
curr_chunk.response_metadata.clear()
if (
isinstance(last_chunk.content, str)
and isinstance(curr_chunk.content, str)
Expand Down
18 changes: 18 additions & 0 deletions libs/core/tests/unit_tests/messages/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def test_merge_message_runs_str_without_separator(
assert messages == messages_model_copy


def test_merge_message_runs_response_metadata() -> None:
messages = [
AIMessage("foo", id="1", response_metadata={"input_tokens": 1}),
AIMessage("bar", id="2", response_metadata={"input_tokens": 2}),
]
expected = [
AIMessage(
"foo\nbar",
id="1",
response_metadata={"input_tokens": 1},
)
]
actual = merge_message_runs(messages)
assert actual == expected
# Check it's not mutated
assert messages[1].response_metadata == {"input_tokens": 2}


def test_merge_message_runs_content() -> None:
messages = [
AIMessage("foo", id="1"),
Expand Down

0 comments on commit ecee41a

Please sign in to comment.