Skip to content

Route messages by language

Know what language a message is in before you act on it, and route it to someone, or something, that can read it.

General-purpose detectors mislabel short African-language messages: a Yorùbá greeting comes back as English, Hausa as “unknown”. Language identification covers 608 languages, and its default traffic mode is tuned for short, messy user input. This script checks a batch of messages in one call and assigns each a queue.

Terminal window
pip install requests
export OLAVERSE_KEY="your-key" # from https://console.olaverselab.com
python route.py
route.py
import os
import requests
URL = os.environ.get("OLAVERSE_URL", "https://api.olaverselab.com")
HEADERS = {"Authorization": f"Bearer {os.environ['OLAVERSE_KEY']}"}
def call(path, body):
r = requests.post(f"{URL}{path}", json=body, headers=HEADERS, timeout=120)
r.raise_for_status()
return r.json()
# ISO 639-3 codes, as the API returns them, to your queues.
QUEUES = {"yor": "yoruba-agents", "hau": "hausa-agents", "ibo": "igbo-agents", "eng": "english-agents"}
SURE = 0.6 # below this the text is usually too short to tell ("ok", "hi")
def route(messages):
# One batch call for up to 256 messages; results come back in the same order.
results = call("/v1/language-id/batch", {"texts": messages})["results"]
routed = []
for message, r in zip(messages, results):
if r["language"] == "zxx" or r["confidence"] < SURE:
queue = "general" # code, numbers, or too short
else:
queue = QUEUES.get(r["language"], "general")
routed.append((queue, r["language"], round(r["confidence"], 2), message))
return routed
if __name__ == "__main__":
messages = [
"Ina son in canza lambar wayata",
"Mo fẹ́ mọ ìgbà tí ẹrù mi máa dé",
"Biko, enyemaka m chọrọ maka akaụntụ m",
"Please cancel my order",
"12345",
]
for queue, language, confidence, message in route(messages):
print(f"{queue:15} {language:4} {confidence:5} {message}")

What /v1/language-id/batch returns for each message, in order. Values are illustrative.

{"results": [
{"language": "hau", "script": "Latn", "label": "hau_Latn", "language_name": "Hausa", "confidence": 0.98,
"top": [{"label": "hau_Latn", "language": "hau", "language_name": "Hausa", "score": 0.98}], "warnings": [],
"model": "lid-neural-608", "mode": "traffic", "usage": {"texts": 1}},
...
]}

The script prints one line per message, for example hausa-agents hau 0.98 Ina son in canza lambar wayata.

  • language is ISO 639-3 (yor, hau, ibo, swh); label adds the script (yor_Latn).
  • zxx means no language: the text is code, numbers or symbols, like 12345.
  • Low confidence on very short text (“ok”, “hi”) is expected; below SURE the script sends it to general. Route those by the conversation’s earlier messages if you have them.
  • Then decide: add the detected language to the state of a decision, or use it to pick the language of your reply.
  • Restore tone marks for display: messages typed without diacritics read better after POST /v1/diacritize with the detected language (Yorùbá, Igbo, Hausa and 8 others).
  • Fastest and cheapest: add "model": "lid-lite-608". For corpora where every language should count equally, "mode": "coverage".
  • Long messages: each text can be up to 1,000 characters; the first 1,000 are plenty to tell the language.