Antigravity text-form tool calls are returned as chat content instead of tool_calls

Author: ladyddCreated Sep 29, 2026Updated Sep 30, 2026
Labelspending

Is it a request payload issue?

  • Yes.
  • No — this concerns response translation after an Antigravity response has arrived.

Describe the bug

While using PI through CLIProxyAPI, a tool-use turn stalled: the client-visible text was a preamble directly concatenated with call:default_api:bash{...}, and the client received finish_reason=stop. The upstream response body and its finishReason were not recorded, so this incident has not been demonstrated to be fixed by the patch.

Subject to parsing and tool-choice checks, text with a preamble is promoted only when the upstream reason is MALFORMED_FUNCTION_CALL, while STOP with a preamble remains unchanged text with no tool execution; a body consisting solely of calls, optionally prefixed by Malformed function call:, can be promoted on either STOP or MALFORMED_FUNCTION_CALL.

I reduced the response-translation behavior to synthetic, credential-free fixtures on dev commit b7f321f2a1a871f31d815bb5c92e6758b03bfc2d. These fixtures are not a claim that a live model deterministically generates this output for a particular prompt.

On unpatched dev:

  • A response containing only call:default_api:bash{command:pwd} with STOP is returned as ordinary assistant content, with no tool call.
  • A response containing a preamble followed by that text form with MALFORMED_FUNCTION_CALL also produces no tool call. Streaming reports stop; non-streaming retains malformed_function_call.

The client therefore has no structured tool call to dispatch. Request-side schema validation may reduce the upstream failures, but does not translate this already-received response.

CLI Type

Antigravity account; downstream OpenAI Chat Completions API.

Model Name

Client model: agy/agy-gemini-3.8-flash-high.

For request fa120d54, the proxy log records the upstream model being switched to gemini-3.8-flash-exp-a and an HTTP 200 response. It does not contain the upstream response body and therefore does not establish the upstream finishReason; HTTP 200 and the downstream stop do not supply that missing evidence.

The local fixtures use fixture-model only as a translator test label, not as a real provider model.

LLM Client

PI 0.87.1 (package @earendil-works/pi-coding-agent).

Request Information

Synthetic request fixture; no request was sent to a running server during verification:

json
{
  "model": "fixture-model",
  "messages": [{"role": "user", "content": "Print the working directory."}],
  "tools": [{
    "type": "function",
    "function": {
      "name": "bash",
      "parameters": {
        "type": "object",
        "properties": {"command": {"type": "string"}},
        "required": ["command"]
      }
    }
  }],
  "tool_choice": "auto"
}

Minimal upstream response fixture:

json
{
  "response": {
    "candidates": [{
      "index": 0,
      "content": {"parts": [{"text": "call:default_api:bash{command:pwd}"}]},
      "finishReason": "STOP"
    }],
    "usageMetadata": {
      "promptTokenCount": 1,
      "candidatesTokenCount": 1,
      "totalTokenCount": 2
    }
  }
}

A second fixture uses text Before\nMalformed function call: call:default_api:bash{command:pwd} and finish reason MALFORMED_FUNCTION_CALL. Streaming also tests text arriving before the terminal reason and usage.

Expected behavior / proposed recovery policy

I propose narrowly scoped response-side recovery, rather than executing arbitrary prose:

  • Preserve native functionCall translation.
  • Promote only fully parsed calls to tools declared and permitted by this request's tool_choice.
  • For STOP, require the entire visible body to be the call sequence, apart from an optional Malformed function call: diagnostic prefix and surrounding whitespace.
  • For MALFORMED_FUNCTION_CALL, allow a call suffix after a preamble, preserving the meaningful preamble.
  • Consult finishMessage only for MALFORMED_FUNCTION_CALL with empty visible text.
  • Never promote a call with trailing explanatory prose, a disallowed/undeclared tool, an empty finish reason, MAX_TOKENS, or a safety/blocking finish reason.
  • On failed malformed-call recovery, retain malformed_function_call; do not turn it into successful stop.
  • Preserve the upstream reason in native_finish_reason.
  • Buffer possible call/diagnostic suffixes until the finish decision; keep ordinary prose streaming. Preserve internal whitespace and numeric literals.
  • Apply the same recovery rules to streaming and non-streaming, including independent non-streaming candidates.

