Getting FinishReason.MALFORMED_FUNCTION_CALL when function calling arugments contain large amount of text content

In the python sdk when function calling is returned by gemini and it contains large amount of text it fails and returns malformed function call.
For example i have a write_file tool attached that takes filename and file content, when i ask it to create a large report of something it uses the tool to write it and second argument contains that large text and it just fails with automatic function calling on and off, first I was confused but this is the only thing that seems to be consistent.

I dont know if there is a limit on how large the function calling response should be, but maybe that is the reason? Seems like a parsing faliure or gemini just stops midway.

Hi @FlinCode,

Welcome to forum!!

Oh interesting, to help me better understand the issue and replicate it on my end, could you possibly provide a simplified sample of the prompt you’re using, along with a representative example of the kind of “large report” content that’s causing the problem? This would allow me to test the scenario and see if I can reproduce the error.

Cheers!

It would also have access to a search tool and get website content tool and i would ask it to look up specific topic like Reinforcement learning and tell it to write a learn large documenation/paper/article on it and thats the usual scenario where it would fail, to add it would usually have access to around 30 more tools including those. But I would expect gemini to not hallucinate and write the improper function calling structure that could cause the malformed function call.
I am still not 100% sure if the large content is the core issue here but most of the time this is what caused that error for me.

Hi @FlinCode,

import os
import datetime
from google import genai
from google.genai import types
from google.colab import userdata

STORAGE_DIR = "gemini_outputs"  # Directory to store outputs

# Create the storage directory if it doesn't exist
if not os.path.exists(STORAGE_DIR):
    os.makedirs(STORAGE_DIR)

def save_content_to_file(file_name: str, file_content: str):
    """Givem file name and store the content in the file.

    Args:
        text_to_store: file name.
        prompt: Content to be stored in the file.

    Returns:
        None
    """

    with open(file_name, "w") as f:
        f.write(f"Response:\n{file_content}")  # Store the response

client = genai.Client(api_key=userdata.get('GOOGLE_API_KEY'))
model_id = "gemini-2.0-flash"

save_content_to_file_tool = types.Tool(function_declarations=[
    types.FunctionDeclaration(
        name="save_content_to_file",
        description="The content generated in response to the prompt is stored to the file name provided.",
        parameters=types.Schema(
            properties={
                'file_name': types.Schema(type='STRING'),
                'file_content': types.Schema(type='STRING'),
            },
            type='OBJECT',
        ),
    )
])

google_search_tool = types.Tool(
    google_search = types.GoogleSearch()
)

response = client.models.generate_content(
    model=model_id,
    contents="What is reinforcement learning? store the content into reinforcement_learning.txt",
    config=types.GenerateContentConfig(
        tools=[save_content_to_file_tool, google_search_tool],
        tool_config={

        }
    )
)

It seems like sometimes I am as well getting malformed function call. Let me check again and dig some more about it. Sorry in the delayed response and thanks for reporting this.

1 Like