proxy icon indicating copy to clipboard operation
proxy copied to clipboard

kimi-k2.6: empty response in Claude Code — thinking blocks require signature_delta + merged message_delta

Open lt-value opened this issue 6 months ago • 1 comments

Problem

Claude Code v2.1.148 with interleaved-thinking-2025-05-14 beta shows empty responses when using kimi-k2.6. The proxy receives a valid response from upstream and streams it back, but Claude Code silently discards it.

kimi-k2.5 works fine (no thinking blocks). kimi-k2.6 returns reasoning_content in every chunk.

Root causes identified

1. Thinking blocks require signature_delta SSE event

Claude Code's extended thinking beta requires a content_block_delta event with type: "signature_delta" before content_block_stop for each thinking block. Without it, the entire response is discarded.

Anthropic streaming format for extended thinking:

content_block_start  {type: "thinking"}
content_block_delta  {type: "thinking_delta", thinking: "..."}   ← repeated
content_block_delta  {type: "signature_delta", signature: "..."}  ← REQUIRED before stop
content_block_stop

The proxy currently emits content_block_stop immediately after thinking deltas, skipping the signature_delta step.

2. Two separate message_delta events instead of one

The proxy emits:

message_delta  {stop_reason: "end_turn"}          ← first (no usage)
message_delta  {usage: {input_tokens: N, ...}}    ← second (no stop_reason)
message_stop

Anthropic spec requires a single message_delta with both stop_reason and usage merged:

message_delta  {stop_reason: "end_turn", usage: {input_tokens: N, ...}}
message_stop

3. kimi-k2.6 rejects thinking and reasoning_effort API parameters

kimi-k2.6 returns thinking blocks naturally (via reasoning_content in chunks) but does not accept the OpenAI-style thinking: {type: "enabled"} or reasoning_effort parameters in the request body — it returns HTTP 400.

The proxy's HasThinkingBlocks() check fires on the second turn (because the first response contained thinking), and the proxy then sends these parameters → 400 → circuit breaker opens → all subsequent requests fail.

Fix: only send thinking/reasoning_effort API params for DeepSeek models or models with explicit config, not for kimi.

Minimal reproduction

# First turn works, second turn fails (thinking history in messages)
curl -X POST http://127.0.0.1:3456/v1/messages \
  -H 'anthropic-beta: interleaved-thinking-2025-05-14' \
  -d '{
    "model": "claude-opus-4-5-20251101",
    "max_tokens": 200,
    "stream": true,
    "thinking": {"type": "enabled", "budget_tokens": 5000},
    "messages": [
      {"role": "user", "content": "what is 2+2?"},
      {"role": "assistant", "content": [
        {"type": "thinking", "thinking": "Let me compute", "signature": "abc"},
        {"type": "text", "text": "4"}
      ]},
      {"role": "user", "content": "and 3+3?"}
    ]
  }'
# → HTTP 400 from kimi-k2.6

Suggested fixes

stream.go:

  • Before each content_block_stop for a thinking block, emit a signature_delta event with a non-empty placeholder signature
  • Merge stop_reason and usage into a single message_delta event

request.go:

  • Add an isKimiModel() or supportsThinkingAPI() check — only send thinking/reasoning_effort params for DeepSeek or explicitly configured models, not for kimi

Environment

  • Claude Code v2.1.148
  • oc-go-cc built from current master
  • kimi-k2.6 via OpenCode Go (accounts/fireworks/models/kimi-k2p6)

lt-value avatar May 22 '26 11:05 lt-value

I reproduced and fixed this locally. The problem was not only cache_control.

Root cause

For kimi-k2.6, OpenCode/Fireworks streams reasoning as OpenAI-style:

{"delta":{"reasoning_content":"..."}}

The proxy converted this into Anthropic thinking_delta, but the Anthropic extended-thinking SSE format was incomplete/incompatible with Claude Code.

Claude Code expects thinking blocks to follow the Anthropic streaming extended thinking format:

  • content_block_start with type: "thinking"
  • one or more thinking_delta events
  • a signature_delta event right before content_block_stop
  • then the text block

Anthropic docs mention this explicitly: https://platform.claude.com/docs/en/build-with-claude/streaming#thinking-delta

Without the proper signature_delta, Claude Code could silently discard the response or show an empty answer after "Cooked/Baked for Ns". I also found that Claude Code may throw Content block is not a thinking block unless the thinking content_block_start contains a non-empty placeholder signature.


Fix

In internal/transformer/stream.go:

Preserve reasoning_content as Anthropic thinking_delta. Start thinking blocks like:

{
  "type": "content_block_start",
  "index": 0,
  "content_block": {
    "type": "thinking",
    "thinking": "",
    "signature": "proxy-thinking-placeholder"
  }
}

Before closing a thinking block, emit:

{
  "type": "content_block_delta",
  "index": 0,
  "delta": {
    "type": "signature_delta",
    "signature": "proxy-thinking-placeholder"
  }
}

Then emit:

{"type":"content_block_stop","index":0}

Also add signature to the streaming delta type:

type Delta struct {
    Type        string `json:"type"`
    Text        string `json:"text,omitempty"`
    Thinking    string `json:"thinking,omitempty"`
    Signature   string `json:"signature,omitempty"`
    PartialJSON string `json:"partial_json,omitempty"`
    StopReason  string `json:"stop_reason,omitempty"`
}

One more bug showed up during real Claude Code testing: the stream fast-path extracted JSON text manually and did not unescape JSON strings, so stdout contained literal \n instead of real newlines. Fixing that requires unquoting the JSON string value before sending text_delta.


Verified

After rebuilding and restarting the proxy:

go test ./...
go build -o bin/oc-go-cc ./cmd/oc-go-cc
oc-go-cc stop
oc-go-cc serve --background

Real Claude Code smoke test through the proxy:

cc -p "Answer with exactly two lines: first OK, second DONE" \
  --permission-mode bypassPermissions \
  --output-format text \
  --debug-file /tmp/cc-kimi-debug.log

Output:

OK
DONE

Debug log no longer contains:

  • Error streaming
  • Content block is not a thinking block
  • falling back to non-streaming mode

lt-value avatar May 22 '26 11:05 lt-value

Fix submitted in #133. It adds the required non-empty signature_delta before thinking block stops, buffers finish reason and trailing usage into one terminal message_delta, and includes a regression confirming Kimi does not receive thinking request parameters without explicit model capability. The focused and full Go test suites pass.

goodnightzsj avatar Aug 12 '26 04:08 goodnightzsj