ModerationProcessor
The ModerationProcessor is a hybrid processor that can be used for both input and output processing to provide content moderation using an LLM to detect inappropriate content across multiple categories. This processor helps maintain content safety by evaluating messages against configurable moderation categories with flexible strategies for handling flagged content.
Usage exampleDirect link to Usage example
import { ModerationProcessor } from '@mastra/core/processors'
const processor = new ModerationProcessor({
model: 'openrouter/openai/gpt-oss-safeguard-20b',
threshold: 0.7,
strategy: 'block',
categories: ['hate', 'harassment', 'violence'],
lastMessageOnly: true,
})
Constructor parametersDirect link to Constructor parameters
options:
model:
errorStrategy?:
categories?:
threshold?:
strategy?:
instructions?:
includeScores?:
lastMessageOnly?:
chunkWindow?:
bufferSize is 0.bufferSize?:
0, streamed text is held back and moderated in one LLM call when it ends a sentence, reaches this many characters, or a non-text part arrives. This makes far fewer LLM calls and catches content split across chunks. Recommended for production streaming, for example bufferSize: 200. With 0, every text chunk gets its own LLM call.providerOptions?:
{ openai: { reasoningEffort: 'low' } })ReturnsDirect link to Returns
id:
name:
processInput:
processOutputStream:
Extended usage exampleDirect link to Extended usage example
Input processingDirect link to Input processing
import { Agent } from '@mastra/core/agent'
import { ModerationProcessor } from '@mastra/core/processors'
export const agent = new Agent({
id: 'moderated-agent',
name: 'moderated-agent',
instructions: 'You are a helpful assistant',
model: 'openai/gpt-5.6-sol',
inputProcessors: [
new ModerationProcessor({
model: 'openrouter/openai/gpt-oss-safeguard-20b',
categories: ['hate', 'harassment', 'violence'],
threshold: 0.7,
strategy: 'block',
instructions: 'Detect and flag inappropriate content in user messages',
includeScores: true,
}),
],
})
Output processing with batchingDirect link to Output processing with batching
For production streaming, set bufferSize (for example bufferSize: 200) so ModerationProcessor makes one moderation call per sentence instead of one per chunk. Buffering also catches content that is split across chunks. Text then reaches the client in sentence-sized pieces.
When using ModerationProcessor as an output processor, it's recommended to combine it with BatchPartsProcessor to optimize performance. The BatchPartsProcessor batches stream chunks together before passing them to the moderator, reducing the number of LLM calls required for moderation.
import { Agent } from '@mastra/core/agent'
import { BatchPartsProcessor, ModerationProcessor } from '@mastra/core/processors'
export const agent = new Agent({
id: 'output-moderated-agent',
name: 'output-moderated-agent',
instructions: 'You are a helpful assistant',
model: 'openai/gpt-5.6-sol',
outputProcessors: [
// Batch stream parts first to reduce LLM calls
new BatchPartsProcessor({
batchSize: 10,
}),
// Then apply moderation on batched content
new ModerationProcessor({
model: 'openrouter/openai/gpt-oss-safeguard-20b',
strategy: 'filter',
chunkWindow: 1,
}),
],
})