mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-04 18:53:02 +00:00
core: Fix output of convert_messages when called with BaseMessage.model_dump() (#29763)
- additional_kwargs was being nested twice - example, response_metadata was placed inside additional_kwargs
This commit is contained in:
parent
f4e3e86fbb
commit
fe59f2cc88
@ -23,7 +23,7 @@ test_watch:
|
|||||||
-u LANGCHAIN_API_KEY \
|
-u LANGCHAIN_API_KEY \
|
||||||
-u LANGSMITH_TRACING \
|
-u LANGSMITH_TRACING \
|
||||||
-u LANGCHAIN_PROJECT \
|
-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:
|
test_profile:
|
||||||
uv run --group test pytest -vv tests/unit_tests/ --profile-svg
|
uv run --group test pytest -vv tests/unit_tests/ --profile-svg
|
||||||
|
@ -236,7 +236,10 @@ def _create_message_from_message_type(
|
|||||||
if tool_call_id is not None:
|
if tool_call_id is not None:
|
||||||
kwargs["tool_call_id"] = tool_call_id
|
kwargs["tool_call_id"] = tool_call_id
|
||||||
if additional_kwargs:
|
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]
|
kwargs["additional_kwargs"] = additional_kwargs # type: ignore[assignment]
|
||||||
|
additional_kwargs.update(additional_kwargs.pop("additional_kwargs", {}))
|
||||||
if id is not None:
|
if id is not None:
|
||||||
kwargs["id"] = id
|
kwargs["id"] = id
|
||||||
if tool_calls is not None:
|
if tool_calls is not None:
|
||||||
@ -258,8 +261,12 @@ def _create_message_from_message_type(
|
|||||||
else:
|
else:
|
||||||
kwargs["tool_calls"].append(tool_call)
|
kwargs["tool_calls"].append(tool_call)
|
||||||
if message_type in ("human", "user"):
|
if message_type in ("human", "user"):
|
||||||
|
if example := kwargs.get("additional_kwargs", {}).pop("example", False):
|
||||||
|
kwargs["example"] = example
|
||||||
message: BaseMessage = HumanMessage(content=content, **kwargs)
|
message: BaseMessage = HumanMessage(content=content, **kwargs)
|
||||||
elif message_type in ("ai", "assistant"):
|
elif message_type in ("ai", "assistant"):
|
||||||
|
if example := kwargs.get("additional_kwargs", {}).pop("example", False):
|
||||||
|
kwargs["example"] = example
|
||||||
message = AIMessage(content=content, **kwargs)
|
message = AIMessage(content=content, **kwargs)
|
||||||
elif message_type in ("system", "developer"):
|
elif message_type in ("system", "developer"):
|
||||||
if message_type == "developer":
|
if message_type == "developer":
|
||||||
|
@ -738,6 +738,15 @@ def test_convert_to_messages() -> None:
|
|||||||
"artifact": {"foo": 123},
|
"artifact": {"foo": 123},
|
||||||
},
|
},
|
||||||
{"role": "remove", "id": "message_to_remove", "content": ""},
|
{"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 = [
|
expected = [
|
||||||
@ -762,6 +771,13 @@ def test_convert_to_messages() -> None:
|
|||||||
ToolMessage(tool_call_id="tool_id", content="Hi!"),
|
ToolMessage(tool_call_id="tool_id", content="Hi!"),
|
||||||
ToolMessage(tool_call_id="tool_id2", content="Bye!", artifact={"foo": 123}),
|
ToolMessage(tool_call_id="tool_id2", content="Bye!", artifact={"foo": 123}),
|
||||||
RemoveMessage(id="message_to_remove"),
|
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
|
assert expected == actual
|
||||||
|
|
||||||
|
@ -1026,7 +1026,7 @@ typing = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "langchain-tests"
|
name = "langchain-tests"
|
||||||
version = "0.3.10"
|
version = "0.3.11"
|
||||||
source = { directory = "../standard-tests" }
|
source = { directory = "../standard-tests" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "httpx" },
|
{ name = "httpx" },
|
||||||
@ -1042,7 +1042,7 @@ dependencies = [
|
|||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "httpx", specifier = ">=0.25.0,<1" },
|
{ name = "httpx", specifier = ">=0.25.0,<1" },
|
||||||
{ name = "langchain-core", specifier = ">=0.3.33,<1.0.0" },
|
{ name = "langchain-core", specifier = ">=0.3.34,<1.0.0" },
|
||||||
{ name = "numpy", marker = "python_full_version < '3.12'", specifier = ">=1.24.0,<2.0.0" },
|
{ name = "numpy", marker = "python_full_version < '3.12'", specifier = ">=1.24.0,<2.0.0" },
|
||||||
{ name = "numpy", marker = "python_full_version >= '3.12'", specifier = ">=1.26.2,<3" },
|
{ name = "numpy", marker = "python_full_version >= '3.12'", specifier = ">=1.26.2,<3" },
|
||||||
{ name = "pytest", specifier = ">=7,<9" },
|
{ name = "pytest", specifier = ">=7,<9" },
|
||||||
@ -1063,14 +1063,14 @@ typing = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "langchain-text-splitters"
|
name = "langchain-text-splitters"
|
||||||
version = "0.3.5"
|
version = "0.3.6"
|
||||||
source = { directory = "../text-splitters" }
|
source = { directory = "../text-splitters" }
|
||||||
dependencies = [
|
dependencies = [
|
||||||
{ name = "langchain-core" },
|
{ name = "langchain-core" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [{ name = "langchain-core", specifier = ">=0.3.33,<1.0.0" }]
|
requires-dist = [{ name = "langchain-core", specifier = ">=0.3.34,<1.0.0" }]
|
||||||
|
|
||||||
[package.metadata.requires-dev]
|
[package.metadata.requires-dev]
|
||||||
dev = [
|
dev = [
|
||||||
|
Loading…
Reference in New Issue
Block a user