From 4700816e636c777e7933bd92de5af91ce909be08 Mon Sep 17 00:00:00 2001 From: sama Pyb Date: Tue, 1 Dec 2026 22:04:37 +0800 Subject: [PATCH] Omit empty model message when replaying stopped Gemini streams --- .../Models/GeminiLanguageModel.swift | 9 ++++- .../GeminiAssistantHistoryTests.swift | 39 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift b/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift index 3afa199a..fa4029b9 100644 --- a/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/GeminiLanguageModel.swift @@ -552,7 +552,14 @@ public struct GeminiLanguageModel: LanguageModel, ProfileRequestIdentifiedLangua switch resolution { case .stop: state.entries.append(.toolCalls(callsEntry)) - continuation.yield(try state.stoppedSnapshot(provider: "Gemini")) + continuation.yield( + try state.stoppedSnapshot( + provider: "Gemini", + providerMetadata: state.responseText.isEmpty + ? stoppedResponseContentMetadata + : nil + ) + ) continuation.finish() return case .invocations(let invocations): diff --git a/Tests/AnyLanguageModelTests/GeminiAssistantHistoryTests.swift b/Tests/AnyLanguageModelTests/GeminiAssistantHistoryTests.swift index 2cdfe320..933e327c 100644 --- a/Tests/AnyLanguageModelTests/GeminiAssistantHistoryTests.swift +++ b/Tests/AnyLanguageModelTests/GeminiAssistantHistoryTests.swift @@ -281,6 +281,45 @@ struct GeminiAssistantHistoryTests { ) } + @Test(arguments: [false, true]) + func aStoppedToolRoundWithoutTextOmitsEmptyModelMessage(streaming: Bool) async throws { + let transport = FakeTransport(replies: [ + try Self.reply(parts: [Self.call], streaming: streaming), + try Self.reply(parts: [Self.text("Bye.")], streaming: streaming), + ]) + let session = LanguageModelSession(model: Self.model(transport), tools: [WeatherTool()]) + session.toolExecutionDelegate = StoppingGeminiDelegate() + _ = try await Self.ask(session, "Weather?", streaming: streaming) + _ = try await Self.ask(session, "Thanks", streaming: streaming) + + #expect( + try Self.labels(transport.bodies[1]) == [ + "user: Weather?", "call #call-signature", "output", "user: Thanks", + ] + ) + } + + @Test(arguments: [false, true]) + func aStoppedToolRoundWithSignedThoughtOmitsEmptyModelMessage(streaming: Bool) async throws { + let transport = FakeTransport(replies: [ + try Self.reply( + parts: [Self.thought("THOUGHT", signature: "thought-signature"), Self.call], + streaming: streaming + ), + try Self.reply(parts: [Self.text("Bye.")], streaming: streaming), + ]) + let session = LanguageModelSession(model: Self.model(transport), tools: [WeatherTool()]) + session.toolExecutionDelegate = StoppingGeminiDelegate() + _ = try await Self.ask(session, "Weather?", streaming: streaming) + _ = try await Self.ask(session, "Thanks", streaming: streaming) + + #expect( + try Self.labels(transport.bodies[1]) == [ + "user: Weather?", "model: #thought-signature", "call #call-signature", "output", "user: Thanks", + ] + ) + } + /// A stopped stream's response is the round's text. The other builders can't read Gemini's /// text parts, so they send that response, once. @Test