Skip to content

[Bug]: anthropic adapter returns empty content (adapter_eof / 0 tokens) for streaming requests against upstreams that emit reasoning blocks (Kimi k3) #312

Closed

Description

@lijianmac

Summary

Custom providers using adapter: anthropic work fine for non-streaming requests, but streaming requests (the default for both Claude Code /v1/messages and Codex App /v1/responses) return empty content with 0 tokens, or response.incomplete with incomplete_details.reason: "adapter_eof".

The issue appears to be in the Responses SSE -> Anthropic SSE back-translation: upstreams that emit reasoning-type output blocks (Kimi k3) lose subsequent content_block events. Upstreams that do not emit reasoning blocks (Volcengine GLM) work on the same path.

Environment

  • opencodex: 2.7.33
  • macOS, Apple Silicon
  • Upstream: Kimi Coding API (https://api.kimi.com/coding), model k3
  • Provider config: adapter: anthropic, baseUrl: https://api.kimi.com/coding, key auth

Reproduction

  1. Direct upstream call, bypassing opencodex:

    • POST https://api.kimi.com/coding/v1/messages with stream:true
    • Returns 200 with the full SSE chain: message_start -> content_block_start -> content_block_delta -> content_block_stop -> message_stop
    • Visible text is returned correctly.
  2. Via proxy /v1/responses, non-streaming:

    • Returns 200 with correct output_text and usage.
  3. Via proxy /v1/responses, streaming:

    • Returns 200 but immediately emits response.incomplete
    • incomplete_details.reason: "adapter_eof"
    • Usage is all zero.
  4. Via proxy /v1/messages (Claude Code path):

    • Non-streaming returns 200 with content: [] and all-zero usage.
    • Streaming emits only message_start -> message_delta -> message_stop, with no content_block_* events and all-zero usage.

Observed usage/error record:

{
  "provider": "Kimi.K3.Mahorse",
  "model": "k3",
  "status": 502,
  "usage": {
    "inputTokens": 0,
    "outputTokens": 0,
    "totalTokens": 0
  },
  "errorCode": "upstream_server_error",
  "upstreamError": "Upstream stream ended unexpectedly without a terminal event (adapter_eof)"
}

Control group

The same config shape against Volcengine Ark GLM (adapter: anthropic, https://ark.cn-beijing.volces.com/api/coding) works fully, including streaming. GLM does not emit the same reasoning output block shape.

Workaround

Switching the provider to:

adapter: openai-chat
baseUrl: https://api.kimi.com/coding/v1

makes proxy /v1/responses streaming and non-streaming, plus proxy /v1/messages streaming, return 200 with correct visible text and usage.

Kimi Coding API supports Chat Completions but does not natively implement Responses (/v1/responses returns 404).

Expected behavior

Reasoning output blocks from anthropic-adapter upstreams should be translated, or at minimum skipped without aborting or dropping the visible text stream. The proxy should not yield empty content, zero-token usage, or adapter_eof when the upstream SSE stream completed successfully.

Activity

  1. Ingwannu commented on Jul 23, 2026

    @Ingwannu
    Owner

    Confirmed on current dev with a Kimi-compatible Anthropic SSE reproduction. The core failure is in OpenCodex's stream framing, not in Kimi's documented Anthropic endpoint.

    The Anthropic adapter currently processes only newline-terminated buffer lines. If a valid terminal message_stop frame is the final SSE line without a trailing newline, it remains stranded in the buffer at EOF. A pending usage snapshot sometimes masks this by synthesizing done; when the compatible upstream omits that snapshot, the adapter emits visible deltas but no terminal event, and the bridge correctly reports adapter_eof / zero terminal usage.

    I am fixing this by:

    1. making the SSE event state survive arbitrary reader chunk boundaries;
    2. flushing the final decoded buffer at EOF, including a terminal frame without a trailing newline;
    3. preserving compatible reasoning/thinking blocks without letting an unknown reasoning block suppress later visible text;
    4. adding regressions for split event:/data: chunks, reasoning followed by text, no usage snapshot, and no final newline;
    5. verifying both Responses streaming and Claude Messages translation paths plus the full suite.

    I will post the merged commit and validation results here, then close this issue once the fix is on dev.

  2. Ingwannu commented on Jul 23, 2026

    @Ingwannu
    Owner

    Fixed and merged into dev via #316 (merge commit af973e54).

    The Anthropic-compatible stream parser now uses a proper SSE decoder that preserves event/data state across arbitrary fetch chunks and dispatches the final record at EOF even when there is no trailing newline or blank delimiter. Compatible reasoning / reasoning_delta blocks are retained without suppressing later visible text. Both Responses and Claude Messages paths now receive a terminal completion instead of adapter_eof for the reproduced Kimi-style stream.

    Validation: 3,616 tests passed with 0 failures; focused adversarial SSE/Anthropic/bridge tests, typecheck, privacy scan, and all Linux/macOS/Windows CI checks passed. Closing as completed.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions