-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(voice): deliver tool results to realtime session on interruption #6600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
longcw
wants to merge
1
commit into
main
Choose a base branch
from
longc/gemini-tool-output-on-interrupt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,13 @@ def _validate_model_api_match(model: str, use_vertexai: bool) -> None: | |
| ) | ||
|
|
||
|
|
||
| def _warn_vertex_scheduling_unsupported() -> None: | ||
| logger.warning( | ||
| "tool_response_scheduling is not supported by Vertex AI and will be ignored; " | ||
| "tool responses use the default scheduling there." | ||
| ) | ||
|
|
||
|
|
||
| def _get_1008_error_hint(error_message: str) -> str | None: | ||
| """ | ||
| Generate a hint for WebSocket 1008 policy violation errors. | ||
|
|
@@ -296,6 +303,8 @@ def __init__( | |
| if is_given(vertexai) | ||
| else os.environ.get("GOOGLE_GENAI_USE_VERTEXAI", "0").lower() in ["true", "1"] | ||
| ) | ||
| if use_vertexai and is_given(tool_response_scheduling): | ||
| _warn_vertex_scheduling_unsupported() | ||
| if not is_given(model): | ||
| model = ( | ||
| "gemini-live-2.5-flash-native-audio" | ||
|
|
@@ -506,6 +515,8 @@ def __init__(self, realtime_model: RealtimeModel) -> None: | |
| # means we're draining that turn's trailing events (which have no generation to attach | ||
| # to). reset when the next generation starts. | ||
| self._rejected_tool_calls = 0 | ||
| # whether playout of the current turn was cut short | ||
| self._playout_interrupted = False | ||
|
|
||
| self._session_resumption_handle: str | None = ( | ||
| self._opts.session_resumption.handle | ||
|
|
@@ -572,6 +583,8 @@ def update_options( | |
| and self._opts.tool_response_scheduling != tool_response_scheduling | ||
| ): | ||
| self._opts.tool_response_scheduling = tool_response_scheduling | ||
| if self._opts.vertexai: | ||
| _warn_vertex_scheduling_unsupported() | ||
| # no need to restart | ||
|
|
||
| if is_given(tool_choice): | ||
|
|
@@ -657,10 +670,20 @@ async def update_chat_ctx(self, chat_ctx: llm.ChatContext) -> None: | |
| append_ctx.items.append(item) | ||
|
|
||
| if append_ctx.items: | ||
| scheduling = self._opts.tool_response_scheduling | ||
| if ( | ||
| not self._opts.vertexai # scheduling is not supported by vertex | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant now that |
||
| and self._playout_interrupted | ||
| # only honoured on NON_BLOCKING declarations | ||
| and self._opts.tool_behavior == types.Behavior.NON_BLOCKING | ||
| ): | ||
| # the turn is over, so record the result without prompting more speech | ||
| scheduling = types.FunctionResponseScheduling.SILENT | ||
|
|
||
| tool_results = get_tool_results_for_realtime( | ||
| append_ctx, | ||
| vertexai=self._opts.vertexai, | ||
| tool_response_scheduling=self._opts.tool_response_scheduling, | ||
| tool_response_scheduling=scheduling, | ||
| ) | ||
| if self._realtime_model.capabilities.mutable_chat_context: | ||
| turns_dict, _ = append_ctx.copy(exclude_function_call=True).to_provider_format( | ||
|
|
@@ -817,6 +840,9 @@ def start_user_activity(self) -> None: | |
| ) | ||
|
|
||
| def interrupt(self) -> None: | ||
| # recorded locally since this cannot reach Gemini under automatic activity detection | ||
| self._playout_interrupted = True | ||
|
|
||
| # Gemini Live treats activity start as interruption, so we rely on start_user_activity | ||
| # notifications to handle it | ||
| if ( | ||
|
|
@@ -1201,6 +1227,7 @@ def _build_connect_config(self) -> types.LiveConnectConfig: | |
|
|
||
| def _start_new_generation(self) -> None: | ||
| self._rejected_tool_calls = 0 | ||
| self._playout_interrupted = False | ||
| if self._current_generation and not self._current_generation._done: | ||
| logger.warning("starting new generation while another is active. Finalizing previous.") | ||
| self._mark_current_generation_done() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chat_ctxalready returnsself._chat_ctx.copy(), so this copies twice.