Anyone ever stumbled upon MALFORMED_FUNCTION_CALL ...
# questions
l
Anyone ever stumbled upon MALFORMED_FUNCTION_CALL error from LiteLLM? Seems like AgentSDK llm.py retry mechanism is unable to identify and handle this type of error. Output from LiteLLM wrapper doesn't include any error indication under this scenario. Occurs with Gemini-2.5-flash model randomly I'm collecting more information on the error. Will raise an issue and try to resolve it at llm.py layer if possible.
c
It was added in litellm 6 months ago. thinky spin
l
This is the exact issue. But, it surfaces as a silent failure in Agent-SDK. I've made a few fixes in LiteLLM so that proper handling can be implemented from llm.py file in AgentSDK. Here is a complete description about the issue. ## Bug: Gemini-2.5-flash Silent Failure with
finish_reason: "MALFORMED_FUNCTION_CALL"
### Description LiteLLM users integrating Gemini-2.5-flash models via VertexAI have encountered an issue where API calls sometimes terminate abruptly without a clear error message. The underlying LiteLLM response indicates
{"finish_reason": "stop", "message": {"content": null}}
even when a more specific failure reason from the Gemini API exists. This leads to a "silent failure" within LiteLLM's abstraction, making it difficult for downstream applications (such as AgentSDK or OpenHands) to properly handle tool call failures and can result in unexpected agent behavior or termination without adequate error reporting. A sample log showing this behavior:
Copy code
log
2025-11-07 07:11:49,259 - DEBUG - openhands.sdk.llm.llm - llm.py:501 - LLM raw response: {
  "id": "T04NabSpIaK7nvgPgIrNiAI",
  "created": 1762479692,
  "model": "gemini-2.5-flash",
  "object": "chat.completion",
  "system_fingerprint": null,
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": null,
        "role": "assistant",
        "tool_calls": null,
        "function_call": null
      }
    }
  ],
  "usage": {
    "completion_tokens": 0,
    "prompt_tokens": 8412,
    "total_tokens": 8412,
    "completion_tokens_details": null,
    "prompt_tokens_details": {
      "audio_tokens": null,
      "cached_tokens": null,
      "text_tokens": 8412,
      "image_tokens": null
    }
  },
  "vertex_ai_grounding_metadata": [],
  "vertex_ai_url_context_metadata": [],
  "vertex_ai_safety_results": [],
  "vertex_ai_citation_metadata": []
}
2025-11-07 07:11:49,266 - INFO - openhands.sdk.agent.agent - agent.py:271 - LLM produced a message response - awaits user input
Message: message=Message(role='assistant', content=[], cache_enabled=False, vision_enabled=False, function_calling_enabled=False, tool_calls=None, tool_call_id=None, name=None, force_string_serializer=False, reasoning_content=None, thinking_blocks=[], responses_reasoning_item=None) metrics=MetricsSnapshot(model_name='vertex_ai/gemini-2.5-flash', accumulated_cost=0.0025236, max_budget_per_task=None, accumulated_token_usage=TokenUsage(model='vertex_ai/gemini-2.5-flash', prompt_tokens=8412, completion_tokens=0, cache_read_tokens=0, cache_write_tokens=0, reasoning_tokens=0, context_window=0, per_turn_token=8412, response_id='')) raw_response=ModelResponse(id='T04NabSpIaK7nvgPgIrNiAI', created=1762479692, model='gemini-2.5-flash', object='chat.completion', system_fingerprint=None, choices=[Choices(finish_reason='stop', index=0, message=Message(content=None, role='assistant', tool_calls=None, function_call=None, provider_specific_fields=None))], usage=Usage(completion_tokens=0, prompt_tokens=8412, total_tokens=8412, completion_tokens_details=None, prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=None, cached_tokens=None, text_tokens=8412, image_tokens=None)), vertex_ai_grounding_metadata=[], vertex_ai_url_context_metadata=[], vertex_ai_safety_results=[], vertex_ai_citation_metadata=[])
2025-11-07 07:11:49,276 - DEBUG - openhands.sdk.conversation.impl.local_conversation - local_conversation.py:235 - Conversation run iteration 1
2025-11-07 07:11:49,278 - DEBUG - asyncio - selector_events.py:64 - Using selector: EpollSelector
### Root Cause Analysis The issue stems from LiteLLM's response handling algorithm, specifically how it converts responses from various vendors (including Gemini via VertexAI) to the OpenAI response model. 1. ***Missing Mapping for `MALFORMED_FUNCTION_CALL`***: The Gemini API can return a
finishReason
of
"MALFORMED_FUNCTION_CALL"
when the model fails to generate proper tool calls. 2. ***Generic Mapping to "stop"***: In LiteLLM's
litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py::VertexGeminiConfig::get_finish_reason_mapping()
, there was no specific mapping for
"MALFORMED_FUNCTION_CALL"
. This unmapped reason, along with
"STOP"
and
"FINISH_REASON_UNSPECIFIED"
, was generically mapped to
"stop"
in the OpenAI API model. 3. ***Loss of Fidelity***: This generic mapping causes a loss of critical information. A
"MALFORMED_FUNCTION_CALL"
indicates a specific failure condition (LLM failed to adhere to structured tool calling), which is distinct from a successful
"STOP"
or other internal issues. Grouping these under
"stop"
prevents downstream applications (like OpenHands/AgentSDK) from distinguishing between a successful completion and a tool-related failure. This leads to the "silent failure" as the agent interprets a malformed function call as a successful
stop
condition. ### Proposed Solution To resolve this, we need to: 1. ***Introduce a New `finish_reason`***: Add a new
finish_reason
value, e.g.,
"tool_code"
, to the
OpenAIChatCompletionFinishReason
Literal type in
litellm/types/llms/openai.py
. This will allow LiteLLM to represent this specific failure case. 2. ***Map
MALFORMED_FUNCTION_CALL
to the New Reason***: Update the
VertexGeminiConfig::get_finish_reason_mapping()
method in
litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
to map
"MALFORMED_FUNCTION_CALL"
from the Gemini API to the newly introduced
"tool_code"
OpenAIChatCompletionFinishReason
. This change will allow applications consuming LiteLLM responses to correctly identify when a Gemini model fails due to a malformed function call, enabling them to implement appropriate error handling or retry mechanisms instead of silently terminating. ### Files to be modified: *
/litellm/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
*
/litellm/litellm/types/llms/openai.py
I have already implemented and validated this fix locally. I will open a Pull Request shortly with the proposed changes.
I will work on modifying the llm.py file to attempt retries under this particular error condition in AgentSDK.