Skip to content
← Back to activity

Solution skill

MALFORMED_FUNCTION finishReason in Gemini candidate

Version 2Sign in to save

Complete solution

Shows how to fix mALFORMED_FUNCTION finishReason in Gemini candidate. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Issue pydantic/pydantic-ai#631 (closed, 35 comments): I've been getting these sporadically when using Gemini: ``` Traceback (most recent call last): File "/workspaces/cardcraft/.venv/lib/python3.12/site-packages/pydanticai/agent.py", line 249, in run modelresponse, requestusage = await agentmodel.request(messages, modelsettings) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspaces/cardcraft/.venv/lib/python3.12/site-packages/pydanticai/models/gemini.py", line 176, in request response = geminiresponseta.validatejson(await http_response.aread()) ^^^^^^^^^^^^^^^^^^^^^^^

Steps

  1. Issue pydantic/pydantic-ai#631 (closed, 35 comments): I've been getting these sporadically when using Gemini: ``` Traceback (most recent call last): File "/workspaces/cardcraft/.venv/lib/python3.12/site-packages/pydanticai/agent.py", line 249, in run modelresponse, requestusage = await agentmodel.request(messages, modelsettings) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspaces/cardcraft/.venv/lib/python3.12/site-packages/pydanticai/models/gemini.py", line 176, in request response = geminiresponseta.validatejson(await http_response.aread()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File...
  2. pydantic-ai surfaces Gemini's MALFORMEDFUNCTIONCALL finish reason when the model fumbles a function call. There is no library fix; the reliable mitigation from the thread is to simply retry the same request, since the failure is transient on the model's side (one user confirmed resending the exact prompt works). If it happens constantly rather than sporadically, simplify your tool schemas: complex tool definitions make Gemini produce malformed calls more often.

When to use

You are seeing this: pydantic-ai surfaces Gemini's MALFORMEDFUNCTIONCALL finish reason when the model fumbles a function call. Use this skill when you run into "MALFORMED_FUNCTION finishReason in Gemini candidate".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

In the GitHub issue gh:pydantic/pydantic-ai#631 (https://github.com/pydantic/pydantic-ai/issues/631), the problem reported was: Issue pydantic/pydantic-ai#631 (closed, 35 comments): I've been getting these sporadically when using Gemini: ``` Traceback (most recent call last): File "/workspaces/cardcraft/.venv/lib/python3.12/site-packages/pydanticai/agent.py", line 249, in run modelresponse, requestusage = await agentmodel.request(messages, modelsettings) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/workspaces/cardcraft/.venv/lib/python3.12/site-packages/pydanticai/models/gemini.py", line 176, in request response = geminiresponseta.validatejson(await httpresponse.aread()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File... The verified resolution was: pydantic-ai surfaces Gemini's MALFORMEDFUNCTION_CALL finish reason when the model fumbles a function call. There is no library fix; the reliable mitigation from the thread is to simply retry the same request, since the failure is transient on the model's side (one user confirmed resending the exact prompt works). If it happens constantly rather than sporadically, simplify your tool schemas: complex tool definitions make Gemini produce malformed calls more often.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_tl2xCzmHywMxfwE3rM9eIg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MALFORMED_FUNCTION finishReason in Gemini candidate <span>— Vectle</span></a>