Skip to content

Commit f097600

Browse files
authored
fix(gemini): keep narration streamed before a tool call in history (#10132)
The streaming branch built the final LLMResponse from the tool-call chunk\nalone and returned immediately, so assistant text and reasoning that had\nalready been streamed to the user were dropped and never written to the\nconversation history. Merge the accumulated text/reasoning into that final\nresponse before returning. Fixes #10105
1 parent 3c149fe commit f097600

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎astrbot/core/provider/sources/gemini_source.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,22 @@ async def _query_stream(
776776
llm_response,
777777
validate_output=False,
778778
)
779+
# This response replaces the whole turn in conversation
780+
# history, so keep the narration and reasoning that were
781+
# already streamed before the tool call. Dropping them made
782+
# the user-visible text missing from history.
783+
if accumulated_text or accumulated_reasoning:
784+
parts = list(llm_response.result_chain.chain or [])
785+
if accumulated_text:
786+
parts.insert(0, Comp.Plain(accumulated_text))
787+
llm_response.result_chain = MessageChain(chain=parts)
788+
if accumulated_reasoning:
789+
# _process_content_parts already stored the reasoning
790+
# that came with the tool-call chunk itself, so append
791+
# to it instead of overwriting that part.
792+
llm_response.reasoning_content = accumulated_reasoning + (
793+
llm_response.reasoning_content or ""
794+
)
779795
llm_response.id = chunk.response_id
780796
if chunk.usage_metadata:
781797
llm_response.usage = self._extract_usage(chunk.usage_metadata)

‎tests/test_gemini_source.py‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pytest
55
from google.genai import types
66

7+
import astrbot.core.message.components as Comp
8+
import astrbot.core.provider.sources.gemini_source as gemini_source
79
import astrbot.core.provider.sources.request_retry as request_retry
810
from astrbot.core.exceptions import EmptyModelOutputError
911
from astrbot.core.provider.entities import LLMResponse
@@ -236,3 +238,135 @@ async def list(self):
236238

237239
assert await provider.get_models() == ["gemini-a"]
238240
assert models.calls == 2
241+
242+
243+
def _gemini_part(*, text=None, thought=None, function_call=None):
244+
return SimpleNamespace(
245+
text=text,
246+
thought=thought,
247+
function_call=function_call,
248+
inline_data=None,
249+
thought_signature=None,
250+
)
251+
252+
253+
def _gemini_stream_chunk(
254+
*,
255+
text=None,
256+
thought=None,
257+
function_call=None,
258+
finish_reason=None,
259+
response_id="resp-1",
260+
):
261+
"""Build a minimal stand-in for a google-genai streaming chunk."""
262+
parts = []
263+
if text is not None:
264+
parts.append(_gemini_part(text=text))
265+
if thought is not None:
266+
parts.append(_gemini_part(text=thought, thought=True))
267+
if function_call is not None:
268+
parts.append(_gemini_part(function_call=function_call))
269+
return SimpleNamespace(
270+
candidates=[
271+
SimpleNamespace(
272+
content=SimpleNamespace(parts=parts),
273+
finish_reason=finish_reason,
274+
)
275+
],
276+
text=text,
277+
response_id=response_id,
278+
usage_metadata=None,
279+
)
280+
281+
282+
def _gemini_stream_provider():
283+
provider = ProviderGoogleGenAI.__new__(ProviderGoogleGenAI)
284+
provider.provider_config = {}
285+
provider.provider_settings = {}
286+
provider.model_name = "gemini-3.7-flash"
287+
provider.safety_settings = []
288+
provider.client = SimpleNamespace(
289+
models=SimpleNamespace(generate_content_stream=lambda **kwargs: None)
290+
)
291+
return provider
292+
293+
294+
async def _drain_gemini_stream(provider, monkeypatch, chunks):
295+
async def fake_stream():
296+
for chunk in chunks:
297+
yield chunk
298+
299+
async def fake_retry(provider_name, request_factory, max_attempts=None):
300+
return fake_stream()
301+
302+
monkeypatch.setattr(gemini_source, "retry_provider_request", fake_retry)
303+
return [
304+
response
305+
async for response in provider._query_stream(
306+
payloads={
307+
"messages": [{"role": "user", "content": "what's the weather?"}],
308+
"model": "gemini-3.7-flash",
309+
},
310+
tools=None,
311+
)
312+
]
313+
314+
315+
@pytest.mark.asyncio
316+
async def test_gemini_stream_keeps_narration_emitted_before_tool_call(monkeypatch):
317+
"""Narration streamed before a tool call must reach the final response.
318+
319+
Regression test for the tool-call branch that used to build the final
320+
LLMResponse from the tool-call chunk alone and return immediately, leaving
321+
text the user had already seen out of the conversation history.
322+
"""
323+
provider = _gemini_stream_provider()
324+
tool_call = SimpleNamespace(
325+
name="get_weather",
326+
args={"city": "Shenyang"},
327+
id="call-1",
328+
thought_signature=None,
329+
)
330+
331+
responses = await _drain_gemini_stream(
332+
provider,
333+
monkeypatch,
334+
[
335+
_gemini_stream_chunk(text="Sure, let me check that for you."),
336+
_gemini_stream_chunk(function_call=tool_call),
337+
],
338+
)
339+
340+
final = responses[-1]
341+
assert final.is_chunk is False
342+
assert final.tools_call_name == ["get_weather"]
343+
plain_text = "".join(
344+
part.text
345+
for part in (final.result_chain.chain or [])
346+
if isinstance(part, Comp.Plain)
347+
)
348+
assert "Sure, let me check that for you." in plain_text
349+
350+
351+
@pytest.mark.asyncio
352+
async def test_gemini_stream_keeps_reasoning_from_tool_call_chunk(monkeypatch):
353+
"""Reasoning on the tool-call chunk itself must not be overwritten."""
354+
provider = _gemini_stream_provider()
355+
tool_call = SimpleNamespace(
356+
name="get_weather",
357+
args={"city": "Shenyang"},
358+
id="call-1",
359+
thought_signature=None,
360+
)
361+
362+
responses = await _drain_gemini_stream(
363+
provider,
364+
monkeypatch,
365+
[
366+
_gemini_stream_chunk(thought="weighing options"),
367+
_gemini_stream_chunk(function_call=tool_call, thought="deciding to call"),
368+
],
369+
)
370+
371+
final = responses[-1]
372+
assert final.reasoning_content == "weighing optionsdeciding to call"

0 commit comments

Comments
 (0)