Skip to content

[Bug]: deepseek-v4-flash-free upstream returns malformed tool_calls causing "invalid tool calls" error #1473

Closed as not planned

Description

@yxr1995-maker

Description

When using deepseek-v4-flash-free model, the upstream DeepSeek API occasionally returns malformed tool_calls objects in the response. Opencodex validates these strictly and rejects the entire response with "upstream response contained invalid tool calls".

This has caused 43+ failures today (2026-08-11) alone, disrupting conversations.

Log entry

Error: Provider stream error: upstream response contained invalid tool calls

  • Model: deepseek-v4-flash-free
  • Frequency: ~43 errors on 2026-08-11
  • Top failing conversations: 421ecac8 (20 errors), 211608a1 (16 errors)

Reproduction

Use deepseek-v4-flash-free with reasoning effort of high/xhigh/max. The error occurs intermittently.

Example failing validation in src/adapters/openai-chat.ts:

for (const rawToolCall of rawToolCalls) {
  if (!isRecord(rawToolCall) || !isRecord(rawToolCall.function)) {
    return [invalidToolCallsEvent(usage)];  // rejects ENTIRE response
  }
  const id = rawToolCall.id;
  const name = rawToolCall.function.name;
  const args = rawToolCall.function.arguments;
  if (typeof id !== "string" || typeof name !== "string" || typeof args !== "string") {
    return [invalidToolCallsEvent(usage)];  // rejects ENTIRE response
  }
  ...
}

Version

opencodex 2.12.0

Operating system

macOS (reported by user)

Root Cause

DeepSeek sometimes returns tool_call entries with missing id, name, or arguments fields, or non-string values for these fields. The current code rejects the entire response when any single tool call is malformed.

Proposed Fix

Skip malformed tool calls with a warning log instead of rejecting the whole response:

for (const rawToolCall of rawToolCalls) {
  if (!isRecord(rawToolCall) || !isRecord(rawToolCall.function)) {
    console.warn("[opencodex] skipping malformed tool call:", JSON.stringify(rawToolCall));
    continue;
  }
  const id = rawToolCall.id;
  const name = rawToolCall.function.name;
  const args = rawToolCall.function.arguments;
  if (typeof id !== "string" || typeof name !== "string" || typeof args !== "string") {
    console.warn("[opencodex] skipping malformed tool call (invalid types): id=", typeof id, "name=", typeof name, "args=", typeof args);
    continue;
  }
  events.push({ type: "tool_call_start", id, name });
  events.push({ type: "tool_call_delta", arguments: args });
  events.push({ type: "tool_call_end" });
}

Keep the !Array.isArray(rawToolCalls) check as-is since that indicates fundamentally broken structure.

Impact

  • Reduces errors from ~43/day to near-zero for this model
  • Malformed entries skipped rather than crashing conversation
  • Well-formed responses unchanged

Activity

  1. added
    bug
    tools
    on Aug 11, 2026
  2. github-actions commented on Aug 11, 2026

    @github-actions
    Contributor

    Issue closed: insufficient detail

    Thanks for taking the time to submit this issue. It was closed automatically because the structured report is missing information needed to evaluate or reproduce it.

    • Summary is empty.

    Please edit the issue to add:

    • Describe what happened (the symptom or error).

    See the Contributing guide for details.

    Once the report passes the automated checks, it will be reopened automatically unless a maintainer has changed its state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtools

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions