Skip to content

[Bug]: Custom OpenAI-compatible provider shows 'Cannot read properties of undefined (reading 0)' before response #8714

@seunghan91

Description

@seunghan91

Bug Description

When using a custom OpenAI-compatible provider (BizRouter), the gateway shows an error message "Cannot read properties of undefined (reading '0')" before displaying the actual response. The response itself works correctly.

Environment

  • OpenClaw Version: 2026.2.3
  • Platform: Docker on Synology NAS (Linux)
  • Node Version: 22.22.0
  • Channel: Telegram

Configuration

{
  "models": {
    "mode": "merge",
    "providers": {
      "bizrouter": {
        "baseUrl": "https://bizrouter.ai/api/v1",
        "apiKey": "sk-br-v1-xxx",
        "api": "openai-completions",
        "models": [
          {
            "id": "openai/gpt-4o",
            "name": "GPT-4o via BizRouter",
            "input": ["text"],
            "contextWindow": 128000,
            "maxTokens": 16384
          }
        ]
      }
    }
  },
  "agents": {
    "defaults": {
      "model": {
        "primary": "bizrouter/openai/gpt-4o"
      }
    }
  }
}

Steps to Reproduce

  1. Configure a custom OpenAI-compatible provider with api: "openai-completions"
  2. Set the provider as the primary model
  3. Send a message via Telegram
  4. Observe the error message appears first, followed by the actual response

Expected Behavior

Only the AI response should be displayed, without the error message.

Actual Behavior

Two messages are sent to the user:

  1. First: Cannot read properties of undefined (reading '0')
  2. Second: The actual AI response (which is correct)

Provider Response Format

The provider returns standard OpenAI-compatible responses:

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1770196354,
  "model": "gpt-4o-2024-08-06",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Hello! How can I assist you today?"
    },
    "finish_reason": "stop"
  }],
  "usage": {"prompt_tokens": 8, "completion_tokens": 9, "total_tokens": 17}
}

Note: The response does NOT include tool_calls field since the model doesn't use tools.

Analysis

The error appears to be in the response parsing code, likely trying to access tool_calls[0] or similar when the field is undefined. The response content is still extracted correctly, which is why the actual message appears after the error.

Logs

Gateway logs show successful completion with no errors:

[agent/embedded] embedded run start: provider=bizrouter model=openai/gpt-4o
[agent/embedded] embedded run agent end: runId=xxx
[agent/embedded] embedded run done: durationMs=2354 aborted=false

Workaround

None currently. The functionality works but the error message is displayed to users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions