-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(google): reject tool calls when tool_choice="none" in realtime #6166
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,28 +48,40 @@ def create_tools_config( | |
| return gemini_tools | ||
|
|
||
|
|
||
| def create_function_response( | ||
| output: llm.FunctionCallOutput, | ||
| *, | ||
| vertexai: bool = False, | ||
| tool_response_scheduling: NotGivenOr[types.FunctionResponseScheduling] = NOT_GIVEN, | ||
| ) -> types.FunctionResponse: | ||
| res = types.FunctionResponse( | ||
| name=output.name, | ||
| response={"error": output.output} if output.is_error else {"output": output.output}, | ||
|
longcw marked this conversation as resolved.
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. 🚩 Behavioral change in error response format for get_tool_results_for_realtime The refactoring of Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| ) | ||
| if is_given(tool_response_scheduling): | ||
| # vertexai currently doesn't support the scheduling parameter, gemini api defaults to idle | ||
| # it's the user's responsibility to avoid this parameter when using vertexai | ||
| res.scheduling = tool_response_scheduling | ||
| if not vertexai: | ||
| # vertexai does not support id in FunctionResponse | ||
| # see: https://github.com/googleapis/python-genai/blob/85e00bc/google/genai/_live_converters.py#L1435 | ||
| res.id = output.call_id | ||
| return res | ||
|
|
||
|
|
||
| def get_tool_results_for_realtime( | ||
| chat_ctx: llm.ChatContext, | ||
| *, | ||
| vertexai: bool = False, | ||
| tool_response_scheduling: NotGivenOr[types.FunctionResponseScheduling] = NOT_GIVEN, | ||
| ) -> types.LiveClientToolResponse | None: | ||
| function_responses: list[types.FunctionResponse] = [] | ||
| for msg in chat_ctx.items: | ||
| if msg.type == "function_call_output": | ||
| res = types.FunctionResponse( | ||
| name=msg.name, | ||
| response={"output": msg.output}, | ||
| ) | ||
| if is_given(tool_response_scheduling): | ||
| # vertexai currently doesn't support the scheduling parameter, gemini api defaults to idle | ||
| # it's the user's responsibility to avoid this parameter when using vertexai | ||
| res.scheduling = tool_response_scheduling | ||
| if not vertexai: | ||
| # vertexai does not support id in FunctionResponse | ||
| # see: https://github.com/googleapis/python-genai/blob/85e00bc/google/genai/_live_converters.py#L1435 | ||
| res.id = msg.call_id | ||
| function_responses.append(res) | ||
| function_responses = [ | ||
| create_function_response( | ||
| msg, vertexai=vertexai, tool_response_scheduling=tool_response_scheduling | ||
| ) | ||
| for msg in chat_ctx.items | ||
| if msg.type == "function_call_output" | ||
| ] | ||
| return ( | ||
| types.LiveClientToolResponse(function_responses=function_responses) | ||
| if function_responses | ||
|
|
||
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.
🚩 The
continueon rejected tool calls skips processing of co-occurring response fieldsWhen
tool_choice="none"and aresponse.tool_callis present, thecontinueat line 1044 skips ALL other processing for that response — includingsession_resumption_update,tool_call_cancellation,usage_metadata, and criticallygo_away(which signals an upcoming server disconnection). If any of these fields co-occur withtool_callin the sameLiveServerMessage, they would be silently dropped. In practice, the Gemini API likely sends these as separate messages, but ifgo_awayever accompanies a tool_call, the session wouldn't prepare for disconnection. Theusage_metadatacase is partially mitigated by the_rejected_tool_callsguard in_handle_usage_metadata.Was this helpful? React with 👍 or 👎 to provide feedback.