Language validator
language-validator detects the language of a request or a model response. It blocks the text, or records a warning, when the language is not allowed. For example, an assistant for English and French users can block Japanese input.
Text that the detector is unsure about passes, so this is not a strict language boundary. apply_to selects the phase. The runtime default is input, which checks requests only.
Configuration
Save this as policy-config.yaml. It allows English and French and blocks the other supported languages when the detection confidence is at least 0.5.
pack:
name: language-validator
version: "1.0.0"
enabled: true
policies:
chain:
- language-validator
policy:
language-validator:
allowed_languages:
- en
- fr
min_confidence: 0.5
action: block
apply_to: inputFields
| Field | Type | Default | Description |
|---|---|---|---|
allowed_languages | string[] | [] | Allowed codes. When not empty, every other detected code is a violation. |
denied_languages | string[] | [] | Rejected codes. Used only when allowed_languages is empty. |
min_confidence | number | 0.5 | Minimum confidence, from 0.0 to 1.0. A less confident detection passes. |
action | block or warn | block | warn allows the request and records a warning reason code. |
apply_to | input, output, or both | input | input checks the request. output checks the model response. both checks the request and the response. |
Lint rejects allowed_languages together with denied_languages, a code outside the supported set, and a min_confidence outside 0.0 to 1.0.
Phase and verdicts
| Item | Value |
|---|---|
| Input phase | The joined user, system, assistant, and tool messages of the request. Runs with apply_to: input or both. |
| Output phase | The text of the model response. Runs with apply_to: output or both. |
| Verdicts | allow, block |
| No violation | language-validator.ok |
| Block | language-validator.blocked.<code>, such as language-validator.blocked.ja |
| Warning | language-validator.warned.<code> |
Input phase with apply_to: output | language-validator.skipped_output_only |
Verify with text from your application
Test a full English sentence, a Japanese one, and a short word:
testing:
suites:
- name: language-validator
cases:
- name: allows-english
input: {messages: [{role: user, content: "Please summarize the quarterly report and list the three main risks for the team."}]}
expected: {verdict: allow, reason_code: ok}
- name: blocks-japanese
input: {messages: [{role: user, content: "四半期報告書を要約して、チームの主なリスクを三つ挙げてください。"}]}
expected: {verdict: block, reason_code: language-validator.blocked.ja}
- name: allows-short-text
input: {messages: [{role: user, content: "OK"}]}
expected: {verdict: allow, reason_code: ok}All three pass. The nested results show the detected code and confidence: OK is detected as English with a confidence of only 0.2, so it passes whatever the list says. Then test real paragraphs from your users, and start with action: warn to measure false matches before you block.
Supported language codes
| Code | Language |
|---|---|
en | English |
es | Spanish |
fr | French |
de | German |
zh | Chinese |
ja | Japanese |
ko | Korean |
ar | Arabic |
pt | Portuguese |
ru | Russian |
How it works
- In the input phase, with
apply_to: output, the control allows withlanguage-validator.skipped_output_onlyand stops. - In the input phase, the gateway joins the
user,system,assistant, andtoolmessages of the request. In the output phase, the gateway uses the text of the model response. - Local Unicode block and trigram rules detect the language and a confidence.
- A confidence below
min_confidencepasses. - With
allowed_languages, a code outside the list is a violation. Otherwise, a code indenied_languagesis a violation. - No violation:
allowwithlanguage-validator.ok. - A violation blocks with
action: block, or is allowed with a warning code withaction: warn. - The details record the detected language, the confidence, both lists, and
min_confidence.
Examples
English-only input gate
pack:
name: english-only
version: "1.0.0"
enabled: true
policies:
chain:
- language-validator
policy:
language-validator:
allowed_languages:
- en
min_confidence: 0.6
action: block
apply_to: inputRecord a warning for denied languages
pack:
name: routing-observability
version: "1.0.0"
enabled: true
policies:
chain:
- language-validator
policy:
language-validator:
denied_languages:
- ja
- ar
min_confidence: 0.4
action: warn
apply_to: inputLimits and failure behavior
- The schema gives
bothas the default ofapply_to, but the runtime usesinput. Setapply_toexplicitly. - For a streaming request,
outputandbothmake the gateway hold the stream until the response is complete. Then the gateway checks the response. A block ends the stream with an error frame. - Only the ten codes are recognized. Other languages get the closest code or a low confidence.
- Mixed or short text often scores below
min_confidenceand passes. - The input phase also checks
assistantandtoolmessages that the caller sends again in the request.
Next steps
- Prompt injection detection: the other main input check.
- PII detector: redact personal data in the same phase.