Budibase/budibase · error · Error

Could not parse AI response as valid JSON.

Error message

Could not parse AI response as valid JSON.

What it means

After generateText runs with a JSON output schema, run() expects response.output.data to exist. If the model produced no usable structured output (null/undefined output or data), the step cannot return extracted data and throws this parse error.

Source

Thrown at packages/server/src/automations/steps/ai/extract.ts:282

      typeof file !== "string"
    ) {
      extractInput = await processAttachmentFile(file, llm)
    } else {
      throw new Error("Invalid file input – source and file type do not match")
    }

    const output = getOutputFromSchema(inputs.schema)
    const modelMessages = buildExtractModelMessages(extractInput)
    const providerOptions = llm.providerOptions?.(false)
    const response = await generateText({
      model: llm.chat,
      messages: modelMessages,
      providerOptions,
      output,
      experimental_download: downloadAssetsForExtract,
    })
    if (!response.output || response.output.data == null) {
      throw new Error("Could not parse AI response as valid JSON.")
    }
    const data = response.output.data

    if (!data.length) {
      throw new Error("Could not extract the requested data.")
    }

    return {
      data,
      success: true,
    }
  } catch (err: any) {
    console.error("Document extraction error:", err)
    return {
      success: false,
      data: {},
      response: automationUtils.getError(err),
    }

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Switch to a stronger model that reliably supports structured/JSON output
  2. Simplify or clarify the extraction schema and prompt instructions
  3. Verify the input document actually contains the content to extract
  4. Retry — transient model truncation can yield unparsable output
  5. Check that the selected provider/model combination supports AI SDK JSON output mode

Example fix

// before
model: weakModel, schema: veryDeepNestedSchema // prose output
// after
model: strongModel, schema: flatSchema, prompt with explicit "respond only with JSON"
Defensive patterns

Strategy: retry

Validate before calling

// pre-flight: confirm the model supports structured output
if (!modelSupportsJsonOutput(llm.model)) {
  throw new Error("Selected model does not support structured output")
}

Try / catch

try {
  const result = await extractStep.run(inputs, ctx)
} catch (err) {
  if (err.message.includes("Could not parse AI response as valid JSON")) {
    // retry once with a stronger model or simplified schema
  }
  throw err
}

Prevention

When it happens

Trigger: generateText returns response with output == null or output.data == null — typically the model returned prose instead of schema-conformant JSON, or output parsing failed for the chosen model/provider.

Common situations: Model too weak to follow the JSON schema; temperature/prompting yielding non-JSON; provider quirks where experimental_output parsing fails; empty or non-document input confusing the model; unsupported model chosen for structured output.

Related errors


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