Repository navigation
Add support for multiple function calling for Gemini - #1267
Conversation
Resolves receptron#1266. Signed-off-by: Masashi Honma <[email protected]>
📝 WalkthroughWalkthroughConvert Gemini agent to collect and pass multiple function calls: converter signature updated to accept a Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Agent
participant GeminiAPI
participant Converter
Client->>Agent: call geminiAgent(params, stream:true)
Agent->>GeminiAPI: start streaming GenerateContent request
GeminiAPI-->>Agent: stream chunks (content + optional chunk.functionCalls)
Agent->>Agent: accumulate chunk.functionCalls into functionCalls[]
Agent->>Converter: convertOpenAIChatCompletion(response, functionCalls, messages, llmMetaData)
Converter-->>Agent: standardized result (includes tool_calls derived from functionCalls)
Agent-->>Client: deliver final result (messages + tool_calls)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
llm_agents/gemini_agent/lib/gemini_agent.js (1)
11-24: Add stable, unique IDs to tool_calls for proper response correlation.Multiple tool calls receive hardcoded
id: "dummy", preventing tools_agent from mapping tool responses to the correct calls. Additionally,message.tool_callslacksidandtypefields required for OpenAI-style tool response handling. During streaming, aggregated calls will all collide on the same ID, breaking multi-tool workflows.Generate unique IDs per call using
call.id ??gemini_${idx}`` for bothtool_callsand `message.tool_calls`, and add the `type: "function"` field to match OpenAI format.
🧹 Nitpick comments (3)
llm_agents/gemini_agent/lib/gemini_agent.js (1)
129-134: Streaming aggregation may duplicate partial functionCalls.If
@google/genaistreams the full functionCalls array on each chunk (or sends partial updates for the same call),push(...chunk.functionCalls)can duplicate or fragment calls. Consider replacing with the latest snapshot or de-duping by id/name after confirming SDK semantics.♻️ Possible adjustment (if chunks include full snapshots)
- if (chunk.functionCalls) { - functionCalls.push(...chunk.functionCalls); - } + if (chunk.functionCalls) { + functionCalls = chunk.functionCalls; + }llm_agents/gemini_agent/tests/run_gemini.ts (1)
56-104: Add a minimal assertion for multi-tool coverage.The new test is a pure smoke test (always
true). To guard the new feature, consider asserting at least thattool_callsis an array (or use a mocked/recorded response for deterministic expectations).✅ Example minimal assertion
if (res) { console.log(res); console.log(res.tool_calls); + assert.ok(Array.isArray(res.tool_calls), "tool_calls should be an array"); }llm_agents/gemini_agent/src/gemini_agent.ts (1)
181-187: Streaming aggregation may duplicate partial functionCalls.If
@google/genaiemits the fullfunctionCallslist per chunk (or partial updates for the same call), pushing into the array can create duplicates or fragmented calls. Consider replacing with the latest snapshot or de-duping after verifying SDK semantics.♻️ Possible adjustment (if chunks include full snapshots)
- if (chunk.functionCalls) { - functionCalls.push(...chunk.functionCalls); - } + if (chunk.functionCalls) { + functionCalls = chunk.functionCalls; + }
Signed-off-by: Masashi Honma <[email protected]>
|
Thank you for the PR!! To verify that the change (collecting multiple tool calls) works correctly, could you add a check for res.tool_calls.length === 2 in test gemini multiple tools? |
|
@ystknsh Thank you for your comment. I'd like to confirm one thing. Looking at other directories, tests starting with |
|
In my environment, the test passed with only one tool call returned: It doesn't have to be an assert - just something that makes it clear when multiple tool calls aren't being returned (e.g., logging tool_calls.length). |
|
Ah, ok I understand what you're trying to say. Gemini doesn't call the function 100% of the time, which is why the issue you mentioned occurs. I tried it ten times, and nine times it called it twice, but one time it only called it once. So, could you please try it a few times? |
|
This is because Gemini defaults to |
|
I understand that retrying the test may return 2 tool calls. You don't need to change the pass/fail result - just add a log so we can tell from the output.
Also, looking forward to the separate PR for forcing function calling!! |
Signed-off-by: Masashi Honma <[email protected]>
|
Sure, fixed. |
|
Thank you for adding the log! |
Resolves #1266.
Summary by CodeRabbit
Improvements
Tests
✏️ Tip: You can customize this high-level summary in your review settings.