Map Gemini pending steps to the CometAPI Chat Completions tool contract

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:

  1. Filter before scheduling. When the Gemini interaction is requires_action, collect the call_id values from its function_result steps. Only unmatched function_call steps 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.
  2. 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[].function structure; do not send Gemini’s steps envelope as a Chat Completions request.
  3. 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.
  4. Preserve the assistant/tool pair. Append the actual CometAPI assistant message containing tool_calls, then the corresponding role: "tool" result with that destination call’s tool_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_call with ID sandbox-1, followed by function_result with call_id: "sandbox-1";
  • an unmatched custom function_call with ID weather-2 and name get_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.

A practical way to make this adapter reviewable is to render the identity mapping as a small, explicit trace rather than only logging the final request. Show the source steps in order, with completed function_result.call_id entries visually crossed out, and leave unmatched custom calls as queued cards containing the source ID, function name, and parsed-argument digest. A second lane can show the destination assistant tool_calls ID and the result message that consumes it. The two lanes should be connected by a mapping edge only after the function and arguments have been checked.

That trace makes two failure modes easy to spot in a fixture review: a completed sandbox call reappearing in the local queue, or a result edge pointing at the Gemini ID instead of the newly issued CometAPI tool_call_id. Keep the display derived from the persisted execution record, not from a successful HTTP response, so retries and uncertain side effects remain visible. The visual is an inspection aid; it does not imply that the providers share state or that the operation is exactly-once.