The easy mistake in a Gemini-to-CometAPI adapter is replaying work that Gemini already completed. In Google’s managed-agents example, sandbox tools run on the server. Custom functions require client action, and the example selects pending calls by excluding every function_call.id already matched by a function_result.call_id.
That distinction should survive a migration to CometAPI’s Chat Completions tool loop. The two APIs do not share a step envelope: CometAPI documents tools entries with type: "function" and a nested function definition, then a response with finish_reason: "tool_calls" and message.tool_calls. It requires parsing JSON-encoded arguments, retaining the assistant message, and appending a tool result message with the matching tool_call_id.
Keep completion state separate from wire format
This is a proposed adapter design, not a claim that Chat Completions can resume a Gemini managed sandbox:
- Filter before scheduling. When the Gemini interaction is
requires_action, collect thecall_idvalues from itsfunction_resultsteps. Only unmatchedfunction_callsteps are pending. Restrict local execution to explicitly registered custom functions; an unknown or managed-tool name should stop for handling, not become an arbitrary local call. - Normalize pending work internally. Store the source interaction identity, call ID, function name, arguments, and execution status. Keep completed sandbox steps as provenance, not jobs to dispatch. Translate the local function definitions into CometAPI’s nested
tools[].functionstructure; do not send Gemini’sstepsenvelope as a Chat Completions request. - Bind results to the destination call, not just the function name. For a new CometAPI tool call, retain its returned call identity and parse its JSON-encoded arguments. Store a source-to-destination mapping only after checking that the intended operation and arguments match. A shared function name alone does not prove two calls represent the same work; otherwise treat them as separate operations.
- Preserve the assistant/tool pair. Append the actual CometAPI assistant message containing
tool_calls, then the correspondingrole: "tool"result with that destination call’stool_call_id, before requesting another completion. Do not use a Gemini call ID as the destination ID by assumption. Reject an unmatched result instead of guessing which call it belongs to.
For an already-pending Gemini custom call, choose explicitly whether to finish it on Gemini or hand it off through an application-controlled migration path. A fresh CometAPI completion is not proof that it will request the same function. The mapping above does not promise lossless provider-state transfer, sandbox continuity, or exactly-once execution.
A small fixture that catches the replay bug
Use an illustrative source interaction containing:
function_callwith IDsandbox-1, followed byfunction_resultwithcall_id: "sandbox-1";- an unmatched custom
function_callwith IDweather-2and nameget_weather.
The pending-work selector must return only weather-2. If both calls enter the local execution queue, the adapter has already failed, before any CometAPI request.
For the destination half, use a mocked CometAPI assistant tool call for that same intended weather operation, with a distinct call ID chat-weather-9. Assert that the assistant message stays in the conversation and the tool result references chat-weather-9, not weather-2. A missing assistant message or an unmatched ID must fail validation. This is a proposed fixture, not a reported API test.
Persist execution status and results before advancing the loop. On retry, consult that record; for side-effecting functions, require tool-side idempotency or reconcile an uncertain outcome rather than executing again blindly. A successful HTTP response alone does not establish either correct correlation or safe replay. Verify the selected CometAPI model’s tool support separately: these documentation examples establish the target message contract, not Gemini managed-agent feature parity.