# 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

```typescript
import { LanguageDetector } from '@mastra/core/processors'

const processor = new LanguageDetector({
  model: 'openrouter/openai/gpt-oss-safeguard-20b',
  targetLanguages: ['English', 'en'],
  threshold: 0.8,
  strategy: 'translate',
  lastMessageOnly: true,
})
```

## Constructor parameters

**options** (`Options`): Configuration options for language detection and translation

**options.model** (`MastraModelConfig`): Model configuration for the detection/translation agent

**options.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')

**options.threshold** (`number`): Confidence threshold for language detection (0-1). Only process when detection confidence exceeds this threshold

**options.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

**options.preserveOriginal** (`boolean`): Whether to preserve original content in message metadata. Useful for audit trails and debugging

**options.instructions** (`string`): Custom detection instructions for the agent. If not provided, uses default instructions

**options.lastMessageOnly** (`boolean`): Whether to detect language only for the most recent message in the batch instead of checking every message. Use this to keep LLM calls flat as conversation history grows.

**options.minTextLength** (`number`): Minimum text length to perform detection. Short text is often unreliable for language detection

**options.includeDetectionDetails** (`boolean`): Whether to include detailed detection info in logs

**options.translationQuality** (`'speed' | 'quality' | 'balanced'`): Translation quality preference: 'speed' prioritizes fast translation, 'quality' prioritizes accuracy, 'balanced' balances between speed and quality

**options.providerOptions** (`ProviderOptions`): Provider-specific options passed to the internal detection agent. Use this to control model behavior like reasoning effort for thinking models (e.g., \`{ openai: { reasoningEffort: 'low' } }\`)

## Returns

**id** (`string`): Processor identifier set to 'language-detector'

**name** (`string`): Optional processor display name

**processInput** (`(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise<MastraDBMessage[]>`): Processes input messages to detect language and optionally translate content before sending to LLM

## Extended usage example

```typescript
import { Agent } from '@mastra/core/agent'
import { LanguageDetector } from '@mastra/core/processors'

export const agent = new Agent({
  id: 'multilingual-agent',
  name: 'multilingual-agent',
  instructions: 'You are a helpful assistant',
  model: 'openai/gpt-5.5',
  inputProcessors: [
    new LanguageDetector({
      model: 'openrouter/openai/gpt-oss-safeguard-20b',
      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',
    }),
  ],
})
```

## Related

- [Guardrails](https://mastra.ai/docs/agents/guardrails)