Skip to content

Conversation

@Solventerritory
Copy link

@Solventerritory Solventerritory commented Dec 19, 2025

Hey! This fixes issue #906.

I ran into the same problem many others reported: the docs mention BidiGenerateContentToolResponse, but they don’t clearly show how to format a tool response when using WebSockets. I tried several approaches, but the WebSocket kept closing unexpectedly.

WHAT I ADDED

Two files to make WebSocket function calling actually usable end-to-end:

  1. Websocket_Tool_Call_Response_Example.py
    A complete, runnable example. You just need to add your API key.
    It shows:
  • How to set up a WebSocket connection with tools enabled
  • How to detect and handle a function call
  • Two different (working) ways to send the tool response back
  • Example tools (weather lookup, turning lights on/off)
  1. WEBSOCKET_TOOL_RESPONSE_FIX.md
    A practical guide covering:
  • What works and what doesn’t
  • Common mistakes that cause WebSocket disconnects
  • Subtleties missing from the current docs

THE ACTUAL SOLUTION

There are two formats that work for sending tool responses over WebSockets.

OPTION 1 (used in the cookbook examples)

response_msg = {
    "tool_response": {
        "function_responses": [{
            "id": function_call_id,
            "name": function_name,
            "response": {"result": your_data}
        }]
    }
}

OPTION 2 (based on @nmfisher’s workaround in the issue)

response_msg = {
    "clientContent": {
        "turns": [{
            "role": "user",
            "parts": [{
                "functionResponse": {
                    "name": function_name,
                    "response": {"output": your_data}
                }
            }]
        }],
        "turnComplete": True
    }
}

Both formats work reliably. I’ve been using Option 1 since it matches the cookbook examples.

WHAT DOES NOT WORK

The following consistently caused failures or WebSocket closures:

  • Trying to use BidiGenerateContentToolResponse directly
  • Forgetting the function call ID
  • Not matching the function name exactly
  • Sending a Python dict instead of a JSON-encoded string

TESTED SCENARIOS

Tested with:

  • Single function calls
  • Multiple function calls in a single turn
  • Follow-up model responses after a function call

…entation

- Add complete working example: Websocket_Tool_Call_Response_Example.py
- Add comprehensive documentation: WEBSOCKET_TOOL_RESPONSE_FIX.md
- Document two working formats for tool responses via websockets
- Clarify BidiGenerateContentToolResponse confusion
- Include examples for function calling with websockets
- Add error handling and multiple function call examples

Resolves google-gemini#906
@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Dec 19, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Solventerritory, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves issue #906 by providing clear, actionable guidance and working examples for handling function/tool call responses with the Gemini Live API over websockets. It addresses the previous lack of explicit documentation on the correct message formats, which often led to connection issues. The changes include a new Markdown document detailing two effective response methods and a comprehensive Python example that can be run directly to illustrate the solution.

Highlights

  • Clarified Websocket Tool Responses: Provides two validated formats for sending function/tool responses over websockets to the Gemini Live API, addressing a common pain point where documentation was unclear and connections would often close.
  • Comprehensive Documentation: Introduces WEBSOCKET_TOOL_RESPONSE_FIX.md, a detailed guide outlining the problem, two working solutions, key differences from existing documentation, common pitfalls, and references for successful implementation.
  • Runnable Python Example: Adds Websocket_Tool_Call_Response_Example.py, a complete and executable Python script demonstrating proper websocket setup, handling tool calls, and sending responses for various scenarios (single, multiple, and follow-up calls).

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great addition, providing much-needed documentation and a working example for using websockets with tool calling. The Python script is clear and the markdown file is very helpful. I've added a few suggestions to improve the script's maintainability and robustness, and to align the documentation with the repository's style guide.

Solventerritory and others added 3 commits December 19, 2025 20:29
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Change all headings to sentence case per style guide
- Make model name a command-line argument with --model flag
- Add default value of gemini-2.0-flash-live-001
- Update function signatures to pass model parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:examples Issues/PR referencing examples folder status:awaiting review PR awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant