mastra-ai/mastra · error

Structured output returned no object

Error message

Structured output returned no object

What it means

In `detectSystemPrompts`, the scrubber calls the model with a structured-output schema and expects `response.object` to contain the parsed detections. When the model returns no object (empty/abstained generation, refused output, or provider returning nothing parseable), it throws 'Structured output returned no object'. This is a runtime failure of the detection step, not a constructor-time config error.

Source

Thrown at packages/core/src/processors/processors/system-prompt-scrubber.ts:318

        this.strategy === 'redact'
          ? baseSchema.extend({
              redacted_content: z.string().describe('Redacted content').nullable(),
            })
          : baseSchema;

      let result: SystemPromptDetectionResult;
      if (isSupportedLanguageModel(model)) {
        const response = await this.detectionAgent.generate(text, {
          structuredOutput: {
            ...(this.structuredOutputOptions ?? {}),
            schema,
          },
          requestContext,
          ...observabilityContext,
        });

        if (!response.object) {
          throw new Error('Structured output returned no object');
        }
        result = response.object;
      } else {
        const standardSchema = toStandardSchema(schema as PublicSchema);
        const response = await this.detectionAgent.generateLegacy(text, {
          output: standardSchemaToJSONSchema(standardSchema),
          requestContext,
          ...observabilityContext,
        });

        result = response.object as SystemPromptDetectionResult;
      }

      return result;
    } catch (error) {
      console.warn('[SystemPromptScrubber] Detection agent failed:', error);
      return {
        detections: null,

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Retry with a more capable model that reliably supports structured output (e.g. gpt-4o class models).
  2. Check the raw response/finish reason and token usage to see whether the model refused or was truncated.
  3. Shorten or sanitize the input text and retry; ensure no content-filter triggers.
  4. Wrap the detect call in try/catch and fall back to `customPatterns`-only matching when LLM detection fails.

Example fix

// before
const detections = await scrubber.detectSystemPrompts(text);
// after
let detections;
try {
  detections = await scrubber.detectSystemPrompts(text);
} catch (e) {
  if (e.message.includes('returned no object')) detections = scrubber.matchCustomPatterns(text);
  else throw e;
}
Defensive patterns

Strategy: fallback

Type guard

function hasObject<T>(r: { object?: T }): r is { object: T } {
  return r.object !== undefined && r.object !== null;
}

Try / catch

try {
  result = await scrubber.detectSystemPrompts(text);
} catch (e) {
  if (e.message.includes('returned no object')) {
    result = []; // fall back to custom-pattern-only behavior
  } else throw e;
}

Prevention

When it happens

Trigger: The detection agent's generate call resolves with `response.object === undefined` — e.g. the model produced no valid JSON matching the schema, the provider returned an empty completion, or finish reasons like content-filter/refusal left `object` unset.

Common situations: Weak/underspecified models failing to emit schema-conformant JSON, models refusing to analyze prompts, streaming parse hiccups, or input text that trips safety filters.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/582c3c6b13af7321. Report an issue: GitHub.