Skip to content

Coalesce consecutive Gemini function responses into one user content - #448

Open
Pybsama wants to merge 1 commit into
huggingface:mainfrom
Pybsama:coalesce-gemini-function-responses
Open

Pybsama wants to merge 1 commit into
huggingface:mainfrom
Pybsama:coalesce-gemini-function-responses

Conversation

@Pybsama

@Pybsama Pybsama commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Problem

Transcript.toGeminiContent() appended a separate role: "user" GeminiContent for every .toolOutput entry. Because GeminiLanguageModel builds every request through toGeminiContent() (both in-flight continuations and subsequent turns), whenever Gemini returns multiple parallel functionCall parts in one model turn, the next request sent multiple consecutive user contents with one functionResponse each instead of one user content containing all functionResponse parts.

Gemini's GenerateContent API requires all functionResponse parts for a parallel tool-call turn to be sent together in a single user content (otherwise returning HTTP 400: "Please ensure that the number of function response parts is equal to the number of function call parts of the function call turn").

Change

  • Coalesce consecutive .toolOutput entries in Transcript.toGeminiContent() (Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift) into a single GeminiContent(role: .user, parts: ...), flushing before any .prompt, .response, or .toolCalls entry and at the end of the transcript so follow-up or pending user prompts remain separate user contents.
  • Add cross-platform regression coverage in Tests/AnyLanguageModelTests/GeminiToolResponseTests.swift covering in-flight parallel tool continuation and multi-turn replay, DynamicProfile historyTransform editing a tool output, stopped parallel tool calls, and pending steering prompts (both non-streaming and streaming).
  • Update the expected contents count in GeminiThoughtSignatureTests.roundTripsMixedParts from [1, 4, 6] to [1, 3, 5] now that its two parallel getWeather responses share one user content.

Verification

  • swift format lint --strict --recursive . (Swift 6.3.3): PASS
  • swift test --disable-index-store --filter GeminiToolResponseTests: 4 tests (8 cases) failed with 30 issues on main → PASS with fix
  • swift test --disable-index-store --filter "GeminiToolResponseTests|GeminiAssistantHistoryTests|GeminiProfileTests|GeminiThoughtSignatureTests|StoppedToolCallReplayTests": 46/46 PASS across 5 suites
  • swift test --skip-build --disable-index-store --skip OllamaLanguageModelTests: 1,117/1,117 PASS across 115 suites

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.

1 participant