CLIProxyAPI icon indicating copy to clipboard operation
CLIProxyAPI copied to clipboard

bug(translator): non-stream /v1/responses drops reasoning_tokens for chat-completions upstreams

Open henlii opened this issue 1 month ago • 0 comments

Goal

Non-streaming POST /v1/responses should report usage.output_tokens_details.reasoning_tokens for upstreams that only expose /v1/chat/completions. It currently reports 0, while the streaming path reports the correct value for the same model and the same upstream.

Environment

  • CLIProxyAPI 8.0.8; the affected code is unchanged on current main
  • Upstream: Inception API (https://api.inceptionlabs.ai/v1), configured as an openai-compatibility provider, model mercury-3
  • The upstream has no Responses endpoint at all:
POST https://api.inceptionlabs.ai/v1/responses        -> 404 {"detail":"Not Found"}
POST https://api.inceptionlabs.ai/v1/chat/completions -> 200

Reproduction

Same prompt, same model, four paths:

Path output_tokens reasoning_tokens
Upstream /chat/completions directly 99 60
Through CPA /chat/completions 100 68
Through CPA /responses stream=true 269 242
Through CPA /responses stream=false 184 0

Chat-completions passthrough is correct. Streaming Responses is correct. Only non-streaming Responses loses the field.

Root cause

internal/translator/openai/openai/responses/openai_openai-responses_response.go, inside ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream (~line 1140):

resp, _ = sjson.SetBytes(resp, "usage.output_tokens", usage.Get("completion_tokens").Int())
// Reasoning tokens not available in Chat Completions; set only if present under output_tokens_details
if d := usage.Get("output_tokens_details.reasoning_tokens"); d.Exists() {
    resp, _ = sjson.SetBytes(resp, "usage.output_tokens_details.reasoning_tokens", d.Int())
}

The comment is incorrect: Chat Completions does carry a reasoning count, under completion_tokens_details.reasoning_tokens — the usual OpenAI shape, and what this upstream returns. The lookup only checks the Responses-shaped key, so for every chat-only upstream it misses and the field stays absent.

The streaming counterpart in the same file already handles both shapes (~line 346):

if v := usage.Get("output_tokens_details.reasoning_tokens"); v.Exists() {
    st.ReasoningTokens = v.Int()
} else if v := usage.Get("completion_tokens_details.reasoning_tokens"); v.Exists() {
    st.ReasoningTokens = v.Int()
}

So this is a stream/non-stream inconsistency rather than a missing feature.

Note that output_tokens itself is correct: the upstream completion_tokens already includes reasoning (184 = roughly 30 visible + about 154 reasoning). Nothing is double counted; only the breakdown is missing.

Intended implementation

Mirror the streaming fallback:

// Chat Completions upstreams report reasoning under completion_tokens_details;
// Responses-native upstreams report it under output_tokens_details. Accept both.
if d := usage.Get("output_tokens_details.reasoning_tokens"); d.Exists() {
    resp, _ = sjson.SetBytes(resp, "usage.output_tokens_details.reasoning_tokens", d.Int())
} else if d := usage.Get("completion_tokens_details.reasoning_tokens"); d.Exists() {
    resp, _ = sjson.SetBytes(resp, "usage.output_tokens_details.reasoning_tokens", d.Int())
}

Suggested regression test alongside openai_openai-responses_response_test.go: converting a non-stream chat body whose usage is

{"prompt_tokens":9,"completion_tokens":99,"total_tokens":108,
 "completion_tokens_details":{"reasoning_tokens":60}}

should produce usage.output_tokens_details.reasoning_tokens == 60.

Secondary observation (deliberately not bundled into this fix)

Inception also returns a top-level reasoning_summary field on its chat response. It was present but null in my samples, and the chat-to-Responses translator does not consume it, so reasoning content never becomes a reasoning output item. Whether other chat-only upstreams emit an equivalent summary field, and in what shape, is a separate question; folding it into a usage-mapping fix would widen the blast radius.

Happy to send a PR for the usage mapping once the direction is agreed. My account reports READ on this repository, so per AGENTS.md I am filing the issue with the intended change rather than editing internal/translator/ directly.

henlii avatar Oct 20 '26 02:10 henlii