> ## Documentation Index
> Fetch the complete documentation index at: https://arizeai-433a7140.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Language Detection

> Detect the primary natural language used throughout a given source text, which could be a conversation session.

## Overview

The **Language Detection** evaluator classifies the primary natural language of a conversation session. It chooses the language that carries the most substantive content — even when that share is below half — then maps it to a fixed label set.

Supported labels are `english`, `mandarin_chinese`, `hindi`, `spanish`, `french`, `arabic`, `portuguese`, `russian`, `japanese`, `german`, `korean`, `italian`, `indonesian`, `bengali`, and `turkish`, plus `mixed` and `other`. `mixed` applies when several languages are approximately equally prevalent and none clearly predominates. `other` covers a predominant language outside the supported list, and sessions that contain no identifiable natural language.

> [!NOTE]
>
> The evaluator is descriptive. It records which language is primary. It does not judge whether that language was correct, expected, or consistent between the user and the assistant.

## Supported Levels

| Level | Supported | Notes |
| - | - | - |
| **Span** | Yes | Apply when a span contains the full exchange to classify. |
| **Trace** | Yes | Useful when a trace holds one complete conversation. |
| **Session** | Yes | The intended level. Pass the user and assistant turns as one string. |

**Relevant span kinds:** AGENT, CHAIN, and LLM spans that preserve the conversation between the user and the assistant. Leave tool calls, tool results, and retrieved text out of the input.

## Input Requirements

| Field | Type | Description |
| - | - | - |
| `session` | `string` | User and assistant turns, in chronological order, as readable text. |

Pass both parties. Leave out tool calls, tool results, and retrieved documents. Natural-language prose inside comments counts. Code, JSON, markup, proper nouns, and lone loanwords do not decide the language on their own.

## Output Interpretation

The evaluator returns a `Score` with one label:

| Property | Value | Description |
| - | - | - |
| `label` | `"english"`, `"mandarin_chinese"`, `"hindi"`, `"spanish"`, `"french"`, `"arabic"`, `"portuguese"`, `"russian"`, `"japanese"`, `"german"`, `"korean"`, `"italian"`, `"indonesian"`, `"bengali"`, `"turkish"`, `"mixed"`, or `"other"` | Primary language of the session |
| `score` | `None` | The result is the label; there is no score |
| `explanation` | `string` | Judge reasoning. Present only when the evaluator is backed by a language model (`LLM`); decision models return a label with probabilities and no explanation. |
| `direction` | `"neutral"` | Neither higher nor lower scores are better |

Read `label`. `score` is `None`.

## Usage Examples

> [tab] Python
>
> ```python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> from phoenix.evals import LLM
> from phoenix.evals.metrics import LanguageDetectionEvaluator
>
> evaluator = LanguageDetectionEvaluator(
>     llm=LLM(provider="openai", model="gpt-6-luna"),
>     temperature=0.0,
> )
>
> scores = evaluator.evaluate({
>     "session": (
>         "User: My deploy keeps failing with an out-of-memory error.\n"
>         "Assistant: Lower the build parallelism or raise the memory limit."
>     ),
> })
>
> print(scores[0])
> # Score(name='language_detection', score=None, label='english', ...)
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> import { createLanguageDetectionEvaluator } from "@arizeai/phoenix-evals";
> import { openai } from "@ai-sdk/openai";
>
> const evaluator = createLanguageDetectionEvaluator({
>   model: openai("gpt-6-luna"),
> });
>
> const result = await evaluator.evaluate({
>   session:
>     "User: My deploy keeps failing with an out-of-memory error.\nAssistant: Lower the build parallelism or raise the memory limit.",
> });
>
> console.log(result.label); // "english"
> ```

## Using a Decision Model

The same evaluator can be backed by a **decision model** instead of a language model. A decision model picks a label directly and returns per-label probabilities (and a confidence when the provider reports one) but no explanation.

> [tab] Python
>
> ```python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> from phoenix.evals import DecisionModel
> from phoenix.evals.metrics import LanguageDetectionEvaluator
>
> evaluator = LanguageDetectionEvaluator(llm=DecisionModel(provider="openai", model="gpt-6-luna"))
>
> scores = evaluator.evaluate({
>     "session": (
>         "User: My deploy keeps failing with an out-of-memory error.\n"
>         "Assistant: Lower the build parallelism or raise the memory limit."
>     ),
> })
> print(scores[0])
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> import { createLanguageDetectionEvaluator } from "@arizeai/phoenix-evals";
> import { openai } from "@ai-sdk/openai";
>
> const evaluator = createLanguageDetectionEvaluator({
>   model: openai.decisionModel("gpt-6-luna"),
> });
> ```

