Skip to content

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

Closed as not planned

Description

@yxr1995-maker

Problem

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.

Root Cause

In src/adapters/openai-chat.ts (lines ~1144-1160), the validation does:

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
  }
  ...
}

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, instead of skipping just that entry.

Reproduction

Use deepseek-v4-flash-free with a reasoning effort of high/xhigh/max. The error occurs intermittently (not every request).

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 (not a record):", 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 field 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" });
}

Also keep the !Array.isArray(rawToolCalls) check as-is since that indicates a fundamentally broken response structure.

Impact

  • Reduces errors from ~43/day to near-zero for this model
  • Malformed entries are skipped rather than crashing the conversation
  • Existing behavior for well-formed responses is unchanged

Activity

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

    @github-actions
    Contributor

    Issue closed: use an issue template

    Thanks for taking the time to submit this issue. It was closed automatically because it was not opened with a recognized issue template (Bug report, Feature request, Documentation, or Provider compatibility). Freeform and API-opened issues cannot skip this check.

    • This looks like a bug report but it does not use the Bug report template headings (for example Description/Log entry instead of Summary).

    Please edit the issue to add:

    • Use the Bug report template, or edit this issue to include: Client or integration, Summary, Reproduction, Version, and Operating system.
    • Retitling with [Bug]: and applying the bug label alone is not enough without those section headings filled in.

    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

    tools

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions