[P1/高] openai-format tool 消息 content 可能被赋为 undefined,导致 API 400 #26

Open
opened 2026-07-21 21:55:53 +08:00 by thzxx · 0 comments
Owner

问题类型

缺陷 / 高 / LLM 适配器

文件位置

electron/harness/adapters/shared/openai-format.ts

问题描述

构建 OpenAI 兼容消息时:

const message = {
  role: 'tool',
  content: toolResult.content ?? undefined, // 错误
  tool_call_id: toolCall.id,
};

toolResult.content 为 null 或空时,赋为 undefined,序列化后字段消失。
OpenAI / DeepSeek / Agnes API 严格要求 tool 消息必须有 content 字段,缺失会返回 400:

Invalid value for 'messages[].content': field is required

影响

  • 工具失败后下次请求必然 400
  • 整个会话卡死

建议修复

const message = {
  role: 'tool',
  content: toolResult.content ?? '', // 空字符串而非 undefined
  tool_call_id: toolCall.id,
};

或保留错误信息:

content: toolResult.content ?? toolResult.error ?? '',

project_memory.md 已要求:

Assistant messages with tool_calls must set content to null (not empty string) to avoid API 400 errors

但 tool 角色消息与 assistant 不同,必须有非空 content。

## 问题类型 缺陷 / 高 / LLM 适配器 ## 文件位置 `electron/harness/adapters/shared/openai-format.ts` ## 问题描述 构建 OpenAI 兼容消息时: ```ts const message = { role: 'tool', content: toolResult.content ?? undefined, // 错误 tool_call_id: toolCall.id, }; ``` 当 `toolResult.content` 为 null 或空时,赋为 `undefined`,序列化后字段消失。 OpenAI / DeepSeek / Agnes API 严格要求 tool 消息必须有 `content` 字段,缺失会返回 400: ``` Invalid value for 'messages[].content': field is required ``` ## 影响 - 工具失败后下次请求必然 400 - 整个会话卡死 ## 建议修复 ```ts const message = { role: 'tool', content: toolResult.content ?? '', // 空字符串而非 undefined tool_call_id: toolCall.id, }; ``` 或保留错误信息: ```ts content: toolResult.content ?? toolResult.error ?? '', ``` project_memory.md 已要求: > Assistant messages with tool_calls must set content to null (not empty string) to avoid API 400 errors 但 tool 角色消息与 assistant 不同,必须有非空 content。
thzxx added the LLM?????? labels 2026-07-21 21:55:53 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MetonaTeam/metona-ai-desktop#26