Repository navigation
fix(openai): preserve reasoning_content round-trip for thinking-mode models - #129
Merged
Merged
Conversation
…models OpenAI-compatible reasoning models (DeepSeek-R1, GLM-4.6 thinking, Qwen-QwQ) emit reasoning_content (CoT) on assistant messages and require it be echoed back. bili's OpenAI adapter dropped it (grep reasoning_content = 0), so every re-request after a proxy tool/compress got HTTP 400 'The reasoning_content in the thinking mode must be passed back to the API.' Mirror the existing Anthropic thinking handling: openaiToCore parses reasoning_content into a contentType:reasoning coreMessage; coreToOpenai re-emits it; adapter-openai parseStream captures delta.reasoning_content and forwards it + includes it in the re-request.
ranxianglei
force-pushed
the
2026-08-13_fix-openai-reasoning-content
branch
from
August 12, 2026 16:30
82c0c3e to
e5edd10
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
OpenAI-compatible reasoning models (DeepSeek-R1, GLM-4.6 thinking, Qwen-QwQ) emit
reasoning_content(chain-of-thought) and require it be echoed back on subsequent requests. After every proxy-tool/compress re-request, the upstream returned HTTP 400:bili's OpenAI adapter dropped it (
grep reasoning_content dist = 0). The Anthropic (thinking) and Responses paths already handled this — only OpenAI was missing.Fix
Mirror the existing Anthropic thinking handling for OpenAI
reasoning_content:src/bili-message.ts— addreasoningContent?: stringsidecar field (next tothinkingSignature).src/openai.tsopenaiToCore: when an incoming assistant message has non-emptyreasoning_content, emit a{contentType:"reasoning", text, reasoningContent}coreMessage before content/tool-calls.coreToOpenai: accumulate reasoning fromcontentType:"reasoning"coreMessages;flush()re-attachesreasoning_contenton the output assistant object (handles text+reasoning, tool_calls+reasoning, reasoning-only).src/loop/core.tsParsedStreamEvent+{kind:"reasoning"; delta; raw?};CompressLoopAdapter+ optionalemitReasoning?(delta):Buffer.runCompressLoopaccumulatesassistantReasoning; forwards to client (raw passthrough round-1 /emitReasoninground-2+); re-request reconstruction pushes the reasoning coreMessage before the text one socoreToOpenaire-emits it → no more 400.src/loop/adapter-openai.ts—parseStreamyields{kind:"reasoning", delta:delta.reasoning_content, raw}for non-emptyreasoning_content(independent of tool_calls/content);emitReasoningrebuilds thedelta:{reasoning_content}SSE chunk.Tests
tests/openai-reasoning-content.test.ts(+155): round-trip / reasoning+tool_calls / no-reasoning-unaffected / parseStream captures reasoning.342/342 tests pass, typecheck clean, build OK.