From b634e10b45cda0bc719674eaea6f2253e8a025e6 Mon Sep 17 00:00:00 2001
From: Howie Zhou <1996yeyan@gmail.com>
Date: Tue, 10 Jun 2025 13:48:49 -0700
Subject: [PATCH] Add JSON serialization for array and object parameters when
converting tools (#8780)
---
openhands/llm/fn_call_converter.py | 5 +++-
tests/unit/test_llm_fncall_converter.py | 38 +++++++++++++++++++++++++
2 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/openhands/llm/fn_call_converter.py b/openhands/llm/fn_call_converter.py
index 90827d10fb..64f67cf086 100644
--- a/openhands/llm/fn_call_converter.py
+++ b/openhands/llm/fn_call_converter.py
@@ -426,7 +426,10 @@ def convert_tool_call_to_string(tool_call: dict) -> str:
ret += f''
if is_multiline:
ret += '\n'
- ret += f'{param_value}'
+ if isinstance(param_value, list) or isinstance(param_value, dict):
+ ret += json.dumps(param_value)
+ else:
+ ret += f'{param_value}'
if is_multiline:
ret += '\n'
ret += '\n'
diff --git a/tests/unit/test_llm_fncall_converter.py b/tests/unit/test_llm_fncall_converter.py
index 5bfc14b91e..52274f8abe 100644
--- a/tests/unit/test_llm_fncall_converter.py
+++ b/tests/unit/test_llm_fncall_converter.py
@@ -680,6 +680,44 @@ def example():
print("hello")
return True
+""",
+ ),
+ # Test case with list parameter value
+ (
+ [
+ {
+ 'index': 1,
+ 'function': {
+ 'arguments': '{"command": "test", "path": "/test/file.py", "tags": ["tag1", "tag2", "tag with spaces"]}',
+ 'name': 'test_function',
+ },
+ 'id': 'test_id',
+ 'type': 'function',
+ }
+ ],
+ """
+test
+/test/file.py
+["tag1", "tag2", "tag with spaces"]
+""",
+ ),
+ # Test case with dict parameter value
+ (
+ [
+ {
+ 'index': 1,
+ 'function': {
+ 'arguments': '{"command": "test", "path": "/test/file.py", "metadata": {"key1": "value1", "key2": 42, "nested": {"subkey": "subvalue"}}}',
+ 'name': 'test_function',
+ },
+ 'id': 'test_id',
+ 'type': 'function',
+ }
+ ],
+ """
+test
+/test/file.py
+{"key1": "value1", "key2": 42, "nested": {"subkey": "subvalue"}}
""",
),
],