diff --git a/changelog/3976.fixed.md b/changelog/3976.fixed.md new file mode 100644 index 000000000..3153cdcbe --- /dev/null +++ b/changelog/3976.fixed.md @@ -0,0 +1 @@ +- Fixed `GoogleLLMService` ignoring the `system_instruction` set via constructor or `GoogleLLMSettings` when a system message was also present in the context. The settings value now correctly takes priority, and a warning is logged when both are set. diff --git a/src/pipecat/services/google/llm.py b/src/pipecat/services/google/llm.py index 85f8c0ce3..cf9e91b53 100644 --- a/src/pipecat/services/google/llm.py +++ b/src/pipecat/services/google/llm.py @@ -1011,12 +1011,16 @@ class GoogleLLMService(LLMService): self, params_from_context: GeminiLLMInvocationParams ) -> AsyncIterator[GenerateContentResponse]: messages = params_from_context["messages"] - if ( - params_from_context["system_instruction"] - and self._settings.system_instruction != params_from_context["system_instruction"] - ): - logger.debug(f"System instruction changed: {params_from_context['system_instruction']}") - self._settings.system_instruction = params_from_context["system_instruction"] + + # Constructor/settings system instruction takes priority over context. + if self._settings.system_instruction and params_from_context["system_instruction"]: + logger.warning( + f"{self}: Both system_instruction and a system message in context are" + " set. Using system_instruction." + ) + system_instruction = ( + self._settings.system_instruction or params_from_context["system_instruction"] + ) tools = [] if params_from_context["tools"]: @@ -1029,7 +1033,7 @@ class GoogleLLMService(LLMService): # Build generation parameters generation_params = self._build_generation_params( - system_instruction=self._settings.system_instruction, + system_instruction=system_instruction, tools=tools, tool_config=tool_config, )