Budibase/budibase · error

Expected indexed request IDs to be an array

Error message

Expected indexed request IDs to be an array

What it means

parseIndexedRequestIds parses a stored/serialized string of indexed LiteLLM request IDs. It JSON.parses the value and requires the result to be an array; anything else (object, number, string, etc.) throws a plain Error("Expected indexed request IDs to be an array"). This guards against corrupted or wrongly-shaped persisted data for the agent log session index document.

Source

Thrown at packages/server/src/sdk/workspace/ai/agentLogs/shared.ts:280

function encodeKeyPart(value: string): string {
  return encodeURIComponent(value)
}

export function getSessionDocId(agentId: string, sessionId: string): string {
  const encodedAgentId = encodeKeyPart(agentId)
  const encodedSessionId = encodeKeyPart(sessionId)
  return `${DocumentType.AGENT_LOG_SESSION}${SEPARATOR}${encodedAgentId}${SEPARATOR}${encodedSessionId}`
}

export function parseIndexedRequestIds(value?: string): Set<string> {
  if (!value) {
    return new Set()
  }

  const parsed = JSON.parse(value)
  if (!Array.isArray(parsed)) {
    throw new Error("Expected indexed request IDs to be an array")
  }
  if (parsed.some(item => typeof item !== "string")) {
    throw new Error("Expected indexed request IDs to contain only strings")
  }

  return new Set(parsed)
}

export function getWorkspaceDbForEnvironment(environment: AgentLogEnvironment) {
  if (environment === "development") {
    return context.getDevWorkspaceDB()
  }
  if (environment === "production") {
    return context.getProdWorkspaceDB()
  }

  throw new HTTPError("Invalid environment query", 400)
}

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Inspect the stored document and rewrite the field as a JSON array of string IDs
  2. Fix the writer that persisted the wrong shape so it stores an array
  3. Wrap the parse call in try/catch and fall back to an empty Set or reindex the session

Example fix

// before
const ids = parseIndexedRequestIds(doc.requestIds) // throws on corrupted doc
// after
function safeParseIndexedRequestIds(value?: string): Set<string> {
  try {
    return parseIndexedRequestIds(value)
  } catch {
    return new Set()
  }
}
Defensive patterns

Strategy: type-guard

Validate before calling

function isStringArray(value: unknown): value is string[] {
  return Array.isArray(value) && value.every(item => typeof item === "string")
}

Type guard

function isStringArray(value: unknown): value is string[] {
  return Array.isArray(value) && value.every(item => typeof item === "string")
}

function safeParse(value?: string): Set<string> {
  if (!value) return new Set()
  const parsed: unknown = JSON.parse(value)
  return isStringArray(parsed) ? new Set(parsed) : new Set()
}

Try / catch

try {
  const ids = parseIndexedRequestIds(value)
} catch (err) {
  console.warn("Corrupt indexed request IDs, treating as empty", err)
  const ids = new Set<string>()
}

Prevention

When it happens

Trigger: Reading an AgentLogSessionIndexDoc (or other doc) whose request-IDs field holds JSON that parses to a non-array — e.g. an object {"id":...}, a bare number, or a JSON string like "\"abc\"".

Common situations: A document was hand-edited or written by an older/newer code version with a different shape; a migration wrote an object instead of an array; a client supplied a non-array value that got persisted verbatim.

Related errors


AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29). Data as JSON: /api/errors/d89545d83deb7722. Report an issue: GitHub.