diff --git a/src/pipecat/adapters/services/open_ai_realtime_adapter.py b/src/pipecat/adapters/services/open_ai_realtime_adapter.py index 6bea0e2ed..3c394d99b 100644 --- a/src/pipecat/adapters/services/open_ai_realtime_adapter.py +++ b/src/pipecat/adapters/services/open_ai_realtime_adapter.py @@ -43,20 +43,26 @@ class OpenAIRealtimeLLMAdapter(BaseLLMAdapter): """Get the identifier used in LLMSpecificMessage instances for OpenAI Realtime.""" return "openai-realtime" - def get_llm_invocation_params(self, context: LLMContext) -> OpenAIRealtimeLLMInvocationParams: + def get_llm_invocation_params( + self, context: LLMContext, *, system_instruction: Optional[str] = None + ) -> OpenAIRealtimeLLMInvocationParams: """Get OpenAI Realtime-specific LLM invocation parameters from a universal LLM context. - This is a placeholder until support for universal LLMContext machinery is added for OpenAI Realtime. - Args: context: The LLM context containing messages, tools, etc. + system_instruction: Optional system instruction from service settings. Returns: Dictionary of parameters for invoking OpenAI Realtime's API. """ messages = self._from_universal_context_messages(self.get_messages(context)) + effective_system = self._resolve_system_instruction( + messages.system_instruction, + system_instruction, + discard_context_system=True, + ) return { - "system_instruction": messages.system_instruction, + "system_instruction": effective_system, "messages": messages.messages, # NOTE: LLMContext's tools are guaranteed to be a ToolsSchema (or NOT_GIVEN) "tools": self.from_standard_tools(context.tools) or [], diff --git a/src/pipecat/services/openai/realtime/llm.py b/src/pipecat/services/openai/realtime/llm.py index bd31369ae..293ee0d87 100644 --- a/src/pipecat/services/openai/realtime/llm.py +++ b/src/pipecat/services/openai/realtime/llm.py @@ -687,14 +687,16 @@ class OpenAIRealtimeLLMService(LLMService): adapter: OpenAIRealtimeLLMAdapter = self.get_llm_adapter() if self._context: - llm_invocation_params = adapter.get_llm_invocation_params(self._context) + llm_invocation_params = adapter.get_llm_invocation_params( + self._context, system_instruction=self._settings.system_instruction + ) # tools given in the context override the tools in the session properties if llm_invocation_params["tools"]: settings.tools = llm_invocation_params["tools"] - # instructions in the context come from an initial "system" message in the - # messages list, and override instructions in the session properties + # The adapter resolves conflicts between init-provided and + # context-provided system instructions (preferring init-provided). if llm_invocation_params["system_instruction"]: settings.instructions = llm_invocation_params["system_instruction"]