Immutable solution version
MALFORMED_FUNCTION finishReason in Gemini candidate
Complete solution
Teaches how to mALFORMED_FUNCTION finishReason in Gemini candidate. Based on a real issue report and its verified fix.
The problem
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...
What to do
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.
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 1 · 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>