MALFORMED_FUNCTION_CALL finish reason happens too frequently with vertex AI

I often get the MALFORMED_FUNCTION_CALL finish reason. This is issue is extremely frequent with Vertex AI and less frequent with Google AI developer API. To repro, use this

import google.genai as genai
from dotenv import load_dotenv
import os

load_dotenv()

client = genai.Client(
    vertexai=True, project=os.getenv("GOOGLE_CLOUD_PROJECT"), location="us-central1"
)

tool_def = {
    "name": "think",
    "description": "Call this tool to think about a given thought.",
    "parameters": {
        "properties": {
            "thought": {
                "description": "The thought to think about.",
                "type": "string",
            }
        },
        "required": ["thought"],
        "type": "object",
    },
}

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents=[{"role": "user", "parts": [{"text": "Hi, how's the weather in Tokyo?"}]}],
    config={
        "tools": [{"function_declarations": [tool_def]}],
        "system_instruction": "You have the tool get_weather available. You ABSOLUTELY MUST call the tool get_weather to answer the user's question.",
        "thinking_config": {"thinking_budget": 24576, "include_thoughts": True},
    },
)
response.model_dump()

6 out of 7 times, the response has the MALFORMED_FUNCTION_CALL finish reason. Now, I know that the system prompt "You have the tool get_weather available. You ABSOLUTELY MUST call the tool get_weather to answer the user's question." is very perverse because it is intentionally trying to trick the model; but I did this to make the issue more reproducible. In my actual production environment, the system prompt looks more like this "You will get the tool get_weather at some point in the future. You ABSOLUTELY MUST call the tool get_weather to answer the user's weather question.".

The interesting phenomenon is that this issue is much less frequent but still present when using the Google AI Developer API. Run the above repro code with the Google AI Developer API and you will get this issue like 1 out of 10 times.

why is malformed function call even handled at the server level? I understand that malformed function calls can happen for any model, but literally any other model provider (OpenAI, Anthropic, etc.) still returns the malformed function call as a regular response. I prefer this because I want to handle the malformed function call at my app layer, which allows me to do things like telling the model "You called tool_x incorrectly in the following way ... please do the following ... to fix it"

1 Like

to me, the ideal fix is to disable server-side MALFORMED_FUNCTION_CALL or expose a flag to disable it. Therefore, malformed function calls are returned as regular responses and developers become responsible for handling them.

I have been noticing this a LOT too over the past week. What’s more, I know for a fact it’s nothing our code is doing - because the exact same API call works 70% of the time - but sometimes Gemini will get its knickers in a twist and malform the call. I’m certain this is a server side error (and honestly your test proves it too)

Recently I have the same problem, in my case switching to version ‘gemini-2.5-flash-preview-04-17’ helped.

ok, outdated, they just turned off ‘gemini-2.5-flash-preview-04-17’, this version is no longer available - thanks a lot guys :slight_smile:

I also seen a rise with this error, which caused me too many hours of wasted time in finding the problem in my code.

YMMV, but what I’ve found is that this shows up when there is a model overload. That is, the prompt plus the information send in the request (chat history, including maybe previous function requests and responses, files uploaded, etc.) causes the model to be confused.

Now I know - if I get this error, many times, the problem is NOT in the code itself, but around the prompt and how I use it. Sometimes it’s a problem with mismatched input, sometimes too much overlapping context. Think about it this way: something distracts the model from behaving properly.

I hope this helps and again - this is only based on my (limited) experience.

@Mrinal_Ghosh hi, have you had a chance to review this issue?

Hi,

Are struggling with Gemini 2.5 Flash only or do you see this error across all models?

@Lalit_Kumar I only use gemini 2.5 flash, so I dont know about the other models. You can try using the repro code I posted above with different models, but I can confirm that the issue definitely exists with gemini 2.5 flash.

Hi,

In your code, you defined a tool called “think”, but in the system prompt you instructed the model to call “get_weather”, which doesn’t exist. If you define a “get_weather” tool and reference it in the system instructions instead, it should help resolve the issue.

If you still face issue, feel free to reach out. We will try to assist you with that.

@Lalit_Kumar did you read this part of my post? I setup the repro code specifically to make it more reproducible. I can make the repro code more “normal” but then it becomes less reproducible.

We are also having this issue, which results in sporadic failures. Is there any timeline for fixing this issue or advice for workarounds? We have no issues running the same workload with OpenAI/Anthropic.

1 Like