|
| 1 | +# Code Interpreter / Tool Call Fix for Gemini Pipeline |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +The Google Gemini pipeline did not properly handle tool calls (function calls) in responses from the Gemini API. This caused the code interpreter feature in Open WebUI to not work - when users asked Gemini to execute code, the response would repeat text instead of executing the code. |
| 6 | + |
| 7 | +## Root Cause |
| 8 | + |
| 9 | +The Gemini pipeline was configured to send tools to the API (via `__tools__` parameter) but did not handle tool call responses. When Gemini returned a response containing a `function_call` part (indicating it wants to call a tool like the code interpreter), the pipeline: |
| 10 | + |
| 11 | +1. **Streaming mode**: Ignored the `function_call` part entirely, only processing `text` and `thought` parts |
| 12 | +2. **Non-streaming mode**: Also ignored `function_call` parts, only processing `text`, `thought`, and `inline_data` parts |
| 13 | + |
| 14 | +This meant Open WebUI never received the tool call and couldn't execute the code. |
| 15 | + |
| 16 | +## Solution |
| 17 | + |
| 18 | +Added proper detection and emission of tool calls in both streaming and non-streaming response handling: |
| 19 | + |
| 20 | +### Streaming Response Changes |
| 21 | + |
| 22 | +In `_handle_streaming_response()` method (around line 1684): |
| 23 | + |
| 24 | +```python |
| 25 | +# Function call parts (tool calls) |
| 26 | +if getattr(part, "function_call", None): |
| 27 | + function_call = part.function_call |
| 28 | + # Emit tool call event for Open WebUI |
| 29 | + await __event_emitter__( |
| 30 | + { |
| 31 | + "type": "chat:message:delta", |
| 32 | + "data": { |
| 33 | + "role": "assistant", |
| 34 | + "content": "", |
| 35 | + "tool_calls": [ |
| 36 | + { |
| 37 | + "id": f"call_{function_call.name}", |
| 38 | + "type": "function", |
| 39 | + "function": { |
| 40 | + "name": function_call.name, |
| 41 | + "arguments": json.dumps( |
| 42 | + dict(function_call.args) |
| 43 | + ), |
| 44 | + }, |
| 45 | + } |
| 46 | + ], |
| 47 | + }, |
| 48 | + } |
| 49 | + ) |
| 50 | +``` |
| 51 | + |
| 52 | +### Non-Streaming Response Changes |
| 53 | + |
| 54 | +In the `pipe()` method's non-streaming path (around line 2083): |
| 55 | + |
| 56 | +1. **Detection**: Added check for `function_call` attribute on response parts |
| 57 | +2. **Collection**: Store tool calls in a list during part processing |
| 58 | +3. **Emission**: Emit tool calls via event emitter |
| 59 | +4. **Response Format**: Return OpenAI-compatible format with tool calls |
| 60 | + |
| 61 | +```python |
| 62 | +# Handle function calls (tool calls) |
| 63 | +if getattr(part, "function_call", None): |
| 64 | + function_call = part.function_call |
| 65 | + tool_call = { |
| 66 | + "id": f"call_{function_call.name}", |
| 67 | + "type": "function", |
| 68 | + "function": { |
| 69 | + "name": function_call.name, |
| 70 | + "arguments": json.dumps(dict(function_call.args)), |
| 71 | + }, |
| 72 | + } |
| 73 | + tool_calls_list.append(tool_call) |
| 74 | +``` |
| 75 | + |
| 76 | +Then at the end of processing: |
| 77 | + |
| 78 | +```python |
| 79 | +# If there are tool calls, return them in the expected format |
| 80 | +if tool_calls_list: |
| 81 | + # Emit tool calls for Open WebUI |
| 82 | + await __event_emitter__( |
| 83 | + { |
| 84 | + "type": "chat:message", |
| 85 | + "data": { |
| 86 | + "role": "assistant", |
| 87 | + "content": full_response or "", |
| 88 | + "tool_calls": tool_calls_list, |
| 89 | + }, |
| 90 | + } |
| 91 | + ) |
| 92 | + # Return in OpenAI-compatible format |
| 93 | + return { |
| 94 | + "choices": [ |
| 95 | + { |
| 96 | + "message": { |
| 97 | + "role": "assistant", |
| 98 | + "content": full_response or "", |
| 99 | + "tool_calls": tool_calls_list, |
| 100 | + } |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | +``` |
| 105 | + |
| 106 | +## Tool Call Format |
| 107 | + |
| 108 | +Tool calls are formatted in OpenAI-compatible structure: |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "id": "call_<function_name>", |
| 113 | + "type": "function", |
| 114 | + "function": { |
| 115 | + "name": "<function_name>", |
| 116 | + "arguments": "<json_string_of_args>" |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +For example, a code execution call: |
| 122 | + |
| 123 | +```json |
| 124 | +{ |
| 125 | + "id": "call_python", |
| 126 | + "type": "function", |
| 127 | + "function": { |
| 128 | + "name": "python", |
| 129 | + "arguments": "{\"code\": \"print('Hello, World!')\"}" |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Testing |
| 135 | + |
| 136 | +To test the fix: |
| 137 | + |
| 138 | +1. Enable code interpreter in Open WebUI |
| 139 | +2. Select a Gemini model |
| 140 | +3. Ask it to execute code, e.g., "Calculate pi to 10 decimal places using Python" |
| 141 | +4. The code should now execute and show results instead of repeating text |
| 142 | + |
| 143 | +## Compatibility |
| 144 | + |
| 145 | +This fix maintains backward compatibility: |
| 146 | +- When no tool calls are present, behavior is unchanged |
| 147 | +- Tool calls follow OpenAI format for compatibility with Open WebUI |
| 148 | +- Works with both streaming and non-streaming modes |
| 149 | +- Does not interfere with existing features (thoughts, images, grounding) |
| 150 | + |
| 151 | +## References |
| 152 | + |
| 153 | +- Google Generative AI SDK documentation on function calling |
| 154 | +- OpenAI API specification for tool calls |
| 155 | +- Open WebUI event emitter documentation |
0 commit comments