For the STOP fixture, the relevant non-streaming choice fields would be:

json
{
  "index": 0,
  "message": {
    "role": "assistant",
    "content": null,
    "tool_calls": [{
      "id": "<generated-id>",
      "type": "function",
      "function": {"name": "bash", "arguments": "{\"command\":\"pwd\"}"}
    }]
  },
  "finish_reason": "tool_calls",
  "native_finish_reason": "stop"
}

For the malformed fixture, content is Before, finish reason is tool_calls, and native finish reason is malformed_function_call. These are proposed recovery results, not raw provider output.

Screenshots

Not required; structured fixtures and test output are supplied instead.

OS Type

  • Local verification: Ubuntu 24.04.3 LTS, linux/amd64, Go 1.26.0.
  • Reported running CLIProxyAPI version at discovery: 7.3.8.
  • Patch verification base: dev b7f321f2a1a871f31d815bb5c92e6758b03bfc2d.
  • The running 7.3.8 instance was not restarted, replaced, or used for the verification.

Additional context / implementation proposal

I understand the translator-path-guard policy: translator changes should be requested through an issue for the maintenance team. This issue is the policy discussion first; it is not a request to bypass the check or move translation into an executor.

The prepared implementation is limited to these paths:

  • internal/translator/antigravity/openai/chat-completions/antigravity_openai_response.go
  • internal/translator/antigravity/openai/chat-completions/malformed_function_call.go
  • internal/translator/antigravity/openai/chat-completions/malformed_function_call_test.go

The full proposed implementation and its regression tests are in the companion pull request, which will be linked here as soon as it is opened. A copy of the patch, the independent repro test, and the unpatched/patched test logs is at https://gist.github.com/ladydd/28543b6da0359fc4c1051ca685490da9. Its shared tool-choice gate is:

go
func recoveryAllowedTools(request []byte) map[string]string {
    declared := util.SanitizedFunctionNameMap(request)
    choice := gjson.GetBytes(request, "tool_choice")
    if choice.Type == gjson.String {
        if strings.EqualFold(strings.TrimSpace(choice.String()), "none") {
            return nil
        }
    } else if choice.IsObject() {
        switch strings.ToLower(strings.TrimSpace(choice.Get("type").String())) {
        case "none":
            return nil
        case "function":
            name := choice.Get("function.name").String()
            if sanitized, ok := declared[name]; ok {
                return map[string]string{name: sanitized}
            }
            return nil
        }
    }
    return declared
}

The parser intentionally declines ambiguous quoted escapes and later unquoted string arguments. It uses json.Number/decoder UseNumber to avoid rounding numeric literals. This is a conservative compatibility repair, not a general parser for arbitrary model-generated code.

Verification:

  • The independent fixture test fails on unpatched dev and passes with the patch (streaming and non-streaming).
  • GOTOOLCHAIN=auto go test -count=1 ./internal/translator/... passes.
  • GOTOOLCHAIN=auto go vet ./internal/translator/antigravity/openai/chat-completions/ passes.
  • git diff --check and formatting checks pass.
  • GOTOOLCHAIN=auto go build -o <external-build-path> ./cmd/server passes. The binary was not run.
  • This does not claim go test ./..., hosted CI, or a live-provider end-to-end test.

Related request-side work includes the schema-cleaning proposal #846 (closed without merging), merged fixes #1116 and #1131, and the closed report #1113. Their schema / VALIDATED changes aim to prevent upstream MALFORMED_FUNCTION_CALL responses; #1131 also includes native tool-call interoperability fixes. This proposal is not a duplicate of those changes: it translates a text-form call after the upstream response has already arrived. It does not request reopening those reports or reverting their fixes.

Other related but different work: #5338 handles truncated OpenAI-compatible streamed arguments; #6040 rejects nameless Responses tool calls; #4243 deduplicates request-side function declarations.

A companion reference PR targeting dev will be linked here once opened; maintainers are welcome to take over or cherry-pick the implementation, or close the PR under the repository's rules if they prefer the issue alone or do not accept this recovery strategy.

Source: router-for-me/CLIProxyAPI