linshenkx/prompt-optimizer · error · RequestConfigError

Image at index ${index} is invalid

Error message

Image at index ${index} is invalid

What it means

Thrown by validateImageUnderstandingRequest during per-image validation when images[index] is null/undefined or not an object. Each element must be an image object (with a b64 field); primitives or holes in the array are rejected. Client-side RequestConfigError including the offending index.

Source

Thrown at packages/core/src/services/llm/adapters/abstract-adapter.ts:217

    }
  }

  protected validateImageUnderstandingRequest(request: ImageUnderstandingRequest): void {
    if (!request || typeof request !== 'object') {
      throw new RequestConfigError('Image understanding request cannot be empty')
    }

    if (typeof request.userPrompt !== 'string' || !request.userPrompt.trim()) {
      throw new RequestConfigError('Image understanding user prompt cannot be empty')
    }

    if (!Array.isArray(request.images) || request.images.length === 0) {
      throw new RequestConfigError('Image understanding request requires at least one image')
    }

    request.images.forEach((image, index) => {
      if (!image || typeof image !== 'object') {
        throw new RequestConfigError(`Image at index ${index} is invalid`)
      }

      if (typeof image.b64 !== 'string' || !image.b64.trim()) {
        throw new RequestConfigError(`Image at index ${index} is missing base64 data`)
      }
    })
  }

  // ===== 工具方法 =====

  /**
   * 处理<think>标签,分离推理内容和主要内容
   * 从现有service.ts中的processStreamContentWithThinkTags逻辑迁移
   *
   * @param content 原始内容
   * @returns 处理后的结果 {content: 主要内容, reasoning?: 推理内容}
   */
  protected processThinkTags(content: string): { content: string; reasoning?: string } {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Wrap each raw base64 string: images.map(b64 => ({ b64 }))
  2. Filter out null/undefined entries before calling: images.filter(Boolean)
  3. Add a type guard isImageInput to validate each element before the call

Example fix

// before
images: [base64String]

// after
images: [{ b64: base64String }]
Defensive patterns

Strategy: validation

Validate before calling

const images = request.images.filter(i => i && typeof i === 'object')
if (images.length === 0) throw new Error('no valid images')

Type guard

function isImageInput(i: unknown): i is { b64: string } {
  return !!i && typeof i === 'object' && typeof (i as { b64: string }).b64 === 'string'
}

Try / catch

null

Prevention

When it happens

Trigger: An array containing null, undefined, a string, or a number, e.g. images: [base64String] (a bare string instead of { b64 }), or sparse arrays from forEach/map chains.

Common situations: Passing raw base64 strings directly as array elements instead of wrapping in { b64 } objects; JSON payloads with null entries; map over failed uploads returning undefined.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/58209ce504024963. Report an issue: GitHub.