Skip to content

gemini-2.5-flash: finish_reason=MALFORMED_FUNCTION_CALL when using streaming + tool definitions + dynamic thinking (no explicit thinking_config) #2081

@bhakta0007

Description

@bhakta0007

Environment details

  • Programming language: Python
  • OS: macOS 26.2
  • Language runtime version: Python 3.12.8
  • Package version: google-genai==1.57.0

Steps to reproduce

  1. Setup a new venv with python3, pip install google-genai==1.57.0
  2. Run the attached script with: python3 gemini_malformed_reproducer.py --path1 --runs 20 (try with more runs if you cannot repro with 20)

Repro logs:

<snip>
======================================================================
[Run 7/20]  PATH 2: Native Google GenAI SDK — streaming + tools + thinking + MALFORMED retry
======================================================================
  model          : gemini-2.5-flash
  thinking_budget: 8192
  query          : 'give me a line chart of vulnerabilities over the last 4 weeks'

Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.
Attempt 1 — dynamic thinking (no explicit budget, matches Torana):
--------------------------------------------------
  [chunk   1]  finish_reason = 'MALFORMED_FUNCTION_CALL'
--------------------------------------------------
  chunks received : 1
  finish_reason   : 'MALFORMED_FUNCTION_CALL'
  tool calls      : 0

⚠️  MALFORMED_FUNCTION_CALL detected on attempt 1
   → Retrying with thinking_budget=0 (CachingGemini workaround)

Attempt 2 — retry with thinking_budget=0:
--------------------------------------------------
  [chunk   1]  finish_reason = 'STOP'
  [chunk   1]  tool_call = 'ui_line_chart'
--------------------------------------------------
  chunks received : 1
  finish_reason   : 'STOP'
  tool calls      : 1

✅  RETRY SUCCEEDED: 'ui_line_chart' called with 5 top-level args
   labels   : ['Week 1', 'Week 2', 'Week 3', 'Week 4']
   datasets : 4 series
     - 'Critical': [120, 110, 90, 80]
     - 'High': [250, 220, 180, 150]
     - 'Medium': [400, 380, 350, 320]
     - 'Low': [600, 550, 500, 450]
   title    : 'Vulnerabilities Over Last 4 Weeks'

  [fail-fast] Bug reproduced — stopping. Use --no-fail-fast to continue.

======================================================================
STATISTICS  (0 runs)
======================================================================

  PATH 2 — Native SDK (with retry):
    Attempt 1 MALFORMED      :   1 / 7  (14%)
      → retry succeeded      :   1 / 1  (100%)
    Success on attempt 1     :   6 / 7  (86%)
    Overall success          :   7 / 7  (100%)

Note: i have also raised a bug with litellm to handle this scenario better: BerriAI/litellm#21744

gemini_malformed_reproducer.py

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions