Merge pull request #4078 from pipecat-ai/cb/gemini-updates

Updates for Gemini Live
This commit is contained in:
kompfner
2026-03-24 11:18:00 -04:00
committed by GitHub
7 changed files with 81 additions and 194 deletions

View File

@@ -98,6 +98,7 @@ try:
FunctionResponse,
GenerationConfig,
GroundingMetadata,
HistoryConfig,
HttpOptions,
LiveConnectConfig,
LiveServerMessage,
@@ -648,6 +649,11 @@ class GeminiLiveLLMService(LLMService):
# Overriding the default adapter to use the Gemini one.
adapter_class = GeminiLLMAdapter
@property
def _is_gemini_3(self) -> bool:
"""Check if the current model is a Gemini 3.x model."""
return "gemini-3" in (self._settings.model or "")
def __init__(
self,
*,
@@ -791,7 +797,7 @@ class GeminiLiveLLMService(LLMService):
self._system_instruction_from_init = system_instruction
self._tools_from_init = tools
self._inference_on_context_initialization = inference_on_context_initialization
self._needs_turn_complete_message = False
self._needs_initial_turn_complete_message = False
self._audio_input_paused = start_audio_paused
self._video_input_paused = start_video_paused
@@ -993,8 +999,8 @@ class GeminiLiveLLMService(LLMService):
self._user_is_speaking = False
self._user_audio_buffer = bytearray()
await self.start_ttfb_metrics()
if self._needs_turn_complete_message:
self._needs_turn_complete_message = False
if self._needs_initial_turn_complete_message:
self._needs_initial_turn_complete_message = False
# NOTE: without this, the model ignores the context it's been
# seeded with before the user started speaking
await self._session.send_client_content(turn_complete=True)
@@ -1056,9 +1062,10 @@ class GeminiLiveLLMService(LLMService):
elif isinstance(frame, LLMMessagesAppendFrame):
# NOTE: handling LLMMessagesAppendFrame here in the LLMService is
# unusual - typically this would be handled in the user context
# aggregator. Leaving this handling here so that user code that
# uses this frame *without* a user context aggregator still works
# (we have an example that does just that, actually).
# aggregator. Leaving this handling here so that legacy user code
# that uses this frame *without* a user context aggregator to kick
# off a conversation still works (we used to have an example that
# did that).
await self._create_single_response(frame.messages)
elif isinstance(frame, LLMSetToolsFrame):
# TODO: implement runtime tool updates for Gemini Live.
@@ -1204,6 +1211,7 @@ class GeminiLiveLLMService(LLMService):
input_audio_transcription=AudioTranscriptionConfig(),
output_audio_transcription=AudioTranscriptionConfig(),
session_resumption=SessionResumptionConfig(handle=session_resumption_handle),
history_config=HistoryConfig(initial_history_in_client_content=True),
)
# Add context window compression to configuration, if enabled
@@ -1508,17 +1516,26 @@ class GeminiLiveLLMService(LLMService):
await self._session.send_client_content(
turns=messages, turn_complete=self._inference_on_context_initialization
)
# Gemini 3.x wants turn_complete=True, but also won't run inference without a realtime input
if self._is_gemini_3 and self._inference_on_context_initialization:
await self._session.send_realtime_input(text=" ")
except Exception as e:
await self._handle_send_error(e)
# If we're generating a response right away upon initializing
# conversation history, set a flag saying that we need a turn complete
# message when the user stops speaking.
if not self._inference_on_context_initialization:
self._needs_turn_complete_message = True
# conversation history, set a flag saying that we'll need a turn
# complete message when the user stops speaking.
# This is a quirky workaround, and not one that Gemini 3 needs.
if not self._inference_on_context_initialization and not self._is_gemini_3:
self._needs_initial_turn_complete_message = True
async def _create_single_response(self, messages_list):
"""Create a single response from a list of messages."""
"""Create a single response from a list of messages.
This is only here to support the very specific 'legacy' scenario of
kicking off a conversation using LLMMessagesAppendFrame when there's no
context aggregators in the pipeline (see process_frame for more details).
"""
if self._disconnecting or not self._session:
return
@@ -1537,6 +1554,9 @@ class GeminiLiveLLMService(LLMService):
try:
await self._session.send_client_content(turns=messages, turn_complete=True)
# Gemini 3.x wants turn_complete=True, but also won't run inference without a realtime input
if self._is_gemini_3:
await self._session.send_realtime_input(text=" ")
except Exception as e:
await self._handle_send_error(e)