adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Random "MALFORMED_FUNCTION_CALL" Error from Gemini Agent

Open onesnzerossc opened this issue 1 year ago • 29 comments

Describe the bug Occasionally, when running the agent, I encounter a response with the following error: Raw response: {"candidates":[{"content":{},"finish_reason":"MALFORMED_FUNCTION_CALL"}],"model_version":"gemini-2.0-flash","usage_metadata":{"prompt_token_count":2931,"prompt_tokens_details":[{"modality":"TEXT","token_count":2931}],"total_token_count":2931},"automatic_function_calling_history":[]}

To Reproduce Steps to reproduce the behavior: The error appears randomly during agent execution. Though there is no pattern to this, I've noticed this issue when you go out of sequence and ask questions to the root agent.

Expected behavior I expect the agent to return a valid and complete response, or at least provide a clearer error message with function its trying to call and guidance on how to resolve the issue.

Screenshots

Image

Desktop (please complete the following information):

  • OS: Ubuntu 24.04.2 LTS
  • Python version(python -V): 3.13.3
  • ADK version(pip show google-adk): 1.1.1

Additional context This issue kind of breaks the orchestration flow of the Root Agent.

onesnzerossc avatar Jun 19 '25 15:06 onesnzerossc

I have same isssue when add to my agents build-in google_search tool. Do you have one?

NikitaZemtsov avatar Jun 20 '25 09:06 NikitaZemtsov

@NikitaZemtsov As mentioned, this error is is very random. In my situation, I have multiple custom tools made available to the agent. The error message is very generic, can't figure out which tool the agent is trying to call. Saying so, based on the context and I can assume the tool, but in other instances the function call to that tool just works fine. So, am little confused.

onesnzerossc avatar Jun 20 '25 11:06 onesnzerossc

I'm seeing something similar and it is also intermittent. In this case it seems to be caused by the sysml parameter string somehow being encoded wrong. There's a newline in there that isn't encoded as a \n. (the bolded line below)

