langgenius/dify · error · ValidationError

${name} must contain non-empty strings

Error message

${name} must contain non-empty strings

What it means

Thrown by ensureStringArray() in validation.ts:63 as a ValidationError. After the array is confirmed non-empty and within the size cap, each item is checked: every element must be a string of length ≥ 1 after trim. A single empty/whitespace item or a non-string element fails the whole call.

Source

Thrown at sdks/nodejs-client/src/client/validation.ts:63

export function ensureOptionalBoolean(value: unknown, name: string): void {
  if (value === undefined || value === null) {
    return
  }
  if (typeof value !== 'boolean') {
    throw new ValidationError(`${name} must be a boolean when set`)
  }
}

export function ensureStringArray(value: unknown, name: string): void {
  if (!Array.isArray(value) || value.length === 0) {
    throw new ValidationError(`${name} must be a non-empty string array`)
  }
  if (value.length > MAX_LIST_LENGTH) {
    throw new ValidationError(`${name} exceeds maximum size of ${MAX_LIST_LENGTH} items`)
  }
  value.forEach((item) => {
    if (typeof item !== 'string' || item.trim().length === 0) {
      throw new ValidationError(`${name} must contain non-empty strings`)
    }
  })
}

export function ensureOptionalStringArray(value: unknown, name: string): void {
  if (value === undefined || value === null) {
    return
  }
  ensureStringArray(value, name)
}

export function ensureRating(value: unknown): void {
  if (value === undefined || value === null) {
    return
  }
  if (value !== 'like' && value !== 'dislike') {
    throw new ValidationError("rating must be either 'like' or 'dislike'")
  }

View on GitHub (pinned to ef8544b173)

Solutions

  1. Filter before sending: tagIds.filter((t): t is string => typeof t === 'string' && t.trim().length > 0).
  2. Validate each id at the source rather than relying on the SDK to reject the batch.
  3. Use a Set to dedupe after filtering.

Example fix

// before
await kb.listDatasets({ tagIds: raw.split(',') }) // 'a,,b' -> ['a','','b']

// after
const tagIds = raw.split(',').map(s => s.trim()).filter(Boolean)
if (tagIds.length) await kb.listDatasets({ tagIds })
Defensive patterns

Strategy: validation

Validate before calling

function cleanStringArray(value: unknown): string[] {
  if (!Array.isArray(value)) return []
  return value.filter((v): v is string => typeof v === 'string' && v.trim().length > 0)
}

Type guard

function isCleanStringArray(value: unknown): value is string[] {
  return Array.isArray(value) && value.every((v) => typeof v === 'string' && v.trim().length > 0)
}

Try / catch

try {
  await kb.listDatasets({ tagIds })
} catch (err) {
  if (err instanceof Error && /must contain non-empty strings/.test(err.message)) {
    const clean = (tagIds as unknown[]).filter((v): v is string => typeof v === 'string' && v.trim().length > 0)
    if (clean.length) await kb.listDatasets({ tagIds: clean })
  } else throw err
}

Prevention

When it happens

Trigger: Calling kb.listDatasets({ tagIds: ['tag-1', '', 'tag-3'] }) (one empty), { tagIds: ['tag-1', 42] } (non-string), { tagIds: [' '] } (whitespace). The forEach at validation.ts:61 throws on the offending item.

Common situations: Mapping objects to ids where some rows have no id (producing undefined then stringified to 'undefined' or filtered to ''); splitting a comma string that yields empty tokens ('a,,b'); mixed-type arrays from untyped JSON.

Related errors


AI-assisted analysis of langgenius/dify@ef8544b173 (2026-08-12). Data as JSON: /api/errors/1bbda8327faad385. Report an issue: GitHub.