Skip to content

Commit 92d6da0

Browse files
author
Mateusz
committed
Treat empty Gemini streams only when no chunks produced
1 parent 5c8db68 commit 92d6da0

File tree

1 file changed

+3
-52
lines changed

1 file changed

+3
-52
lines changed

src/core/services/backend_request_manager_service.py

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -374,32 +374,13 @@ async def _process_streaming_response(
374374
)
375375

376376
prefetched_chunks: list[ProcessedResponse | bytes] = []
377-
has_text = False
378-
has_tool_calls = False
379-
stream_consumed_without_data = False
380377

381378
async for chunk in original_stream:
382379
prefetched_chunks.append(chunk)
383-
if not has_text:
384-
text_fragment = self._extract_text_from_chunk(chunk)
385-
if text_fragment and text_fragment.strip():
386-
has_text = True
387-
if not has_tool_calls and self._chunk_contains_tool_call(chunk):
388-
has_tool_calls = True
389-
if has_text or has_tool_calls:
390-
break
380+
break
391381
else:
392-
stream_consumed_without_data = True
393-
394-
if stream_consumed_without_data and not has_tool_calls:
395-
return await self._retry_stream_with_recovery(
396-
reason="Streaming response contained no text or tool calls; retrying with recovery prompt.",
397-
stream_envelope=stream_envelope,
398-
original_request=original_request,
399-
session_id=session_id,
400-
context=context,
401-
retry_depth=retry_depth,
402-
)
382+
# Generator produced no data at all
383+
pass
403384

404385
if not prefetched_chunks:
405386
return await self._retry_stream_with_recovery(
@@ -555,36 +536,6 @@ def _create_loop_detector(self) -> ILoopDetector:
555536
fallback.reset()
556537
return fallback
557538

558-
@staticmethod
559-
def _chunk_contains_tool_call(chunk: ProcessedResponse | bytes) -> bool:
560-
import json
561-
562-
if isinstance(chunk, ProcessedResponse):
563-
if chunk.metadata and chunk.metadata.get("tool_calls"):
564-
return True
565-
candidate = chunk.content
566-
elif isinstance(chunk, bytes):
567-
try:
568-
decoded = chunk.decode("utf-8")
569-
candidate = json.loads(decoded)
570-
except (UnicodeDecodeError, json.JSONDecodeError):
571-
return False
572-
else:
573-
candidate = None
574-
575-
if isinstance(candidate, dict):
576-
choices = candidate.get("choices")
577-
if isinstance(choices, list) and choices:
578-
choice = choices[0]
579-
if isinstance(choice, dict):
580-
delta = choice.get("delta")
581-
if isinstance(delta, dict) and delta.get("tool_calls"):
582-
return True
583-
message = choice.get("message")
584-
if isinstance(message, dict) and message.get("tool_calls"):
585-
return True
586-
return False
587-
588539
@staticmethod
589540
def _extract_text_from_chunk(chunk: ProcessedResponse | bytes) -> str:
590541
"""Extract textual content from a streaming chunk for loop analysis."""

0 commit comments

Comments
 (0)