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.
This commit is contained in:
Paul Kompfner
2026-03-24 17:06:56 -04:00
parent e0c49927cf
commit 56a56a4174

View File

@@ -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)