{"record":{"id":"393102b8c8cafcfa","repo":"linshenkx/prompt-optimizer","slug":"messages-array-cannot-be-empty","errorCode":null,"errorMessage":"Messages array cannot be empty","messagePattern":"Messages array cannot be empty","errorType":"validation","errorClass":"RequestConfigError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/llm/adapters/abstract-adapter.ts","lineNumber":184,"sourceCode":"  ): Promise<void> {\n    this.validateImageUnderstandingRequest(request)\n    await this.doSendImageUnderstandingStream(request, config, callbacks)\n  }\n\n  // ===== 公共验证方法 =====\n\n  /**\n   * 验证消息数组格式\n   * @param messages 消息数组\n   * @throws {RequestConfigError} 当消息数组无效时\n   */\n  protected validateMessages(messages: Message[]): void {\n    if (!Array.isArray(messages)) {\n      throw new RequestConfigError('Messages must be an array')\n    }\n\n    if (messages.length === 0) {\n      throw new RequestConfigError('Messages array cannot be empty')\n    }\n\n    for (const msg of messages) {\n      if (!msg.role || !msg.content) {\n        throw new RequestConfigError('Each message must have role and content')\n      }\n\n      if (!['system', 'user', 'assistant', 'tool'].includes(msg.role)) {\n        throw new RequestConfigError(`Invalid message role: ${msg.role}`)\n      }\n\n      if (typeof msg.content !== 'string') {\n        throw new RequestConfigError('Message content must be a string')\n      }\n    }\n  }\n\n  protected validateImageUnderstandingRequest(request: ImageUnderstandingRequest): void {","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/llm/adapters/abstract-adapter.ts#L166-L202","documentation":"Thrown by validateMessages in the abstract LLM adapter when the messages array passed to sendMessage / sendMessageStream / sendMessageStreamWithTools has zero elements. The adapter refuses to build a provider request with no conversation content because every chat API requires at least one message. It is a client-side RequestConfigError raised before any network call.","triggerScenarios":"Calling sendMessage([], ...), sendMessageStream([]), or sendMessageStreamWithTools([]) with an empty array; building messages dynamically (e.g. mapping an empty history and forgetting a user prompt) and passing the result.","commonSituations":"Chat-history-driven apps where the history array is empty on first turn and the code forgets to prepend the user's message; filtering out all messages (e.g. by role) before sending; passing an uninitialized [] variable.","solutions":["Ensure at least one message is included, typically the user's prompt: messages = [{role:'user', content: prompt}]","If messages are built from history, append/prepend the current user turn after mapping history","Add a guard before calling send* to short-circuit when messages.length === 0"],"exampleFix":"// before\nawait adapter.sendMessage([], { model: 'gpt-4' })\n\n// after\nawait adapter.sendMessage([{ role: 'user', content: 'Hello' }], { model: 'gpt-4' })","handlingStrategy":"validation","validationCode":"if (!Array.isArray(messages) || messages.length === 0) {\n  throw new Error('Refusing to call LLM with no messages')\n}\nawait adapter.sendMessage(messages, opts)","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Always append the current user turn before sending","Guard messages.length > 0 before calling send* APIs"],"tags":["validation","messages","llm","empty-array"],"backgroundTag":"empty-messages-array","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}