ERROR: Malformed function call: print(default_api.save_new_project_from_text(project_name = "Drone Requirements 2", sysml = "private import ScalarValues::;\nprivate import ISQ::;\nprivate import SI::;\npackage Definitions {\n part def DronePart {\n comment / A part that can be used to build a drone /\n attribute name : ScalarValues::String;\n attribute weight :> ISQBase::mass;\n attribute price : ScalarValues::Real;\n }\n\n part def Drone {\n comment / Defines all the parts that make up a drone /\n part assembly {\n part assemblyFrame : DronePart;\n part fasteners : DronePart;\n }\n \n part propulsionSystem {\n part motor : DronePart;\n part propellers : DronePart;\n part esc : DronePart;\n }\n \n part powerSystem {\n part battery : DronePart;\n part mount : DronePart;\n }\n \n part controlSystem {\n part flightController : DronePart;\n part gps : DronePart;\n part radio : DronePart;\n part windSensor : DronePart;\n }\n \n part cameraSystem {\n part camera : DronePart;\n part mount : DronePart;\n }\n \n part landingGear {\n part gear : DronePart;\n }\n \n }\n\n}\n\npackage Parts {\n comment / List of parts available to build a drone /\n private import Definitions::DronePart;\n part xFrame : Definitions::DronePart {\n :>> name = "Carbon Fiber X-Frame";\n :>> weight = 8.8185 [oz];\n :>> price = 45;\n }\n\n part carbonFiber10 : Definitions::DronePart {\n :>> name = "10" Carbon Fiber";\n :>> weight = 0.70548 [oz];\n :>> price = 12;\n }\n\n part f40 : Definitions::DronePart {\n :>> name = "T-Motor F40 Pro III";\n :>> weight = 1.23459 [oz];\n :>> price = 30;\n }\n\n part f60 : Definitions::DronePart {\n :>> name = "T-Motor F60 Pro V";\n :>> weight = 1.164042 [oz];\n :>> price = 40;\n }\n\n part xRotor : Definitions::DronePart {\n :>> name = "Hobbywing XRotor 40A";\n :>> weight = 0.70548 [oz];\n :>> price = 25;\n }\n\n part pixhawk : Definitions::DronePart {\n :>> name = "Pixhawk 4";\n :>> weight = 3.17466 [oz];\n :>> price = 110;\n }\n\n part lipo5200 : Definitions::DronePart {\n :>> name = "LiPo 5200mAh 4S";\n :>> weight = 12.3459 [oz];\n :>> price = 70;\n }\n\n part lipo6000 : Definitions::DronePart {\n :>> name = "LiPo 6000mAh 4S";\n :>> weight = 14.81508 [oz];\n :>> price = 85;\n }\n\n part runcam : Definitions::DronePart {\n :>> name = "Runcam Split 4K";\n :>> weight = 0.52911 [oz];\n :>> price = 60;\n }\n\n part ublox : Definitions::DronePart {\n :>> name = "Ublox Neo-M8N";\n :>> weight = 1.05822 [oz];\n :>> price = 35;\n }\n\n part frsky : Definitions::DronePart {\n :>> name = "FrSky Taranis X9D + R-XSR";\n :>> weight = 3.5274 [oz];\n :>> price = 200;\n }\n\n part feiyutech : Definitions::DronePart {\n :>> name = "FeiyuTech Mini 3-Axis";\n :>> weight = 5.2911 [oz];\n :>> price = 150;\n }\n\n part lightweight : Definitions::DronePart {\n :>> name = "Lightweight Foldable";\n :>> weight = 1.7637 [oz];\n :>> price = 15;\n }\n\n part reinforced : Definitions::DronePart {\n :>> name = "Reinforced Foldable";\n :>> weight = 2.11644 [oz];\n :>> price = 20;\n }\n\n part anemometer : Definitions::DronePart {\n :>> name = "Anemometer Module";\n :>> weight = 0.70548 [oz];\n :>> price = 25;\n }\n\n part hobbywing : Definitions::DronePart {\n :>> name = "Hobbywing LCD Programmer";\n :>> weight = 4.23288 [oz];\n :>> price = 35;\n }\n\n part screws : Definitions::DronePart {\n :>> name = "Screws and Fasteners";\n :>> weight = 0.070548 [oz];\n :>> price = 5;\n }\n\n part batteryMount : Definitions::DronePart {\n :>> name = "Battery Strap and Mount";\n :>> weight = 0.070548 [oz]; :>> price = 10;\n }\n\n}\n\npackage Drone2 {\n private import Requirements::;\n private import Definitions::;\n private import Parts::;\n part drone2 : Definitions::Drone {\n :>> assembly {\n :>> assemblyFrame = xFrame;\n :>> fasteners = screws;\n }\n \n :>> propulsionSystem {\n :>> motor = f60;\n :>> propellers = carbonFiber10;\n :>> esc = xRotor;\n }\n \n :>> powerSystem {\n :>> battery = lipo6000;\n :>> mount = batteryMount;\n }\n \n :>> controlSystem {\n :>> flightController = pixhawk;\n :>> gps = ublox;\n :>> radio = frsky;\n :>> windSensor = anemometer;\n }\n \n :>> cameraSystem {\n :>> camera = runcam;\n :>> mount = screws;\n }\n \n :>> landingGear {\n :>> gear = reinforced;\n }\n \n }\n\n}\n\npackage Requirements {\n private import Definitions::;\n requirement stability {\n doc / The drone must be able to maintain steady flight and camera stabilization in winds up to 35 km/h using an integrated gimbal system. /\n subject drone : Definitions::Drone;\n require constraint;\n require constraint;\n }\n\n requirement weight {\n doc / The total weight of the drone, including the battery and gimbal system, must be kept under 2.645544 pounds to balance stability and maneuverability. /\n subject drone : Definitions::Drone;\n attribute totalWeight = ;\n require constraint;\n }\n\n requirement materials {\n doc / The frame and landing gear must be made of reinforced carbon fiber to withstand impacts and turbulence without adding excessive weight. /\n subject drone : Definitions::Drone;\n require constraint;\n require constraint;\n }\n\n requirement speed {\n doc / The drone must reach a minimum speed of 25 km/h, allowing it to maneuver effectively against strong wind resistance. /\n subject drone : Definitions::Drone;\n require constraint;\n }\n\n requirement sustainability {\n doc / The drone's power system must include a battery with a lifespan of at least 250 charge cycles, using eco-friendly materials. /\n subject drone : Definitions::Drone;\n require constraint;\n }\n\n requirement windResistance {\n doc / The propulsion and control systems must be calibrated to maintain stability and control during gusts of up to 40 km/h without loss of altitude or direction. */\n subject drone : Definitions::Drone;\n require constraint;\n require constraint;\n require constraint;\n }\n\n}\n"))

halfpasttense avatar Jun 23 '25 18:06 halfpasttense

Further research suggest these may be caused by:

https://github.com/googleapis/python-aiplatform/issues/4472

(at least for my specific error)

halfpasttense avatar Jun 23 '25 20:06 halfpasttense

Could you share the frequency of this issue?

Could you share the model(id or name) you used?

Need to collect some data point to figure out which model have this problem.

hangfei avatar Jun 24 '25 20:06 hangfei

I'm using "gemini-2.5-flash". I would say that when passing long complex multiline strings such as the example above as function tool parameters, it fails like this more often than not.

halfpasttense avatar Jun 24 '25 22:06 halfpasttense

Any Solution??? Having the same Issue :

using gemini 2.5 pro

veevortexiq avatar Jun 25 '25 18:06 veevortexiq

@hangfei Am using the model "gemini-2.0-flash". Though the occurrence was random I noticed the error only when I post a specific question to my root_agent.

This my setup. All the sub agents are linked to the root agent via agent tool. I used to get this error only when the root agent tries to calls sub agent2 and only for a specific question. root_agent

  • sub agent1
  • sub agent2
  • sub agent3

onesnzerossc avatar Jun 25 '25 23:06 onesnzerossc

https://github.com/googleapis/python-aiplatform/issues/4472

I read through this thread, posted by @halfpasttense

Cause :

The issue is, when llm generates content anything other than string, like json, html, python (in my case i was generating code) it adds this ``` python ```

If it's a JSON, it'll be ``` json ```

The above thread suggest that, you can't have function / tool call, if the llm generates content with these formattors (but the wierd thing is, the error happens randomly, atleast for me)

Workaround :

I'm using Litellm for a different model provider and everything works as it should be.

Apollovishwas avatar Jun 26 '25 08:06 Apollovishwas

This happens to me when querying tables from supabase

jmdelatorre avatar Jun 29 '25 19:06 jmdelatorre

As an agent shouldn't the framework retry automatically providing the schema again and the bad response as well may be and try to regenerate the function params?

sdivakarrajesh avatar Jul 01 '25 04:07 sdivakarrajesh

happening to me as well, but using typescript

I have also been seeing this error randomly on gemini-2.5-flash. I now have retry logic (which should be unnecessary) and after a few retries it randomly works again.

sumitg avatar Jul 02 '25 15:07 sumitg

Happened to us to a couple of days ago

ValerioNeriGit avatar Jul 03 '25 12:07 ValerioNeriGit

I have also been seeing this error randomly on gemini-2.5-flash. I now have retry logic (which should be unnecessary) and after a few retries it randomly works again.

may I ask how did you implement the retry logic?

This issue is very serious. Based on my experience, it occurs more frequently when calling complex functions. In the case of agents implemented with simple logic, one might think the problem can be resolved by simply re-executing. However, in my case, the structure is complex, with multiple agents combined. Therefore, even if a malformed error occurs and I retry, it inevitably leads to a loss of considerable time and cost. According to search results, malformed errors have been occurring for over two years. Considering that such issues do not appear in GPT or Claude, it seems likely that Google could also resolve this problem, yet it remains unresolved, which is questionable. In fact, Gemini still has many old bugs remaining besides malformed errors, but it appears that there has been no effort to fix them over a long period. Astonishingly, the Gemini forum has been responding to malformed error reports for two years with the simple suggestion: “Use a higher model.”

wltnrhkd123 avatar Jul 08 '25 23:07 wltnrhkd123

Additionally, this issue causes another error for users who use Vertex AI together with mysqldb. When attempting to store the malformed error in a table, the following error occurs: sqlalchemy.exc.DataError: (pymysql.err.DataError) (1406, "Data too long for column 'error_message' at row 1") Although it could potentially be resolved by simply adjusting the configuration of the error_message field, the fact that an error that shouldn't occur in the first place is happening—and then causing another error because it can't be stored in the database—is highly problematic.

I have been committed to building services based on the Google ADK since version 0.1.0. From my extended experience using Google ADK, it is clear that many bugs, which should have been discovered during development, are still present to this day. Why is that? It’s hard to believe these issues were not discovered during the verification process, especially considering this error occurs with all tools—even Google’s own default tools.

I sincerely hope the Google ADK developers will pay closer attention to the errors that arise during the testing process.

wltnrhkd123 avatar Jul 09 '25 00:07 wltnrhkd123

MALFORMED_FUNCTION_CALL is something returned by Model API server. It's not something that can be controlled by ADK.

seanzhou1023 avatar Jul 11 '25 00:07 seanzhou1023

You are right that it’s the problem with the underlying LLM. But as an agent framework, the framework should try to recover from this error by having mechanisms as that’s the core idea - agent taking decisions and driving the flow of actions. One bad response shouldn’t cause the “agent” to crash

sdivakarrajesh avatar Jul 11 '25 06:07 sdivakarrajesh

makes sense. I think we can probably discard such response and resend the last request. but It's hard for streaming response given there were already partial response event yielded.

On the other hand we already has bug filed against model server.

seanzhou1023 avatar Jul 12 '25 20:07 seanzhou1023