From 11e6d40c7a9e53c15a1936aee183fc612c624f43 Mon Sep 17 00:00:00 2001 From: CoreJa Date: Mon, 26 May 2025 20:10:10 +0800 Subject: [PATCH] bug: fix fn_call error during API response (#8695) Co-authored-by: Engel Nyst --- openhands/llm/fn_call_converter.py | 8 +++++-- tests/unit/test_llm_fncall_converter.py | 28 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/openhands/llm/fn_call_converter.py b/openhands/llm/fn_call_converter.py index 35ae68b23e..09079d4d76 100644 --- a/openhands/llm/fn_call_converter.py +++ b/openhands/llm/fn_call_converter.py @@ -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: diff --git a/tests/unit/test_llm_fncall_converter.py b/tests/unit/test_llm_fncall_converter.py index a8a5d816fb..7d3694086b 100644 --- a/tests/unit/test_llm_fncall_converter.py +++ b/tests/unit/test_llm_fncall_converter.py @@ -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"]}' + )