🤯 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): tolerate null function.name in streaming tool_call deltas (#14139)
Arvin Xuba05c324898d
diff --git a/packages/model-runtime/src/helpers/parseToolCalls.test.ts b/packages/model-runtime/src/helpers/parseToolCalls.test.tsindex 9089228c2c7..8b33fd0b2d2 100644--- a/packages/model-runtime/src/helpers/parseToolCalls.test.ts+++ b/packages/model-runtime/src/helpers/parseToolCalls.test.ts@@ -163,6 +163,83 @@ describe('parseToolCalls', () => {]);});+ // LOBE-8199: NVIDIA NIM (z-ai/glm5, qwen3.5-MoE) and some proxies open a+ // tool_call with function.name=null as a start marker; the real name+ // arrives in a subsequent delta.+ it('should coerce null function.name on the first delta and patch it in from a later delta', () => {+ const chunk1 = [+ {+ index: 0,+ id: 'call_1',+ type: 'function',+ function: { name: null as any, arguments: '' },+ },+ ];+ const result1 = parseToolCalls([], chunk1);+ expect(result1).toEqual([+ { id: 'call_1', type: 'function', function: { name: '', arguments: '' } },+ ]);++ const chunk2 = [{ index: 0, function: { name: 'get_weather', arguments: '{"loc' } }];+ const result2 = parseToolCalls(result1, chunk2);+ expect(result2).toEqual([+ { id: 'call_1', type: 'function', function: { name: 'get_weather', arguments: '{"loc' } },+ ]);++ const chunk3 = [{ index: 0, function: { arguments: 'ation":"Paris"}' } }];+ const result3 = parseToolCalls(result2, chunk3);+ expect(result3).toEqual([+ {+ id: 'call_1',+ type: 'function',+ function: { name: 'get_weather', arguments: '{"location":"Paris"}' },+ },+ ]);+ });++ it('should accept a first chunk with null name when merging into existing tool calls', () => {+ const origin = [+ {+ id: 'call_a',+ type: 'function',+ function: { name: 'get_a', arguments: '{"x":1}' },+ },+ ];+ // Second parallel tool_call opens with null name at a fresh index+ const chunk = [+ {+ index: 1,+ id: 'call_b',+ type: 'function',+ function: { name: null as any, arguments: '' },+ },+ ];+ const result = parseToolCalls(origin, chunk);+ expect(result).toEqual([+ { id: 'call_a', type: 'function', function: { name: 'get_a', arguments: '{"x":1}' } },+ { id: 'call_b', type: 'function', function: { name: '', arguments: '' } },+ ]);+ });++ it('should not overwrite an already-resolved name with a later empty name delta', () => {+ const origin = [+ {+ id: 'call_1',+ type: 'function',+ function: { name: 'get_weather', arguments: '{"loc' },+ },+ ];+ const chunk = [{ index: 0, function: { name: '', arguments: 'ation":"Paris"}' } }];+ const result = parseToolCalls(origin, chunk);+ expect(result).toEqual([+ {+ id: 'call_1',+ type: 'function',+ function: { name: 'get_weather', arguments: '{"location":"Paris"}' },+ },+ ]);+ });+it('should throw error if incomplete tool calls data', () => {const origin = [{diff --git a/packages/model-runtime/src/helpers/parseToolCalls.ts b/packages/model-runtime/src/helpers/parseToolCalls.tsindex cc434831d52..ff199637dd5 100644--- a/packages/model-runtime/src/helpers/parseToolCalls.ts+++ b/packages/model-runtime/src/helpers/parseToolCalls.ts@@ -3,11 +3,25 @@ import { produce } from 'immer';import type { MessageToolCall, MessageToolCallChunk } from '../types';import { MessageToolCallSchema } from '../types';+// Some providers (e.g. NVIDIA NIM serving z-ai/glm5 and qwen3.5-MoE, plus some+// aihubmix-style proxies) emit the opening tool_call delta with+// `function.name = null` or `''` as a start-of-tool marker. The real name is+// carried by a subsequent delta. Passing `null` through the strict+// MessageToolCallSchema throws ZodError mid-stream and kills the entire+// operation. Coerce to '' so parsing succeeds; the merge logic below patches+// the name in once a later delta supplies it. See LOBE-8199.+const normalizeChunkForParse = <T extends Omit<MessageToolCallChunk, 'index'>>(chunk: T): T => {+ if (chunk.function && chunk.function.name == null) {+ return { ...chunk, function: { ...chunk.function, name: '' } };+ }+ return chunk;+};+export const parseToolCalls = (origin: MessageToolCall[], value: MessageToolCallChunk[]) =>produce(origin, (draft) => {// if there is no origin, we should parse all the value and set it to draftif (draft.length === 0) {- draft.push(...value.map((item) => MessageToolCallSchema.parse(item)));+ draft.push(...value.map((item) => MessageToolCallSchema.parse(normalizeChunkForParse(item))));return;}@@ -17,22 +31,29 @@ export const parseToolCalls = (origin: MessageToolCall[], value: MessageToolCallconst existingByIdIndex = item.id ? draft.findIndex((d) => d.id === item.id) : -1;if (existingByIdIndex !== -1) {- // Found existing tool call with same id - merge arguments+ // Found existing tool call with same id - merge arguments and (if the+ // first delta had null/empty name) patch in the name from this delta.if (item.function?.arguments) {draft[existingByIdIndex].function.arguments += item.function.arguments;}+ if (item.function?.name && !draft[existingByIdIndex].function.name) {+ draft[existingByIdIndex].function.name = item.function.name;+ }} else if (!draft?.[index]) {// No item at this index - insert new tool call- draft?.splice(index, 0, MessageToolCallSchema.parse(item));+ draft?.splice(index, 0, MessageToolCallSchema.parse(normalizeChunkForParse(item)));} else if (item.id && draft[index].id !== item.id) {// Different id at same index - this is a new parallel tool call (e.g., from Gemini)// Push to end of draft instead of overwriting- draft.push(MessageToolCallSchema.parse(item));+ draft.push(MessageToolCallSchema.parse(normalizeChunkForParse(item)));} else {- // Same index and same id (or no id) - merge arguments+ // Same index and same id (or no id) - merge arguments and patch name.if (item.function?.arguments) {draft[index].function.arguments += item.function.arguments;}+ if (item.function?.name && !draft[index].function.name) {+ draft[index].function.name = item.function.name;+ }}});});diff --git a/src/server/modules/AgentRuntime/RuntimeExecutors.ts b/src/server/modules/AgentRuntime/RuntimeExecutors.tsindex 48141451136..60ed2aabeb1 100644--- a/src/server/modules/AgentRuntime/RuntimeExecutors.ts+++ b/src/server/modules/AgentRuntime/RuntimeExecutors.ts@@ -941,26 +941,24 @@ export const createRuntimeExecutors = (// ===== 2. Then accumulate to AgentState =====const newState = structuredClone(state);- // Carry the persisted DB id so downstream executors (notably- // `request_human_approve`) can look up the parent assistant from- // `state.messages` without an extra DB round-trip. Without the id- // the lookup at `request_human_approve` (which filters on `m.id`)- // falls through to a DB query; when human-approve fires on the- // fresh LLM turn, both code paths miss and the op errors with- // "No assistant message found as parent for pending tool messages".- // Sanitize mirrors the DB write above — state.messages flows into the- // next LLM call payload, so the same poisoning risk applies here.- // See LOBE-7761.- const stateToolCalls =+ // state.messages flows into the next LLM call payload, so entries+ // must be safe for strict-provider history replay:+ // - drop tool_calls with empty name (undispatchable, and strict+ // providers 400 on nameless entries)+ // - coerce malformed JSON `arguments` to valid JSON+ const sanitizedToolCalls =tool_calls.length > 0- ? tool_calls.map((tc) => ({- ...tc,- function: {- ...tc.function,- arguments: sanitizeToolCallArguments(tc.function.arguments),- },- }))- : undefined;+ ? tool_calls+ .filter((tc) => !!tc.function.name)+ .map((tc) => ({+ ...tc,+ function: {+ ...tc.function,+ arguments: sanitizeToolCallArguments(tc.function.arguments),+ },+ }))+ : [];+ const stateToolCalls = sanitizedToolCalls.length > 0 ? sanitizedToolCalls : undefined;newState.messages.push({content,