Skip to content

The agent engineering platform.

huggingface: fix tool argument serialization in _convert_TGI_message_to_LC_message (#26075)

Wang, Yid834c6b6185b
diff --git a/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py b/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py
index c09f0bcbb6..593df16133 100644
--- a/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py
+++ b/libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py
@@ -1,5 +1,6 @@
"""Hugging Face Chat Wrapper."""
+import json
from dataclasses import dataclass
from typing import (
Any,
@@ -106,9 +107,10 @@ def _convert_TGI_message_to_LC_message(
additional_kwargs: Dict = {}
if tool_calls := _message.tool_calls:
if "arguments" in tool_calls[0]["function"]:
- functions_string = str(tool_calls[0]["function"].pop("arguments"))
- corrected_functions = functions_string.replace("'", '"')
- tool_calls[0]["function"]["arguments"] = corrected_functions
+ functions = tool_calls[0]["function"].pop("arguments")
+ tool_calls[0]["function"]["arguments"] = json.dumps(
+ functions, ensure_ascii=False
+ )
additional_kwargs["tool_calls"] = tool_calls
return AIMessage(content=content, additional_kwargs=additional_kwargs)
diff --git a/libs/partners/huggingface/tests/unit_tests/test_chat_models.py b/libs/partners/huggingface/tests/unit_tests/test_chat_models.py
index 1b9bfad66d..fb9d141d02 100644
--- a/libs/partners/huggingface/tests/unit_tests/test_chat_models.py
+++ b/libs/partners/huggingface/tests/unit_tests/test_chat_models.py
@@ -66,7 +66,7 @@ def test_convert_message_to_chat_message(
TGI_MESSAGE(
role="assistant",
content="",
- tool_calls=[{"function": {"arguments": "'function string'"}}],
+ tool_calls=[{"function": {"arguments": "function string"}}],
),
AIMessage(
content="",
@@ -75,6 +75,23 @@ def test_convert_message_to_chat_message(
},
),
),
+ (
+ TGI_MESSAGE(
+ role="assistant",
+ content="",
+ tool_calls=[
+ {"function": {"arguments": {"answer": "function's string"}}}
+ ],
+ ),
+ AIMessage(
+ content="",
+ additional_kwargs={
+ "tool_calls": [
+ {"function": {"arguments": '{"answer": "function\'s string"}'}}
+ ]
+ },
+ ),
+ ),
],
)
def test_convert_TGI_message_to_LC_message(