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

This commit is contained in:
Howie Zhou 2025-06-10 13:48:49 -07:00 committed by GitHub
parent 73f01657eb
commit b634e10b45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View File

@ -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'

View File

@ -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>""",
),
],