Handling Swahili and Sheng in production

Real Kenyan messages switch language mid-sentence. A system that assumes one language per message will misread customers and answer them in the wrong voice.

A laptop showing a test suite of mixed English, Swahili and Sheng messages with the expected reply language for each

The message comes in at 7:40 in the morning: "boss hiyo stock ya cement imeisha ama bado?" An English-language system sees a handful of unknown words and "cement" and "stock", guesses, and replies in formal English with a stock report the owner did not ask for. A Swahili-only system stumbles on "boss" and "stock". Neither is how a person in the shop would have answered: "Bado iko, bags 46."

Kenyan users write the way they speak. English, Swahili and Sheng, often in the same sentence, with spelling that varies from person to person and month to month. If you are building anything that reads or answers Kenyan messages, this is the normal case, not an edge case. We built Rai to answer in the language you typed in; this is what that takes underneath.

Do not route by a language detector#

The first instinct is to run a language detector, label each message "en" or "sw", and send it down a separate path. In practice that goes badly:

  • Detectors are trained on clean text. A message like "nimeshindwa ku-login" confuses most of them, and Sheng confuses them further.
  • Short messages carry too little signal. "Sawa" or "poa, send" gives a detector almost nothing.
  • Separate paths drift apart. If the Swahili path has its own prompts and its own logic, every fix has to be made twice, and one of them gets forgotten.

What works better is one path. Modern language models read mixed English, Swahili and Sheng reasonably well as they are. Keep one prompt, one set of tools and one set of business rules, and treat language as a property of the reply, not a fork in the code.

Decide the reply language explicitly#

"Reply in the user's language" sounds clear and is not. Which language is "boss hiyo stock ya cement imeisha ama bado"? Our rule, written into the system prompt and tested:

  1. Mirror the dominant language of the user's latest message. Mostly Swahili in, mostly Swahili out.
  2. Mirror the register. Casual in, casual out. Do not answer "poa, send" with a formal paragraph.
  3. Keep business terms as the user writes them. If they say "stock" and "order", do not translate them into Swahili words nobody in the shop uses.
  4. Numbers, amounts and product names stay exact, whatever the language. KES 4,200 is KES 4,200 in every reply.
  5. Respect an explicit preference. If a user says "niandikie kwa English", store it and use it until they change it.

Sheng needs care in replies. Reading it is one thing; writing it convincingly is another. A system trying too hard to sound like a matatu tout is worse than plain, friendly Swahili. Our default is to understand Sheng fully and reply in light, everyday Swahili or English, with a Sheng word only where the user's own words make it natural.

Normalise inputs where it helps, and only there#

Before matching product names or customer names against your records, a light normalisation step helps: lowercase, strip repeated letters ("sawaaa"), map common spellings ("cimenti", "simiti" to cement) through a synonym table that grows from real traffic. Do this for lookups, not for the text the model reads. The model is better at reading the original than your normaliser is at cleaning it.

The synonym table is a good example of rules beside the model: a confirmed mapping is a fact, so it lives in a table, not in a prompt.

Test with real messages, not invented ones#

The single most valuable asset is a test set built from real conversations, with personal details removed. Each case has the input, the expected reply language and register, and the facts the reply must contain. Ours covers:

  • Pure English, pure Swahili, and mixes in both directions.
  • Heavy Sheng, including slang that has changed meaning over time.
  • Very short messages: "sawa", "ok boss", "aje".
  • Numbers written as words in Swahili, and amounts written "4k" or "4,2".
  • Requests to switch language mid-conversation.

Run it on every prompt change, model change and release, several times per case, since model output varies. A case that passes two runs out of three is a failing case. This is the same discipline as regression tests for AI answers, with language as one more thing to assert.

Keep adding to it. Every time a user complains about a misread or an odd reply, that message becomes a test case before the fix is written.

What to do this week#

Export a hundred recent messages from your production logs, remove names and phone numbers, and label each with the language and register you would have wanted in the reply. Run them through your current system and score the replies. The failure pattern, whether it is short messages, Sheng or numbers, tells you where to start.

All posts

Want this working in your business?

We build the AI systems we write about - for retail, clinics, workshops and property businesses across Africa. Tell us what you run and we will be honest about where it helps.