LanguageDetector
The LanguageDetector
is an input processor that identifies the language of input text and optionally translates it to a target language for consistent processing. This processor helps maintain language consistency by detecting the language of incoming messages and providing flexible strategies for handling multilingual content, including automatic translation to ensure all content is processed in the target language.
Usage example
import { openai } from "@ai-sdk/openai";
import { LanguageDetector } from "@mastra/core/processors";
const processor = new LanguageDetector({
model: openai("gpt-4.1-nano"),
targetLanguages: ["English", "en"],
threshold: 0.8,
strategy: "translate"
});
Constructor parameters
options:
Options
Configuration options for language detection and translation
Options
model:
MastraLanguageModel
Model configuration for the detection/translation agent
targetLanguages?:
string[]
Target language(s) for the project. If content is detected in a different language, it may be translated. Can be language name ('English') or ISO code ('en')
threshold?:
number
Confidence threshold for language detection (0-1). Only process when detection confidence exceeds this threshold
strategy?:
'detect' | 'translate' | 'block' | 'warn'
Strategy when non-target language is detected: 'detect' only detects language, 'translate' automatically translates to target language, 'block' rejects content not in target language, 'warn' logs warning but allows through
preserveOriginal?:
boolean
Whether to preserve original content in message metadata. Useful for audit trails and debugging
instructions?:
string
Custom detection instructions for the agent. If not provided, uses default instructions
minTextLength?:
number
Minimum text length to perform detection. Short text is often unreliable for language detection
includeDetectionDetails?:
boolean
Whether to include detailed detection info in logs
translationQuality?:
'speed' | 'quality' | 'balanced'
Translation quality preference: 'speed' prioritizes fast translation, 'quality' prioritizes accuracy, 'balanced' balances between speed and quality
Returns
name:
string
Processor name set to 'language-detector'
processInput:
(args: { messages: MastraMessageV2[]; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise<MastraMessageV2[]>
Processes input messages to detect language and optionally translate content before sending to LLM
Extended usage example
src/mastra/agents/multilingual-agent.ts
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { LanguageDetector } from "@mastra/core/processors";
export const agent = new Agent({
name: "multilingual-agent",
instructions: "You are a helpful assistant",
model: openai("gpt-4o-mini"),
inputProcessors: [
new LanguageDetector({
model: openai("gpt-4.1-nano"),
targetLanguages: ["English", "en"],
threshold: 0.8,
strategy: "translate",
preserveOriginal: true,
instructions: "Detect language and translate non-English content to English while preserving original intent",
minTextLength: 10,
includeDetectionDetails: true,
translationQuality: "quality"
})
]
});