π OpenHands: AI-Driven Development
bug: fix fn_call error during API response (#8695)
CoreJa11e6d40c7a9e
diff --git a/openhands/llm/fn_call_converter.py b/openhands/llm/fn_call_converter.pyindex 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 + contentelif 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}] + contentelse:diff --git a/tests/unit/test_llm_fncall_converter.py b/tests/unit/test_llm_fncall_converter.pyindex 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"]}'+ )