🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(model-runtime): keep answer text when a delta carries reasoning and content (#19378)
Arvin Xu8a3e225247ac
diff --git a/packages/model-runtime/src/core/streams/openai/openai.test.ts b/packages/model-runtime/src/core/streams/openai/openai.test.tsindex 517a3836f59..9f73df46859 100644--- a/packages/model-runtime/src/core/streams/openai/openai.test.ts+++ b/packages/model-runtime/src/core/streams/openai/openai.test.ts@@ -2220,6 +2220,74 @@ describe('OpenAIStream', () => {);});+ // Providers that batch several tokens per SSE frame (e.g. custom OpenAI-compatible+ // proxies) can emit the tail of the reasoning and the head of the answer in the same+ // delta. Both have to survive — dropping the content there makes the reply start+ // mid-sentence. See the Venice AI reports.+ it('should keep content when a single delta carries both reasoning_content and content', async () => {+ const data = [+ {+ id: '1',+ object: 'chat.completion.chunk',+ created: 1737563070,+ model: 'qwen-3-6-plus',+ choices: [{ index: 0, delta: { role: 'assistant', reasoning_content: 'Let me think' } }],+ },+ {+ id: '1',+ object: 'chat.completion.chunk',+ created: 1737563070,+ model: 'qwen-3-6-plus',+ choices: [+ { index: 0, delta: { reasoning_content: '. Ready.', content: 'Dear Angela,' } },+ ],+ },+ {+ id: '1',+ object: 'chat.completion.chunk',+ created: 1737563070,+ model: 'qwen-3-6-plus',+ choices: [{ index: 0, delta: { content: ' I hear you.' } }],+ },+ ];++ const mockOpenAIStream = new ReadableStream({+ start(controller) {+ data.forEach((chunk) => {+ controller.enqueue(chunk);+ });++ controller.close();+ },+ });++ const protocolStream = OpenAIStream(mockOpenAIStream);++ const decoder = new TextDecoder();+ const chunks = [];++ // @ts-ignore+ for await (const chunk of protocolStream) {+ chunks.push(decoder.decode(chunk, { stream: true }));+ }++ expect(chunks).toEqual(+ [+ 'id: 1',+ 'event: reasoning',+ `data: "Let me think"\n`,+ 'id: 1',+ 'event: reasoning',+ `data: ". Ready."\n`,+ 'id: 1',+ 'event: text',+ `data: "Dear Angela,"\n`,+ 'id: 1',+ 'event: text',+ `data: " I hear you."\n`,+ ].map((i) => `${i}\n`),+ );+ });it('should handle reasoning in litellm', async () => {const data = [{diff --git a/packages/model-runtime/src/core/streams/openai/openai.ts b/packages/model-runtime/src/core/streams/openai/openai.tsindex 75379023ba5..66d80fdcd81 100644--- a/packages/model-runtime/src/core/streams/openai/openai.ts+++ b/packages/model-runtime/src/core/streams/openai/openai.ts@@ -524,10 +524,34 @@ const transformOpenAIStream = (}}+ // A provider that batches several tokens into one SSE frame can put the tail of the+ // reasoning and the head of the answer in the SAME delta. Returning only the reasoning+ // chunk there silently drops that first slice of the answer, so the user sees a reply+ // that starts mid-sentence (or without the opening the system prompt asked for).+ // Carry the reasoning along instead and let the content branch below run as usual.+ const carriedReasoning: StreamProtocolChunk[] = [];+if (typeof reasoning_content === 'string') {- return { data: reasoning_content, id: chunk.id, type: 'reasoning' };+ const reasoningChunk: StreamProtocolChunk = {+ data: reasoning_content,+ id: chunk.id,+ type: 'reasoning',+ };++ if (typeof content !== 'string' || content === '') return reasoningChunk;++ carriedReasoning.push(reasoningChunk);}+ // Prepends the deferred reasoning chunk (when there is one) to whatever the content+ // branch resolved to, preserving the reasoning -> text order within the frame.+ const withCarriedReasoning = (+ result: StreamProtocolChunk | StreamProtocolChunk[],+ ): StreamProtocolChunk | StreamProtocolChunk[] =>+ carriedReasoning.length === 0+ ? result+ : [...carriedReasoning, ...(Array.isArray(result) ? result : [result])];+if (typeof content === 'string') {// If content is an empty string but chunk has usage, prioritize returning usage (e.g., Gemini image-preview eventually returns usage in a separate chunk)if (content === '' && chunk.usage) {@@ -565,7 +589,9 @@ const transformOpenAIStream = (});}- return results.length > 0 ? results : { data: '', id: chunk.id, type: 'text' };+ return withCarriedReasoning(+ results.length > 0 ? results : { data: '', id: chunk.id, type: 'text' },+ );}// Remove <think> tag (no need to split, as content after <think> tag is all reasoning)@@ -611,7 +637,7 @@ const transformOpenAIStream = (type: streamContext?.thinkingInContent ? 'reasoning' : 'text',},];- return baseChunks;+ return withCarriedReasoning(baseChunks);}}@@ -628,16 +654,16 @@ const transformOpenAIStream = (type: 'base64_image' as const,})),);- return arr;+ return withCarriedReasoning(arr);}}// Determine return type based on current thinking mode- return {+ return withCarriedReasoning({data: thinkingContent,id: chunk.id,type: streamContext?.thinkingInContent ? 'reasoning' : 'text',- };+ });}}