From 1e0275ee08fb0f4734085a01af25bee3e3d9ae0e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 30 Jan 2026 15:53:40 +0000 Subject: [PATCH] Improve code clarity and add documentation about in-place modification Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com> --- .../pkg/provider/modelmgr/requesters/geminichatcmpl.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/langbot/pkg/provider/modelmgr/requesters/geminichatcmpl.py b/src/langbot/pkg/provider/modelmgr/requesters/geminichatcmpl.py index 7fe70e1d..bb1c721d 100644 --- a/src/langbot/pkg/provider/modelmgr/requesters/geminichatcmpl.py +++ b/src/langbot/pkg/provider/modelmgr/requesters/geminichatcmpl.py @@ -25,6 +25,8 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions): Gemini API requires a thought_signature field in function call parts. See: https://ai.google.dev/gemini-api/docs/thought-signatures + + Note: This function modifies the dictionaries in the messages list in place. """ for msg in messages: if 'tool_calls' in msg and msg['tool_calls']: @@ -170,14 +172,16 @@ class GeminiChatCompletions(chatcmpl.OpenAIChatCompletions): remove_think: bool = False, ) -> tuple[provider_message.Message, dict]: """Override _closure to add thought_signature to messages""" + # Make a shallow copy to avoid mutating the caller's list + messages = req_messages.copy() + # Add thought_signature to tool_calls for Gemini compatibility - # Note: modifying in place is safe here as parent _closure will make a copy - req_messages = self._add_thought_signature_to_messages(req_messages) + messages = self._add_thought_signature_to_messages(messages) # Call parent implementation return await super()._closure( query=query, - req_messages=req_messages, + req_messages=messages, use_model=use_model, use_funcs=use_funcs, extra_args=extra_args,