Commit 892056a

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-07-08 13:13:05
fix(gemini): ensure params consistency when retrying
1 parent 750d4ff
Changed files (1)
src
src/llm/gemini.py
@@ -160,8 +160,8 @@ async def gemini_stream(
         http_options = hook_gemini_httpoptions(http_options, message)
         app = genai.Client(api_key=api_key, http_options=http_options)
         # Construct the request params
-        if "contents" not in params and "conversations" in params:  # convert conversations to contents
-            params["contents"] = await get_conversation_contexts(client, params.pop("conversations"), ctx_format="gemini", app=app)
+        if "conversations" in params:  # convert conversations to contents
+            params["contents"] = await get_conversation_contexts(client, params["conversations"], ctx_format="gemini", app=app)
         gemini_logging(params["contents"])
         tokens = await app.aio.models.count_tokens(model=params["model"], contents=params["contents"])  # type: ignore
         num_tokens = tokens.total_tokens or 0
@@ -172,7 +172,8 @@ async def gemini_stream(
         sent_messages = []
         is_reasoning = False
         is_reasoning_conversation = None  # to indicate whether it is a reasoning conversation
-        async for chunk in await app.aio.models.generate_content_stream(**params):
+        genai_params = {"model": params["model"], "contents": params["contents"], "config": params["config"]}
+        async for chunk in await app.aio.models.generate_content_stream(**genai_params):
             resp = parse_response(chunk.model_dump())
             answer = resp.get("texts", "")
             thinking = resp.get("thinking", "")
@@ -296,7 +297,7 @@ async def gemini_nonstream(
         http_options = hook_gemini_httpoptions(http_options, message)
         app = genai.Client(api_key=api_key, http_options=http_options)
         # Construct the request params
-        if "contents" not in params and "conversations" in params:  # convert conversations to contents
+        if "conversations" in params:  # convert conversations to contents
             params["contents"] = await get_conversation_contexts(client, params.pop("conversations"), ctx_format="gemini", app=app)
         if clean_marks:
             clean_gemini_sourcemarks(params["contents"])
@@ -307,7 +308,8 @@ async def gemini_nonstream(
             params["model"] = GEMINI.TEXT_TOKENS_FALLBACK_MODEL
             params["config"].thinking_config = None
             params["config"].response_modalities = ["TEXT"]
-        response = await app.aio.models.generate_content(**params)
+        genai_params = {"model": params["model"], "contents": params["contents"], "config": params["config"]}
+        response = await app.aio.models.generate_content(**genai_params)
         prefix = f"🤖**{model_name}**:{BOT_TIPS}\n"
         res = parse_response(response.model_dump())
         texts = res.get("texts", "")