From b97bcb6d6d7d540a88150aba9c46121d8278f307 Mon Sep 17 00:00:00 2001 From: B-Step62 Date: Wed, 11 Sep 2024 18:04:29 +0900 Subject: [PATCH 1/3] Stop propagating 'id' field from Message object to request payload Signed-off-by: B-Step62 --- libs/databricks/langchain_databricks/chat_models.py | 3 --- libs/databricks/tests/unit_tests/test_chat_models.py | 9 +++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/databricks/langchain_databricks/chat_models.py b/libs/databricks/langchain_databricks/chat_models.py index a513aa0..f9fc7be 100644 --- a/libs/databricks/langchain_databricks/chat_models.py +++ b/libs/databricks/langchain_databricks/chat_models.py @@ -422,9 +422,6 @@ def _convert_message_to_dict(message: BaseMessage) -> dict: if (name := message.name or message.additional_kwargs.get("name")) is not None: message_dict["name"] = name - if id := message.id: - message_dict["id"] = id - if isinstance(message, ChatMessage): return {"role": message.role, **message_dict} elif isinstance(message, HumanMessage): diff --git a/libs/databricks/tests/unit_tests/test_chat_models.py b/libs/databricks/tests/unit_tests/test_chat_models.py index 92ba8a0..bbd519f 100644 --- a/libs/databricks/tests/unit_tests/test_chat_models.py +++ b/libs/databricks/tests/unit_tests/test_chat_models.py @@ -231,6 +231,15 @@ def test_convert_message(role: str, expected_output: BaseMessage) -> None: assert dict_result == message +def test_convert_message_not_propagate_id() -> None: + # The AIMessage returned by the model endpoint can contain "id" field, + # but it is not always supported for requests. Therefore, we should not + # propagate it to the request payload. + message = AIMessage(content="foo", id="some-id") + result = _convert_message_to_dict(message) + assert result == {"role": "assistant", "content": "foo"} + + def test_convert_message_with_tool_calls() -> None: ID = "call_fb5f5e1a-bac0-4422-95e9-d06e6022ad12" tool_calls = [ From db74230872b530a6d6d98af3c7a1828a5d2c6800 Mon Sep 17 00:00:00 2001 From: B-Step62 Date: Wed, 11 Sep 2024 21:46:36 +0900 Subject: [PATCH 2/3] test fix Signed-off-by: B-Step62 --- libs/databricks/tests/unit_tests/test_chat_models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/databricks/tests/unit_tests/test_chat_models.py b/libs/databricks/tests/unit_tests/test_chat_models.py index bbd519f..6db99b2 100644 --- a/libs/databricks/tests/unit_tests/test_chat_models.py +++ b/libs/databricks/tests/unit_tests/test_chat_models.py @@ -276,6 +276,7 @@ def test_convert_message_with_tool_calls() -> None: # convert back dict_result = _convert_message_to_dict(result) + message_with_tools.pop("id") # id is not propagated assert dict_result == message_with_tools @@ -329,6 +330,7 @@ def test_convert_tool_message_chunk() -> None: # convert back dict_result = _convert_message_to_dict(result) + delta.pop("id") # id is not propagated assert dict_result == delta From dd39f0b240e1ee6175a92799aecdb27d39bd2af9 Mon Sep 17 00:00:00 2001 From: Yuki Watanabe <31463517+B-Step62@users.noreply.github.com> Date: Sat, 14 Sep 2024 10:48:10 +0900 Subject: [PATCH 3/3] Update libs/databricks/tests/unit_tests/test_chat_models.py Co-authored-by: Harutaka Kawamura --- libs/databricks/tests/unit_tests/test_chat_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/databricks/tests/unit_tests/test_chat_models.py b/libs/databricks/tests/unit_tests/test_chat_models.py index 6db99b2..bb20da4 100644 --- a/libs/databricks/tests/unit_tests/test_chat_models.py +++ b/libs/databricks/tests/unit_tests/test_chat_models.py @@ -237,7 +237,7 @@ def test_convert_message_not_propagate_id() -> None: # propagate it to the request payload. message = AIMessage(content="foo", id="some-id") result = _convert_message_to_dict(message) - assert result == {"role": "assistant", "content": "foo"} + assert "id" not in result def test_convert_message_with_tool_calls() -> None: