Openhands cli seems to crash (kimi-k2 via openhand...
# questions
r
Openhands cli seems to crash (kimi-k2 via openhands provider)
165334 - openhandsERROR agent_controller.py:306 - [Agent Controller dadcf27f-5d07-49ee-9995-b5a03fa2a859-155cc730957f78d7] Error while running the agent (session ID: dadcf27f-5d07-49ee-9995-b5a03fa2a859-155cc730957f78d7): litellm.BadRequestError: Error code: 400 - {'error': {'message': "litellm.BadRequestError: MoonshotException - Invalid request: the message at position 53 with role 'assistant' must be a stringNo fallback model group found for original model_group=kimi-k2-0711-preview. Fallbacks=[{'claude-3-5-haiku-20241022': ['anthropic/claude-3-5-haiku-20241022']}, {'claude-3-5-sonnet-20241022': ['anthropic/claude-3-5-sonnet-20241022']}, {'claude-3-5-sonnet-20240620': ['anthropic/claude-3-5-sonnet-20240620']}, {'gpt-4o-2024-08-06': ['openai/gpt-4o-2024-08-06']}, {'gpt-4o-2024-05-13': ['openai/gpt-4o-2024-05-13']}, {'gpt-4o-mini-2024-07-18': ['openai/gpt-4o-mini-2024-07-18']}]. Received Model Group=kimi-k2-0711-preview\nAvailable Model Group Fallbacks=None\nError doing the fallback: litellm.BadRequestError: MoonshotException - Invalid request: the message at position 53 with role 'assistant' must be a stringNo fallback model group found for original model_group=kimi-k2-0711-preview. Fallbacks=[{'claude-3-5-haiku-20241022': ['anthropic/claude-3-5-haiku-20241022']}, {'claude-3-5-sonnet-20241022': ['anthropic/claude-3-5-sonnet-20241022']}, {'claude-3-5-sonnet-20240620': ['anthropic/claude-3-5-sonnet-20240620']}, {'gpt-4o-2024-08-06': ['openai/gpt-4o-2024-08-06']}, {'gpt-4o-2024-05-13': ['openai/gpt-4o-2024-05-13']}, {'gpt-4o-mini-2024-07-18': ['openai/gpt-4o-mini-2024-07-18']}] LiteLLM Retried: 2 times, LiteLLM Max Retries: 3", 'type': 'invalid_request_error', 'param': None, 'code': '400'}}. Traceback: Traceback (most recent call last): Usually happens after the agent finished something and I give a follow up instruction
l
I found this (likely related), translated from this chinese page. Just in case someone will start looking this up.
c
I think maybe we should remove empty "assistant" messages in LLM calls before we send an LLM request? @nutritious-balloon-65098 can you take a look
n
I'm on it! Xingyao Wang can track my progress here.
I've fixed the issue with empty "assistant" messages in LLM calls. Here's a summary of what I found and fixed: Problem Identified When using the kimi-k2 model via the OpenHands provider, the system was encountering an error: "the message at position 53 with role 'assistant' must be a string". This occurred because some LLM providers (like Moonshot AI for kimi-k2) require that assistant messages always have a string content, even if it's empty. Root Cause In the
_process_action
method of
ConversationMemory
class, when an assistant message had empty content, the code was creating a message with an empty content list:
Copy code
content=[TextContent(text=assistant_msg.content)]
if assistant_msg.content and assistant_msg.content.strip()
else [],
Solution Implemented I modified the code to always include a TextContent with an empty string rather than an empty list:
Copy code
content=[TextContent(text=assistant_msg.content or '')],
This ensures that even when the assistant message content is empty, we still include a TextContent with an empty string, which satisfies the requirements of LLM providers like Moonshot AI. The changes have been committed to the repository with appropriate comments explaining the fix. All pre-commit hooks and linting checks have passed successfully. This fix should resolve the issue with empty assistant messages in LLM calls, particularly when using the kimi-k2 model.
c
@nutritious-balloon-65098 any reason why we need that empty message in the first place?
n
I'm on it! Xingyao Wang can continue tracking my progress here.
I've fixed the issue with empty "assistant" messages in LLM calls that was causing errors with the kimi-k2 model via the Moonshot AI provider. The problem was in the
_process_action
method of
ConversationMemory
class, where empty assistant messages were being created with an empty content list (
[]
) instead of a list containing an empty string. Some LLM providers like Moonshot AI require that assistant messages always have a string content, even if it's empty. The fix: • Changed the code to always include a TextContent with an empty string rather than an empty list • Added a comment explaining why this is necessary • Verified the change passes all linting checks This empty message is necessary because: 1. It contains important
tool_calls
information for function calling 2. It maintains the proper conversation flow and role alternation 3. It satisfies API requirements for providers like Moonshot AI The change is minimal and focused on the specific issue, modifying only the necessary code to fix the problem.