FlowiseAI/Flowise · error · Error

sourceIdKey should be null, a string or a function, got ${ty

Error message

sourceIdKey should be null, a string or a function, got ${typeof sourceIdKey}

What it means

Thrown by _getSourceIdAssigner() when the sourceIdKey option is neither null, a string, nor a function. The function builds a closure that extracts a source ID from each document for incremental cleanup; an invalid type (number, boolean, object, array) cannot be used as a key or extractor and is rejected at line 220.

Source

Thrown at packages/components/src/indexing.ts:220

        }

        if (!seen.has(hashedDoc.hash_)) {
            seen.add(hashedDoc.hash_)
            deduplicated.push(hashedDoc)
        }
    }
    return deduplicated
}

export function _getSourceIdAssigner(sourceIdKey: StringOrDocFunc | null): (doc: DocumentInterface) => string | null {
    if (sourceIdKey === null) {
        return (_doc: DocumentInterface) => null
    } else if (typeof sourceIdKey === 'string') {
        return (doc: DocumentInterface) => doc.metadata[sourceIdKey]
    } else if (typeof sourceIdKey === 'function') {
        return sourceIdKey
    } else {
        throw new Error(`sourceIdKey should be null, a string or a function, got ${typeof sourceIdKey}`)
    }
}

export const _isBaseDocumentLoader = (arg: any): arg is BaseDocumentLoader => {
    if ('load' in arg && typeof arg.load === 'function' && 'loadAndSplit' in arg && typeof arg.loadAndSplit === 'function') {
        return true
    }
    return false
}

interface IndexArgs {
    docsSource: BaseDocumentLoader | DocumentInterface[]
    recordManager: ExtendedRecordManagerInterface
    vectorStore: VectorStore
    options?: IndexOptions
}

/**

View on GitHub (pinned to abe4a8601a)

Solutions

  1. Set options.sourceIdKey to a metadata key string (e.g. 'source') or a (doc) => string function.
  2. Validate the config value's type before calling index() and coerce or reject it.
  3. If you do not need incremental cleanup, set cleanup to undefined or 'full' and omit sourceIdKey.
  4. Add a runtime assertion at the config-loading boundary so bad types are caught early with a clear message.

Example fix

// before
await index({ docsSource, recordManager, vectorStore, options: { cleanup: 'incremental', sourceIdKey: 0 } })

// after
await index({ docsSource, recordManager, vectorStore, options: { cleanup: 'incremental', sourceIdKey: 'source' } })
Defensive patterns

Strategy: type-guard

Validate before calling

function assertSourceIdKey(v: unknown): asserts v is string | ((doc: any) => string) | null {
  if (v !== null && typeof v !== 'string' && typeof v !== 'function') {
    throw new TypeError(`sourceIdKey must be string|function|null, got ${typeof v}`)
  }
}

assertSourceIdKey(options.sourceIdKey)
await index({ docsSource, recordManager, vectorStore, options })

Type guard

function isValidSourceIdKey(v: unknown): v is string | ((doc: any) => string) | null {
  return v === null || typeof v === 'string' || typeof v === 'function'
}

Prevention

When it happens

Trigger: Passing options.sourceIdKey as a number (e.g. 0), a boolean, an object, or an array to index(). The type check chain at lines 213-219 exhausts null/string/function and falls through to the throw.

Common situations: Loading options from JSON config where the sourceIdKey field is numeric or null-ish in a way that bypasses the truthiness guard at line 261 but still isn't a string/function. Programmatic misconfiguration. A typo passing the wrong variable (e.g. an index instead of a key name).

Related errors


AI-assisted analysis of FlowiseAI/Flowise@abe4a8601a (2026-08-12). Data as JSON: /api/errors/46379b6431e06324. Report an issue: GitHub.