OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Fix KeyError when accessing content in messages with function calls

Open neubig opened this issue 1 year ago • 1 comments

End-user friendly description of the problem this fixes or functionality that this introduces

  • [x] Include this change in the Release Notes. If checked, you must provide an end-user friendly description for your change below

Fixes a "message['content'] key" error


Give a summary of what the PR does, explaining any non-trivial design decisions

Fixes #5142

The error occurs when a message in the messages list doesn't have a 'content' key. This can happen with OpenRouter when using function calls, as some messages might only have function call information without content.

Let me summarize what OpenHands did to fix the issue ("I" = OpenHands):

I identified that the error was occurring in the convert_fncall_messages_to_non_fncall_messages function when trying to access the 'content' key from messages that only had function calls.

I wrote a test case to reproduce the issue, which showed that messages with function calls but no content were causing a KeyError.

I fixed the issue by modifying the code to use message.get('content') instead of message['content'] in three places:

In the main message processing loop In the system message handling In the assistant message handling The fix ensures that messages without a 'content' key are handled gracefully, which is important when dealing with function calls from OpenRouter or other providers that might not include content in their function call messages.

neubig avatar Nov 21 '24 01:11 neubig

Just to note, also fixed here: https://github.com/All-Hands-AI/OpenHands/pull/5026

Along with a couple of other conversion issues.

enyst avatar Nov 21 '24 02:11 enyst

Oh, thanks a lot @enyst , I didn't see it mentioned in the issue. I'll close this in favor of yours.

neubig avatar Nov 21 '24 02:11 neubig

My bad, thank you. I'm working on a refactoring that will hopefully make these stop cropping up.

enyst avatar Nov 21 '24 02:11 enyst