## Viewing and Modifying the Prompt

The default prompt is maintained in the
[classification evaluator config](https://github.com/Arize-ai/phoenix/blob/main/prompts/classification_evaluator_configs/LANGUAGE_DETECTION_CLASSIFICATION_EVALUATOR_CONFIG.yaml).

> [tab] Python
>
> ```python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> from phoenix.evals import LLM, ClassificationEvaluator
> from phoenix.evals.metrics import LanguageDetectionEvaluator
>
> llm = LLM(provider="openai", model="gpt-6-luna")
> evaluator = LanguageDetectionEvaluator(llm=llm)
>
> print(evaluator.prompt_template)
>
> custom_evaluator = ClassificationEvaluator(
>     name="language_detection",
>     prompt_template=evaluator.prompt_template,
>     llm=llm,
>     choices=LanguageDetectionEvaluator.CHOICES,
>     direction="neutral",
> )
> ```

> [tab] TypeScript
>
> ```typescript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> import { createLanguageDetectionEvaluator } from "@arizeai/phoenix-evals";
> import { openai } from "@ai-sdk/openai";
>
> const evaluator = createLanguageDetectionEvaluator({ model: openai("gpt-6-luna") });
>
> // Inspect the default prompt.
> console.log(evaluator.promptTemplate);
>
> // Reuse or adapt it in a custom evaluator.
> const customEvaluator = createLanguageDetectionEvaluator({
>   model: openai("gpt-6-luna"),
>   promptTemplate: evaluator.promptTemplate,
> });
> ```

## Configuration

For model and provider options, see
[Configuring the LLM](https://arizeai-433a7140.mintlify.site/docs/phoenix/evaluation/how-to-evals/configuring-the-llm.md).

## Using with Phoenix

* [Evaluating Phoenix Traces](https://arizeai-433a7140.mintlify.site/docs/phoenix/tracing/how-to-tracing/feedback-and-annotations/evaluating-phoenix-traces.md)
* [Logging LLM Evaluations](https://arizeai-433a7140.mintlify.site/docs/phoenix/tracing/how-to-tracing/feedback-and-annotations/llm-evaluations.md)
* [Using Evaluators in Experiments](https://arizeai-433a7140.mintlify.site/docs/phoenix/datasets-and-experiments/how-to-experiments/using-evaluators.md)

## Benchmarks

On a 47-example synthetic benchmark overriding EVAL\_MODEL=gpt-4o, the default prompt achieves 0.96 accuracy, 0.97 macro precision, 0.95 macro recall, and 0.95 macro F1. See [language\_detection.eval.ts](https://github.com/Arize-ai/phoenix/blob/main/js/benchmarks/evals-benchmarks/src/language_detection.eval.ts) for the categorized example set.

On 50 passages sampled from [candradhipa/Language-Detection](https://huggingface.co/datasets/candradhipa/Language-Detection) — 10 each of English, French, Spanish, and Hindi; one each of Portuguese, Italian, Russian, Arabic, Turkish, and German; and one each of Swedish, Dutch, Greek, and Tamil (reported as `other`) — the same model and prompt achieve 1.00 accuracy, 1.00 macro precision, 1.00 macro recall, and 1.00 macro F1. That dataset has no Mandarin. See [language\_detection\_candradhipa.eval.ts](https://github.com/Arize-ai/phoenix/blob/main/js/benchmarks/evals-benchmarks/src/language_detection_candradhipa.eval.ts).

## API Reference

* **Python:** [LanguageDetectionEvaluator](https://arize-phoenix.readthedocs.io/projects/evals/en/latest/api/evals.html#module-phoenix.evals.metrics)
* **TypeScript:** [createLanguageDetectionEvaluator](https://arize-ai.github.io/phoenix/modules/_arizeai_phoenix-evals.llm.html)

## Related

* [User Friction Evaluator](https://arizeai-433a7140.mintlify.site/docs/phoenix/evaluation/pre-built-metrics/user-friction.md)
* [Refusal Evaluator](https://arizeai-433a7140.mintlify.site/docs/phoenix/evaluation/pre-built-metrics/refusal.md)
