Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Explain gemini tool loop

Abi Raja8deecce0ceae
diff --git a/backend/agent/providers/gemini/adapter.py b/backend/agent/providers/gemini/adapter.py
index 612cbd41..3a2376c4 100644
--- a/backend/agent/providers/gemini/adapter.py
+++ b/backend/agent/providers/gemini/adapter.py
@@ -1,5 +1,5 @@
# pyright: reportUnknownVariableType=false
-from typing import Any, Dict, List, cast
+from typing import List
from google import genai
from google.genai import types
@@ -60,17 +60,13 @@ class GeminiAdapter:
step_result: StepResult,
executed_tool_calls: list[ExecutedToolCall],
) -> None:
- function_call_parts = cast(
- List[types.Part],
- ((step_result.provider_state or {}).get("function_call_parts") or []),
- )
-
- model_parts: List[Any] = []
- if step_result.assistant_text:
- model_parts.append({"text": step_result.assistant_text}) # type: ignore
- model_parts.extend(function_call_parts)
+ model_content = (step_result.provider_state or {}).get("model_content")
+ if not isinstance(model_content, types.Content) or not model_content.parts:
+ raise ValueError(
+ "Gemini step is missing model_content. Cannot append tool results without the original model turn."
+ )
- self._contents.append(types.Content(role="model", parts=model_parts))
+ self._contents.append(model_content)
tool_result_parts: List[types.Part] = []
for executed in executed_tool_calls:
diff --git a/backend/agent/providers/gemini/parser.py b/backend/agent/providers/gemini/parser.py
index 041a16b7..45e8733f 100644
--- a/backend/agent/providers/gemini/parser.py
+++ b/backend/agent/providers/gemini/parser.py
@@ -12,7 +12,8 @@ from agent.tools import ToolCall
class GeminiParseState:
assistant_text: str = ""
tool_calls: List[ToolCall] = field(default_factory=list)
- function_call_parts: List[types.Part] = field(default_factory=list)
+ model_parts: List[types.Part] = field(default_factory=list)
+ model_role: str = "model"
async def parse_chunk(
@@ -27,13 +28,19 @@ async def parse_chunk(
if not candidate_content or not candidate_content.parts:
return
+ if candidate_content.role:
+ state.model_role = candidate_content.role
+
for part in candidate_content.parts:
+ # Preserve each model part as streamed so thought signatures remain
+ # attached to their original parts for the next turn.
+ state.model_parts.append(part)
+
if getattr(part, "thought", False) and part.text:
await on_event(StreamEvent(type="thinking_delta", text=part.text))
continue
if part.function_call:
- state.function_call_parts.append(part)
args = part.function_call.args or {}
tool_id = part.function_call.id or f"tool-{uuid.uuid4().hex[:6]}"
tool_name = part.function_call.name or "unknown_tool"
@@ -62,8 +69,13 @@ async def parse_chunk(
def build_step_result(state: GeminiParseState) -> StepResult:
+ model_content = (
+ types.Content(role=state.model_role, parts=state.model_parts)
+ if state.model_parts
+ else None
+ )
return StepResult(
assistant_text=state.assistant_text,
tool_calls=state.tool_calls,
- provider_state={"function_call_parts": state.function_call_parts},
+ provider_state={"model_content": model_content},
)