mastra-ai/mastra · error

SystemPromptScrubber requires a model for detection

Error message

SystemPromptScrubber requires a model for detection

What it means

SystemPromptScrubber detects (and redacts) embedded system-prompt text using an LLM-based detection agent, which itself needs a model. If `options.model` is missing, the constructor throws a plain Error because detection cannot run at all. All other options (strategy, patterns, redaction method) have defaults; the model does not.

Source

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

export class SystemPromptScrubber implements Processor<'system-prompt-scrubber'> {
  public readonly id = 'system-prompt-scrubber';
  public readonly name = 'System Prompt Scrubber';

  private strategy: 'block' | 'warn' | 'filter' | 'redact';
  private customPatterns: string[];
  private includeDetections: boolean;
  private instructions: string;
  private redactionMethod: 'mask' | 'placeholder' | 'remove';
  private placeholderText: string;
  private model: MastraModelConfig;
  private detectionAgent: Agent;
  private lastMessageOnly: boolean;
  private structuredOutputOptions?: SystemPromptScrubberOptions['structuredOutputOptions'];

  constructor(options: SystemPromptScrubberOptions) {
    if (!options.model) {
      throw new Error('SystemPromptScrubber requires a model for detection');
    }

    this.strategy = options.strategy || 'redact';
    this.customPatterns = options.customPatterns || [];
    this.includeDetections = options.includeDetections || false;
    this.redactionMethod = options.redactionMethod || 'mask';
    this.placeholderText = options.placeholderText || '[SYSTEM_PROMPT]';
    this.lastMessageOnly = options.lastMessageOnly ?? false;
    this.structuredOutputOptions = options.structuredOutputOptions;

    // Initialize instructions after customPatterns is set
    this.instructions = options.instructions || this.getDefaultInstructions();

    // Store the model for lazy initialization
    this.model = options.model;

    this.detectionAgent = new Agent({
      id: 'system-prompt-detector',

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Pass a model: `new SystemPromptScrubber({ model: 'openai/gpt-4o' })`.
  2. Confirm the variable holding your model is defined at construction time.
  3. If you only want pattern-based behavior, verify whether a non-LLM variant/config fits your use case before omitting the model.
  4. Wrap processor construction in config validation that asserts required options exist.

Example fix

// before
new SystemPromptScrubber({ strategy: 'redact' });
// after
new SystemPromptScrubber({ strategy: 'redact', model: 'openai/gpt-4o' });
Defensive patterns

Strategy: validation

Validate before calling

if (!options.model) throw new TypeError('SystemPromptScrubber: model is required');
const scrubber = new SystemPromptScrubber(options);

Type guard

function hasModel(o: SystemPromptScrubberOptions): o is SystemPromptScrubberOptions & { model: NonNullable<SystemPromptScrubberOptions['model']> } {
  return Boolean(o.model);
}

Try / catch

try {
  scrubber = new SystemPromptScrubber(opts);
} catch (e) {
  if (e.message.includes('requires a model')) {
    scrubber = new SystemPromptScrubber({ ...opts, model: DEFAULT_MODEL });
  } else throw e;
}

Prevention

When it happens

Trigger: `new SystemPromptScrubber({})` or `new SystemPromptScrubber({ customPatterns: [...] })` without `model`, or `model: undefined` from an uninitialized/failed model factory.

Common situations: Assuming custom regex patterns alone are enough (detection still uses the agent), model constructed behind a feature flag that was off, forgetting to pass the model after upgrading to a constructor signature that requires it.

Related errors


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