CLIProxyAPI icon indicating copy to clipboard operation
CLIProxyAPI copied to clipboard

Responses route: reasoning sent as typed content-part arrays (delta.content / message.content) reaches the client as raw JSON text

Open Gaoshou101 opened this issue 3 weeks ago • 1 comments

Summary

When an OpenAI-compatible upstream (such as Mistral serving deepseek-v4-1-flash-1017, zai-glm-5-3, or zai-glm-5-3-flash) returns reasoning as typed content-part arrays inside choices[0].delta.content (streaming) or choices[0].message.content (non-streaming), the OpenAI Chat Completions to OpenAI Responses translator (internal/translator/openai/openai/responses/openai_openai-responses_response.go) treats content as a plain string via ojson.Result.String().

As a result, the raw JSON array representation of every thinking chunk is emitted verbatim into response.output_text.delta / output_text and concatenated into the assistant's visible text, while no reasoning output item or response.reasoning_summary_text.delta events are produced.

This is the /v1/responses counterpart to #7255 (which was fixed for the Claude /v1/messages response translator in commit 6044c5d14270fa094333b25932ac11ab13282821).

Evidence Chain & Observed Behavior

1. Upstream Streaming SSE Payload (api.mistral.ai/v1/chat/completions)

When reasoning occurs on Mistral (deepseek-v4-1-flash-1017 with reasoning_effort, or zai-glm-5-3 / zai-glm-5-3-flash by default), the upstream does not populate delta.reasoning_content. Instead, it streams typed content-part arrays in choices[0].delta.content, then switches back to plain strings once thinking closes:

data: {"id":"bf39068555d24f22aaa60dc43231a002","object":"chat.completion.chunk","created":1794374142,"model":"deepseek-v4-1-flash-1017","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"bf39068555d24f22aaa60dc43231a002","object":"chat.completion.chunk","created":1794374142,"model":"deepseek-v4-1-flash-1017","choices":[{"index":0,"delta":{"index":0,"content":[{"type":"thinking","thinking":[{"type":"text","text":"The"}],"closed":true}]},"finish_reason":null}]}

data: {"id":"bf39068555d24f22aaa60dc43231a002","object":"chat.completion.chunk","created":1794374142,"model":"deepseek-v4-1-flash-1017","choices":[{"index":0,"delta":{"index":0,"content":[{"type":"thinking","thinking":[{"type":"text","text":"erse."}],"closed":true},{"type":"text","text":"收到,链路"}]},"finish_reason":null}]}

data: {"id":"bf39068555d24f22aaa60dc43231a002","object":"chat.completion.chunk","created":1794374142,"model":"deepseek-v4-1-flash-1017","choices":[{"index":0,"delta":{"index":0,"content":"正常"},"finish_reason":null}]}

data: {"id":"bf39068555d24f22aaa60dc43231a002","object":"chat.completion.chunk","created":1794374142,"model":"deepseek-v4-1-flash-1017","choices":[{"index":0,"delta":{"index":0,"content":""},"finish_reason":"stop"}],"usage":{"prompt_tokens":31,"total_tokens":62,"completion_tokens":31,"completion_tokens_details":{"reasoning_tokens":21}}}

2. Upstream Non-Streaming JSON Payload

Similarly, on non-streaming responses, choices[0].message.content is returned as a JSON array of typed parts:

{
  "id": "bd5377d8250c4022b9a1b4dcb258ec4b",
  "object": "chat.completion",
  "model": "deepseek-v4-1-flash-1017",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "tool_calls": null,
        "content": [
          {
            "type": "thinking",
            "thinking": [
              {
                "type": "text",
                "text": "We need answer to \"test\". Respond concise."
              }
            ],
            "closed": true
          },
          {
            "type": "text",
            "text": "Test received. How can I help?"
          }
        ]
      }
    }
  ]
}

3. Root Cause in openai_openai-responses_response.go

  • Streaming (ConvertOpenAIChatCompletionsResponseToOpenAIResponses, line 965):
    if c := delta.Get("content"); c.Exists() && c.String() != "" {
        ...
        _ = msgDoc.Set("delta", c.String())
        st.MsgTextBuf[idx].WriteString(c.String())
    }
    
    When c is an ojson array, c.String() returns the raw JSON text of the array ([{"type":"thinking",...}]). Each chunk's raw JSON array is emitted as response.output_text.delta and concatenated into the final assistant message:
    [{"type":"thinking","thinking":[{"type":"text","text":"The"}],"closed":true}]...[{"type":"thinking","thinking":[{"type":"text","text":"erse."}],"closed":true},{"type":"text","text":"收到,链路"}]正常
    
  • Non-Streaming (ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream, lines 1134–1173): choices.0.message.reasoning_content and choices.0.message.reasoning are empty, while msg.Get("content").String() dumps the raw JSON array into content.0.text of the message output item.

Expected Behavior

  • Streaming (ConvertOpenAIChatCompletionsResponseToOpenAIResponses): When delta.content is an array, iterate through its parts in order:
    • Map "thinking" / "reasoning" parts (extracting nested text fragments or plain strings) to response.reasoning_summary_text.delta events within a reasoning output item.
    • Map "text" parts (and unknown fallback parts) to response.output_text.delta events within a message output item, closing any open reasoning item at the transition boundary.
  • Non-Streaming (ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream): When message.content is an array, extract "thinking" / "reasoning" parts as fallback reasoning text for the reasoning output item's summary_text, and join "text" parts (and unknown fallback parts) for the message output item's output_text.

Gaoshou101 avatar Nov 11 '26 11:11 Gaoshou101

Confirmed fixed in dev via commit c3a6c741 (translate typed thinking content-part arrays in responses translator), including reasoning-only stream finalization on explicit finish_reason. Verified with the upstream test suite — all translator tests pass. Thanks @luispater for the merged implementation!

Gaoshou101 avatar Nov 12 '26 03:11 Gaoshou101