From 56a56a417482cf8d866a32f87d11910de40406d9 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Tue, 24 Mar 2026 17:06:56 -0400 Subject: [PATCH] Prefer init-provided system instruction in Gemini Live Pass self._system_instruction_from_init to the adapter's get_llm_invocation_params(), which calls _resolve_system_instruction() to prefer init-provided over context-provided system instructions and warn on conflicts. Previously context-provided took precedence. Also fix the reconnect check to only reconnect when the resolved system instruction actually differs from what the initial connection used, avoiding unnecessary reconnects. --- .../services/google/gemini_live/llm.py | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/pipecat/services/google/gemini_live/llm.py b/src/pipecat/services/google/gemini_live/llm.py index 3e282e7ee..bb916a7cf 100644 --- a/src/pipecat/services/google/gemini_live/llm.py +++ b/src/pipecat/services/google/gemini_live/llm.py @@ -1078,28 +1078,26 @@ class GeminiLiveLLMService(LLMService): # We got our initial context self._context = context - # If context contains system instruction or tools, reconnect in - # order to apply them. - # (Context-provided system instruction and tools take precedence - # over the ones provided at initialization time. Note that we could - # do more sophisticated comparisons here, but for now this is - # sufficient: we'll assume folks won't mean to provide these - # settings both in the context and at initialization time. In a - # future change, we could/should implement the ability to swap - # these settings at any point). + # Reconnect if context changes the effective system instruction + # or tools compared to the initial connection (which used the + # init-provided values). Note that the determination of "effective" + # system instruction is delegated to the adapter, which still + # chooses the init-provided value if there is one. adapter: GeminiLLMAdapter = self.get_llm_adapter() - params = adapter.get_llm_invocation_params(self._context) + params = adapter.get_llm_invocation_params( + self._context, system_instruction=self._system_instruction_from_init + ) system_instruction = params["system_instruction"] tools = params["tools"] - if system_instruction and self._system_instruction_from_init: - logger.warning( - "System instruction provided both at init time and in context; using context-provided value." - ) + system_instruction_changed = system_instruction != self._system_instruction_from_init if tools and self._tools_from_init: logger.warning( "Tools provided both at init time and in context; using context-provided value." ) - if system_instruction or tools: + # For tools we simply check presence rather than diffing against + # init-provided tools, assuming that if context provides tools + # they warrant a reconnect. + if system_instruction_changed or tools: await self._reconnect() # Initialize our bookkeeping of already-completed tool calls in @@ -1281,10 +1279,12 @@ class GeminiLiveLLMService(LLMService): system_instruction = None tools = None if self._context: - params = adapter.get_llm_invocation_params(self._context) + params = adapter.get_llm_invocation_params( + self._context, system_instruction=self._system_instruction_from_init + ) system_instruction = params["system_instruction"] tools = params["tools"] - if not system_instruction: + else: system_instruction = self._system_instruction_from_init if not tools: tools = adapter.from_standard_tools(self._tools_from_init)