Skip to content

fix(gemini): group parallel function responses into one turn - #79

Closed
HamzaKhanBUIC wants to merge 1 commit into
getdownandcode:mainfrom
HamzaKhanBUIC:fix/gemini-parallel-function-calls
Closed

HamzaKhanBUIC wants to merge 1 commit into
getdownandcode:mainfrom
HamzaKhanBUIC:fix/gemini-parallel-function-calls

Conversation

@HamzaKhanBUIC

Copy link
Copy Markdown

Summary

Fixes #62.

Resolves an issue where Google Gemini parallel function calls caused HTTP 400 Bad Request errors on subsequent model turns.

Problem

  1. When Gemini emitted multiple parallel function calls in a single candidate turn, GeminiClient._convert_messages mapped each tool result into its own ypes.Content(role=user) container. The Gemini API requires all parallel function responses to be bundled together into a single role=user turn with one unction_response part per function call.
  2. Inbound FunctionCall.id was discarded in favor of a randomly generated uuid.uuid4(), and outbound unction_response.id was omitted, causing call-result join failure.

Solution

  • Grouping Parallel Tool Responses: Updated GeminiClient._convert_messages so that consecutive Role.TOOL messages following an assistant turn are grouped into a single ypes.Content(role=user) turn.
  • Tool ID Preservation:
    • Outbound: Sets ool_part.function_response.id = msg.tool_call_id.
    • Inbound: Preserves c.id when provided by the API as a string, with UUID fallback.
  • Regression Tests: Added est_gemini_parallel_function_calls_grouped_and_preserve_ids in ests/unit/test_gemini_adapter.py.

Verification

  • uv run pytest tests/unit/test_gemini_adapter.py -> 6/6 passed
  • uv run ruff check . -> All checks passed
  • uv run ruff format --check . -> 110 files already formatted
  • uv run mypy agentkit -> Success: no issues found in 42 source files
  • Structural AST lint & Semgrep security check -> Clean

Copilot AI balanced review requested due to automatic review settings October 20, 2026 21:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@HamzaKhanBUIC
HamzaKhanBUIC deleted the fix/gemini-parallel-function-calls branch October 21, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AUDIT-05 — High — Gemini parallel function calls are broken

2 participants