---
title: "Reference: Language Detector | Processors"
description: "Documentation for the LanguageDetector in Mastra, which detects language and can translate content in AI responses."
packages:
  - "@mastra/core"
---

# 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"
});
```

## Constructor parameters

<PropertiesTable
  content={[
    {
      name: "options",
      type: "Options",
      description: "Configuration options for language detection and translation",
      isOptional: false,
    },
  ]}
/>

### Options

<PropertiesTable
  content={[
    {
      name: "model",
      type: "MastraModelConfig",
      description: "Model configuration for the detection/translation agent",
      isOptional: false,
    },
    {
      name: "targetLanguages",
      type: "string[]",
      description: "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')",
      isOptional: true,
      default: "['English', 'en']",
    },
    {
      name: "threshold",
      type: "number",
      description: "Confidence threshold for language detection (0-1). Only process when detection confidence exceeds this threshold",
      isOptional: true,
      default: "0.7",
    },
    {
      name: "strategy",
      type: "'detect' | 'translate' | 'block' | 'warn'",
      description: "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",
      isOptional: true,
      default: "'detect'",
    },
    {
      name: "preserveOriginal",
      type: "boolean",
      description: "Whether to preserve original content in message metadata. Useful for audit trails and debugging",
      isOptional: true,
      default: "true",
    },
    {
      name: "instructions",
      type: "string",
      description: "Custom detection instructions for the agent. If not provided, uses default instructions",
      isOptional: true,
      default: "undefined",
    },
    {
      name: "minTextLength",
      type: "number",
      description: "Minimum text length to perform detection. Short text is often unreliable for language detection",
      isOptional: true,
      default: "10",
    },
    {
      name: "includeDetectionDetails",
      type: "boolean",
      description: "Whether to include detailed detection info in logs",
      isOptional: true,
      default: "false",
    },
    {
      name: "translationQuality",
      type: "'speed' | 'quality' | 'balanced'",
      description: "Translation quality preference: 'speed' prioritizes fast translation, 'quality' prioritizes accuracy, 'balanced' balances between speed and quality",
      isOptional: true,
      default: "'quality'",
    },
    {
      name: "providerOptions",
      type: "ProviderOptions",
      description: "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' } }`)",
      isOptional: true,
      default: "undefined",
    },
  ]}
/>

## Returns

<PropertiesTable
  content={[
    {
      name: "id",
      type: "string",
      description: "Processor identifier set to 'language-detector'",
      isOptional: false,
    },
    {
      name: "name",
      type: "string",
      description: "Optional processor display name",
      isOptional: true,
    },
    {
      name: "processInput",
      type: "(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise<MastraDBMessage[]>",
      description: "Processes input messages to detect language and optionally translate content before sending to LLM",
      isOptional: false,
    },
  ]}
/>

## Extended usage example

```typescript title="src/mastra/agents/multilingual-agent.ts"
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.1",
  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](/docs/v1/agents/guardrails)
