🤯 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): drop empty text parts from assistant turns (#21416)
YuTengjing5502f0b2af6d
diff --git a/AGENTS.md b/AGENTS.mdindex f337e7546b8..c3dee7147d4 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -92,7 +92,7 @@ Use the `acceptance` skill to decide whether the delivery needs product verifica- Changes to rendered UI text, including locale keys and translations, change product behavior and are not exempt. A test that only asserts key presence does not show that the page renders the text.- Reuse a completed acceptance that covers the delivered behavior. If its report and evidence exist only locally, inspect and upload them with `lh acceptance run ingest`; if already published, reuse the link. Do not rerun the product merely to open a PR or obtain a report URL.- For new or changed product behavior not covered by valid evidence, verify the affected outcomes on the real product, capture the required evidence, and publish the result. The skill owns reuse criteria and the execution workflow.-- Agent tool results shown in chat are Web UI outcomes even when the implementation is server-side. Verify the rendered tool card and parent reply, including the failure state when recovery is the goal.+- Agent tool results and model replies shown in chat are Web UI outcomes even when the implementation is server-side. Verify the rendered tool card and parent reply, including the failure state when recovery is the goal. A provider request-payload fix that stops a chat error (for example, an upstream 400 on a model switch) needs a before/after chat run; unit tests and a direct upstream probe do not replace it.When acceptance is required, put its published `https://app.lobehub.com/acceptance/<id>` link in the PR body. Tests, lint, and type-check remain separate quality gates; they do not replace product acceptance.diff --git a/packages/model-runtime/src/core/contextBuilders/openai.emptyTextParts.test.ts b/packages/model-runtime/src/core/contextBuilders/openai.emptyTextParts.test.tsnew file mode 100644index 00000000000..4c14a8f1bf0--- /dev/null+++ b/packages/model-runtime/src/core/contextBuilders/openai.emptyTextParts.test.ts@@ -0,0 +1,88 @@+import { describe, expect, it } from 'vitest';++import { convertOpenAIMessages } from './openai';++/**+ * Moonshot (official API and Novita) rejects `{ type: 'text', text: '' }` with 400+ * "text content is empty"; a tool-call-only turn with a signed reasoning block used+ * to become `[thinking, text: '']` and leave that empty part behind.+ */+describe('convertOpenAIMessages empty text parts', () => {+ it('should send null content for a signed tool-call-only assistant turn', async () => {+ const toolCalls = [+ {+ function: { arguments: '{}', name: 'activateSkill' },+ id: 'call_1',+ type: 'function',+ },+ ];+ const messages = [+ {+ content: [+ { signature: 'sig', thinking: '', type: 'thinking' },+ { text: '', type: 'text' },+ ],+ role: 'assistant',+ tool_calls: toolCalls,+ },+ ] as any;++ const result = await convertOpenAIMessages(messages);++ expect(result).toEqual([{ content: null, role: 'assistant', tool_calls: toolCalls }]);+ });++ it('should drop empty text parts but keep non-empty assistant parts', async () => {+ const messages = [+ {+ content: [+ { signature: 'sig', thinking: 'plan', type: 'thinking' },+ { text: '', type: 'text' },+ { text: 'Answer', type: 'text' },+ ],+ role: 'assistant',+ },+ {+ content: [+ { signature: 'sig', thinking: 'plan', type: 'thinking' },+ { text: undefined, type: 'text' },+ ],+ role: 'assistant',+ },+ ] as any;++ const result = await convertOpenAIMessages(messages);++ expect(result).toEqual([+ { content: [{ text: 'Answer', type: 'text' }], role: 'assistant' },+ { content: '', role: 'assistant' },+ ]);+ });++ it('should not send an empty array for assistant turns without content', async () => {+ const toolCalls = [+ { function: { arguments: '{}', name: 'search' }, id: 'call_2', type: 'function' },+ ];+ const messages = [+ { content: null, role: 'assistant', tool_calls: toolCalls },+ { content: [], role: 'assistant', tool_calls: toolCalls },+ { content: [], role: 'assistant' },+ ] as any;++ const result = await convertOpenAIMessages(messages);++ expect(result).toEqual([+ { content: null, role: 'assistant', tool_calls: toolCalls },+ { content: null, role: 'assistant', tool_calls: toolCalls },+ { content: '', role: 'assistant' },+ ]);+ });++ it('should keep user message parts unchanged', async () => {+ const messages = [{ content: [{ text: '', type: 'text' }], role: 'user' }] as any;++ const result = await convertOpenAIMessages(messages);++ expect(result).toEqual(messages);+ });+});diff --git a/packages/model-runtime/src/core/contextBuilders/openai.ts b/packages/model-runtime/src/core/contextBuilders/openai.tsindex 9d12ff8c8ac..e725001e380 100644--- a/packages/model-runtime/src/core/contextBuilders/openai.ts+++ b/packages/model-runtime/src/core/contextBuilders/openai.ts@@ -120,6 +120,30 @@ const isInternalThinkingContentPart = (content: OpenAICompatibleContentPart,): content is Extract<UserMessageContentPart, { type: 'thinking' }> => content.type === 'thinking';+const isEmptyTextContentPart = (content: OpenAICompatibleContentPart) =>+ content.type === 'text' && !(content as { text?: string }).text;++/**+ * Drops content parts an OpenAI-compatible upstream must not receive.+ *+ * Signed reasoning turns arrive as `[thinking, text]` even when the reply was+ * tool calls only, which leaves `{ type: 'text', text: '' }` once thinking is+ * removed. Moonshot (official API and aggregators such as Novita) rejects that+ * with 400 "text content is empty" and every retry of the topic fails, while+ * `content: null` is accepted. When nothing remains, use `null` for tool-call+ * turns and `''` otherwise.+ */+const filterOutgoingContentParts = (msg: any, parts: OpenAICompatibleContentPart[]) => {+ const filtered = parts.filter(+ (c) =>+ !isInternalThinkingContentPart(c) && (msg.role !== 'assistant' || !isEmptyTextContentPart(c)),+ );++ if (filtered.length > 0 || msg.role !== 'assistant') return filtered;++ return msg.tool_calls?.length ? null : '';+};+export const convertMessageContent = async (content: ConvertibleMessageContentPart,options?: ConvertMessageContentOptions,@@ -232,15 +256,20 @@ export const convertOpenAIMessages = async (// Explicitly map only valid ChatCompletionMessageParam fields// Exclude reasoning and reasoning_content fields as they should not be sent in requests+ const parts =+ typeof message.content === 'string'+ ? message.content+ : filterOutgoingContentParts(+ msg,+ (message.content || []) as OpenAICompatibleContentPart[],+ );+const result: any = {- content:- typeof message.content === 'string'- ? message.content- : await Promise.all(- (message.content || [])- .filter((c) => !isInternalThinkingContentPart(c as OpenAICompatibleContentPart))- .map((c) => convertMessageContent(c as ConvertibleMessageContentPart, options)),- ),+ content: Array.isArray(parts)+ ? await Promise.all(+ parts.map((c) => convertMessageContent(c as ConvertibleMessageContentPart, options)),+ )+ : parts,role: msg.role,};