mastra-ai/mastra · error · Error
structuredOutput object is undefined
Error message
structuredOutput object is undefined
What it means
generateWithStructuredOutput calls the language model requesting structured output and asserts the result contains an `object`. When the model response's structured output field is undefined — despite resolving without throwing — this error is raised instead of returning undefined.
Source
Thrown at packages/memory/src/processors/observational-memory/extraction-runner.ts:81
## Extractors
${extractorInstructions}${priorLines.length > 0 ? `\n\n## Prior Extracted Values\n\n${priorLines.join('\n\n')}` : ''}`;
const values: Record<string, unknown> = {};
const failures: Array<{ slug: string; error: string }> = [];
const generateWithStructuredOutput = async (jsonPromptInjection?: boolean | 'system' | 'inline') => {
const output = await opts.agent.generate(prompt, {
structuredOutput: { schema, ...(jsonPromptInjection ? { jsonPromptInjection } : {}) },
...(opts.memory ? { memory: opts.memory } : {}),
...(opts.abortSignal ? { abortSignal: opts.abortSignal } : {}),
...(opts.requestContext ? { requestContext: opts.requestContext } : {}),
...opts.observabilityContext,
});
if (output.object === undefined) {
throw new Error('structuredOutput object is undefined');
}
return output.object;
};
let object: Record<string, unknown>;
let retryEmptyObject = false;
try {
object = await generateWithStructuredOutput();
retryEmptyObject = shouldRetryEmptyStructuredObject(object, structuredExtractors);
} catch (error) {
if (isAbortError(error, opts.abortSignal)) {
throw error;
}
try {
const fallbackJsonPromptInjection = coreFeatures.has('json-prompt-injection:inline') ? 'inline' : true;
object = await generateWithStructuredOutput(fallbackJsonPromptInjection);View on GitHub (pinned to 75dd419e61)
Solutions
- Switch to a model that supports structured output reliably (or enable JSON mode) in the extraction options
- Simplify the extraction schema / instructions so the model returns valid JSON
- Check for truncation (raise maxOutputTokens) and verify the provider response shape; add a retry around extraction
Example fix
// before
const values = await extractStructuredValues({ model: smallLocalModel, ... });
// after
const values = await extractStructuredValues({
model: 'gpt-4o', // supports structured output
maxOutputTokens: 4096,
...
}); Defensive patterns
Strategy: retry
Validate before calling
null
Try / catch
try {
object = await generateWithStructuredOutput(opts);
} catch (err) {
if (err instanceof Error && err.message === 'structuredOutput object is undefined') {
object = await generateWithStructuredOutput({ ...opts, model: structuredOutputCapableModel });
} else {
throw err;
}
} Prevention
- Use models known to support structured output / JSON mode for extraction
- Set sufficient maxOutputTokens so JSON responses are not truncated
- Keep extraction schemas small and explicit; test them against the chosen model
- Retry once on empty structured output before surfacing the failure
When it happens
Trigger: The LLM call completes but `output.object` is undefined: model ignored the JSON-schema instruction, the provider returned an empty/malformed structured result, or the response was truncated/aborted in a way that still resolved.
Common situations: Using a model that does not reliably support native structured output/JSON mode; very small or weak models with complex extraction schemas; provider API changes returning a different response shape; low maxOutputTokens truncating the JSON before it parses.
Related errors
- Structured output returned no object
- CursorSDKAgent does not support structuredOutput because the
- @mastra/livekit: reply generation runs through the Mastra ag
- Failed to rename session (${res.status})
- No result received from agent execution on iteration ${itera
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/10c359bf6a0e142b.
Report an issue: GitHub.