Skip to content

Commit

Permalink
Disable ensure_ascii when dumping to json tool responses (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza authored Nov 20, 2024
1 parent b5be4b6 commit 1b228a4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion haystack_experimental/components/generators/chat/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def _convert_message_to_openai_format(message: ChatMessage) -> Dict[str, Any]:
{
"id": tc.id,
"type": "function",
"function": {"name": tc.tool_name, "arguments": json.dumps(tc.arguments)},
# We disable ensure_ascii so special chars like emojis are not converted
"function": {"name": tc.tool_name, "arguments": json.dumps(tc.arguments, ensure_ascii=False)},
}
)
openai_msg["tool_calls"] = openai_tool_calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ def run(self, messages: List[ChatMessage]):
try:
function_response = function_to_call(**function_args)
messages.append(
# We disable ensure_ascii so special chars like emojis are not converted
ChatMessage.from_function(
content=json.dumps(function_response),
content=json.dumps(function_response, ensure_ascii=False),
name=function_name,
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def run(
except Exception as e: # pylint: disable=broad-exception-caught
logger.error("Error invoking OpenAPI endpoint. Error: {e}", e=str(e))
service_response = {"error": str(e)}
response_messages = [ChatMessage.from_user(json.dumps(service_response))]
# We disable ensure_ascii so special chars like emojis are not converted
response_messages = [ChatMessage.from_user(json.dumps(service_response, ensure_ascii=False))]

return {"service_response": response_messages}

Expand Down
3 changes: 2 additions & 1 deletion haystack_experimental/components/tools/tool_invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def _prepare_tool_result_message(self, result: Any, tool_call: ToolCall) -> Chat

if self.convert_result_to_json_string:
try:
tool_result_str = json.dumps(result)
# We disable ensure_ascii so special chars like emojis are not converted
tool_result_str = json.dumps(result, ensure_ascii=False)
except Exception as e:
if self.raise_on_failure:
raise StringConversionError("Failed to convert tool result to string using `json.dumps`") from e
Expand Down

0 comments on commit 1b228a4

Please sign in to comment.