bug: fix fn_call error during API response (#8695)

Co-authored-by: Engel Nyst <engel.nyst@gmail.com>
This commit is contained in:
CoreJa
2025-05-26 20:10:10 +08:00
committed by GitHub
parent 41d84ee8cd
commit 11e6d40c7a
2 changed files with 34 additions and 2 deletions

View File

@@ -536,8 +536,12 @@ def convert_fncall_messages_to_non_fncall_messages(
if isinstance(content, str):
content = prefix + content
elif isinstance(content, list):
if content and content[-1]['type'] == 'text':
content[-1]['text'] = prefix + content[-1]['text']
if content and (
first_text_content := next(
(c for c in content if c['type'] == 'text'), None
)
):
first_text_content['text'] = prefix + first_text_content['text']
else:
content = [{'type': 'text', 'text': prefix}] + content
else:

View File

@@ -994,3 +994,31 @@ def test_convert_fncall_messages_without_cache_control():
result[0]['content'][0]['text']
== 'EXECUTION RESULT of [test_tool]:\ntest content'
)
def test_convert_fncall_messages_with_image_url():
"""Test that convert_fncall_messages_to_non_fncall_messages handles image URLs correctly."""
messages = [
{
'role': 'tool',
'name': 'browser',
'content': [
{
'type': 'text',
'text': 'some browser tool results',
},
{
'type': 'image_url',
'image_url': {'url': 'data:image/gif;base64,R0lGODlhAQABAAAAACw='},
},
],
}
]
converted_messages = convert_fncall_messages_to_non_fncall_messages(messages, [])
assert len(converted_messages) == 1
assert converted_messages[0]['role'] == 'user'
assert len(converted_messages[0]['content']) == len(messages[0]['content'])
assert (
next(c for c in converted_messages[0]['content'] if c['type'] == 'text')['text']
== f'EXECUTION RESULT of [{messages[0]["name"]}]:\n{messages[0]["content"][0]["text"]}'
)