{"record":{"id":"10c359bf6a0e142b","repo":"mastra-ai/mastra","slug":"structuredoutput-object-is-undefined","errorCode":null,"errorMessage":"structuredOutput object is undefined","messagePattern":"structuredOutput object is undefined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/memory/src/processors/observational-memory/extraction-runner.ts","lineNumber":81,"sourceCode":"\n## Extractors\n\n${extractorInstructions}${priorLines.length > 0 ? `\\n\\n## Prior Extracted Values\\n\\n${priorLines.join('\\n\\n')}` : ''}`;\n\n  const values: Record<string, unknown> = {};\n  const failures: Array<{ slug: string; error: string }> = [];\n\n  const generateWithStructuredOutput = async (jsonPromptInjection?: boolean | 'system' | 'inline') => {\n    const output = await opts.agent.generate(prompt, {\n      structuredOutput: { schema, ...(jsonPromptInjection ? { jsonPromptInjection } : {}) },\n      ...(opts.memory ? { memory: opts.memory } : {}),\n      ...(opts.abortSignal ? { abortSignal: opts.abortSignal } : {}),\n      ...(opts.requestContext ? { requestContext: opts.requestContext } : {}),\n      ...opts.observabilityContext,\n    });\n\n    if (output.object === undefined) {\n      throw new Error('structuredOutput object is undefined');\n    }\n\n    return output.object;\n  };\n\n  let object: Record<string, unknown>;\n  let retryEmptyObject = false;\n  try {\n    object = await generateWithStructuredOutput();\n    retryEmptyObject = shouldRetryEmptyStructuredObject(object, structuredExtractors);\n  } catch (error) {\n    if (isAbortError(error, opts.abortSignal)) {\n      throw error;\n    }\n\n    try {\n      const fallbackJsonPromptInjection = coreFeatures.has('json-prompt-injection:inline') ? 'inline' : true;\n      object = await generateWithStructuredOutput(fallbackJsonPromptInjection);","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/processors/observational-memory/extraction-runner.ts#L63-L99","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst values = await extractStructuredValues({ model: smallLocalModel, ... });\n\n// after\nconst values = await extractStructuredValues({\n  model: 'gpt-4o', // supports structured output\n  maxOutputTokens: 4096,\n  ...\n});","handlingStrategy":"retry","validationCode":"null","typeGuard":null,"tryCatchPattern":"try {\n  object = await generateWithStructuredOutput(opts);\n} catch (err) {\n  if (err instanceof Error && err.message === 'structuredOutput object is undefined') {\n    object = await generateWithStructuredOutput({ ...opts, model: structuredOutputCapableModel });\n  } else {\n    throw err;\n  }\n}","preventionTips":["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"],"tags":["observational-memory","llm","structured-output","extraction"],"backgroundTag":"empty-structured-output","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}