linshenkx/prompt-optimizer · error · Error

Missing variables

Error message

Missing variables

What it means

The v1 schema requires a top-level `variables` array (which may legitimately be empty). This error means data.variables is not an Array at all — it is missing, null, or an object. Each entry is later mapped to {name, defaultValue}, so the array invariant is enforced before mapping.

Source

Thrown at packages/ui/src/composables/app/useAppPromptGardenImport.ts:570

    let promptText: string | undefined
    let promptMessages: ConversationMessage[] | undefined
    if (format === 'text') {
      const t = prompt && typeof prompt.text === 'string' ? prompt.text : ''
      if (!t.trim()) {
        throw new Error('Empty prompt.text')
      }
      promptText = t
    } else {
      const msgs = normalizeImportedConversationMessages(prompt?.messages)
      if (!msgs.length) {
        throw new Error('Empty prompt.messages')
      }
      promptMessages = msgs
    }

    if (!Array.isArray(data.variables)) {
      throw new Error('Missing variables')
    }
    const variablesForImport = data.variables
      .map((v): { name: string; defaultValue?: string } => {
        if (!isPlainObject(v)) return { name: '' }
        const name = typeof v.name === 'string' ? v.name.trim() : ''
        const defaultValue = typeof v.defaultValue === 'string' ? v.defaultValue : undefined
        return { name, defaultValue }
      })
      .filter((v) => isValidVariableName(v.name))

    const snapshotVariables = data.variables
      .map((v): GardenSnapshotVariable | null => {
        if (!isPlainObject(v)) return null
        const name = typeof v.name === 'string' ? v.name.trim() : ''
        if (!isValidVariableName(name)) return null

        const type =
          v.type === 'string' || v.type === 'number' || v.type === 'boolean' || v.type === 'enum'

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Ensure the payload includes "variables": [] (empty array) at minimum
  2. If variables arrive as an object map, convert with Object.values(variables) before import or fix the exporter
  3. Validate the full payload shape against the v1 schema before handing it to parseV1

Example fix

// before
{ ...payload, "variables": { "topic": { "defaultValue": "x" } } }

// after
{ ...payload, "variables": [ { "name": "topic", "defaultValue": "x" } ] }
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(raw?.variables)) { raw.variables = [] /* or reject */ }

Type guard

const hasVariablesArray = (d: unknown): boolean => isPlainObject(d) && Array.isArray(d.variables)

Try / catch

catch (e) { if (e.message === 'Missing variables') { payload.variables = []; retryImport(payload); } else throw e }

Prevention

When it happens

Trigger: data.variables is undefined, null, a plain object (e.g. a keyed map of variables), or a string.

Common situations: Exports that serialize variables as an object map ({"var1": {...}}) instead of an array; older/newer export versions; hand-written fixtures omitting the field because it looked optional.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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