Skip to content

Tool Calls Fail with g4f API in OpenCode #3373

@marvin1099

Description

@marvin1099

When invoking a tool call (bash with ls -la) through the g4f bridge (OpenAI‑compatible API) the model returns a AI_TypeValidationError. The response payload contains a tool_calls array where the index field is missing and the arguments field is an object instead of a JSON‑encoded string. This triggers a validation error:

AI_TypeValidationError: Type validation failed: Value: {"id":"chatcmpl-ALk2SMK3YYALaGE5qB03aB2J3Hxs","object":"chat.completion.chunk","created":1772454039,"model":"gpt-oss:120b","provider":"Ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning":null,"tool_calls":[{"id":"call_2c7wibf7","function":{"name":"bash","arguments":{"command":"ls -la","description":"List files in current directory","timeout":120000,"workdir":"/home/marvin"}}}]},"finish_reason":null}]}.
Error message: [{"code":"invalid_union","errors":[[{"expected":"number","code":"invalid_type","path":["choices",0,"delta","tool_calls",0,"index"],"message":"Invalid input: expected number, received undefined"},{"expected":"string","code":"invalid_type","path":["choices",0,"delta","tool_calls",0,"function","arguments"],"message":"Invalid input: expected string, received object"}],[{"expected":"object","code":"invalid_type","path":["error"],"message":"Invalid input: expected object, received undefined"}]],"path":[],"message":"Invalid input"}]

Environment

Component Version / Details
NVIDIA provider GPT‑OSS 120B
g4f bridge (OpenCode) @ai-sdk/openai-compatible (npm)
Bridge base URL http://127.0.0.1:1337/v1
Model identifier gpt-oss-120b (mapped to g4f)
Tool definition bash with fields command, description, timeout, workdir

Steps to Reproduce

  1. Run "g4f api"

  2. Configure OpenCode with the g4f bridge as shown:

    {
      "ollama": {
        "npm": "@ai-sdk/openai-compatible",
        "name": "Gf4 (bridge)",
        "options": { "baseURL": "http://127.0.0.1:1337/v1" },
        "models": {
          "gpt-oss-120b": { "name": "gpt-oss-120b" }
        }
      }
    }
  3. Open opencode

  4. Send a chat request that includes a tool call like "run ls"

  5. in opencode i get the AI_TypeValidationError from above

Expected Behavior
The bridge should use a tool call without errors

Why
Don't know.

Impact
Tool calls cannot be used with the g4f bridge, breaking any workflow that relies on external functions (e.g., file listings, shell commands, API calls). Users see opaque validation errors instead of the expected tool execution.

Additional Notes

  • The issue appears to be isolated to the g4f API implementation (error appears in opencode only).
  • No errors are logged on the g4f api cli.

Reported it here first but apparently it may be a python issue.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingstale

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions