LangChain Mastery

Course Content

LangChain Mastery

7 sections · 109 lessons

How do you use LangChain to handle multi-lingual NLP tasks?


What you need to know

Make the language explicit

Do not hope the model will reply in the user's language. Tell it:

Python
from typing import Literalfrom pydantic import BaseModelfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import RunnablePassthroughclass Lang(BaseModel):    language: Literal["English", "Hindi", "Tamil", "Hinglish", "Other"]detect = ChatPromptTemplate.from_template(    "Which language is this message in?\n{question}") | small_llm.with_structured_output(Lang)answer_prompt = ChatPromptTemplate.from_messages([    ("system", "Answer in {language}. Keep product names, amounts and "               "account numbers exactly as written. Use only the articles:\n{articles}"),    ("human", "{question}"),])chain = (RunnablePassthrough.assign(language=lambda x: detect.invoke(x).language)         | RunnablePassthrough.assign(articles=lambda x: search(x["question"]))         | answer_prompt | llm | parser)

Detection uses a small, cheap model with a closed list of labels. A library like fasttext language ID is faster still, but struggles with Hinglish (Hindi written in the Latin alphabet), which is very common in Indian chat.

Retrieval across languages

If your help articles are in English and questions come in Hindi, an English-only embedder will return unrelated chunks without any error. Options: use a multilingual embedder, or translate the question to English before retrieval and answer in the user's language.

Costs and quality

  • Tokens: Devanagari, Tamil and other Indic scripts usually take more tokens per word than English, so the same message costs more and fills the context window sooner.
  • Quality varies by language: a model that scores well in English may be weaker in Odia or Assamese. Build a small test set per language.
  • Keep exact values untouched: tell the model not to translate names, amounts and IDs.

A real-life example

A bank's support bot over its help-centre docs served English only. When it opened to Hindi and Tamil users, first-week answer accuracy was 88% in English but 61% in Hindi. The trace showed retrieval was the problem: the English embedder returned random chunks for Hindi questions. Switching to a multilingual embedder raised Hindi accuracy to 83%. Hinglish questions ("mera card block ho gaya") were still weak, so the team added 150 Hinglish examples to their test set and a routing rule that translates Hinglish to English before retrieval. They also found Tamil answers cost about twice the tokens of English ones and adjusted the budget.

Follow-up questions to expect

  • "Translate everything to English, or work natively?" — Translation is simple and uses your English prompts, but adds latency and can lose meaning; native works better with strong multilingual models and embedders. Test both on your data.
  • "How do you evaluate?" — A per-language test set of real questions, with native speakers checking a sample; do not assume English scores carry over.
  • "What about mixed-language input?" — Treat code-mixed text such as Hinglish as its own category in detection and testing.