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
- Switch to a stronger model that reliably supports structured/JSON output
- Simplify or clarify the extraction schema and prompt instructions
- Verify the input document actually contains the content to extract
- Retry — transient model truncation can yield unparsable output
- 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
- Use models known to support structured/JSON output
- Keep extraction schemas simple and flat
- Write explicit prompt instructions to output only schema-conformant JSON
- Test the step with representative documents before production
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
- Invalid agent request outcome response
- Error generating tables
- LLM not available
- No response found
- AI user message must be a string
AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29).
Data as JSON: /api/errors/ba1f89f357fc6404.
Report an issue: GitHub.