Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Add JSON serialization for array and object parameters when converting tools (#8780)

Howie Zhoub634e10b45cd
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'<parameter={param_name}>'
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 += '</parameter>\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
</parameter>
+</function>""",
+ ),
+ # 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',
+ }
+ ],
+ """<function=test_function>
+<parameter=command>test</parameter>
+<parameter=path>/test/file.py</parameter>
+<parameter=tags>["tag1", "tag2", "tag with spaces"]</parameter>
+</function>""",
+ ),
+ # 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',
+ }
+ ],
+ """<function=test_function>
+<parameter=command>test</parameter>
+<parameter=path>/test/file.py</parameter>
+<parameter=metadata>{"key1": "value1", "key2": 42, "nested": {"subkey": "subvalue"}}</parameter>
</function>""",
),
],