My AI system kept randomly switching to French mid-answer and it took me way too long to figure out why

Reddit r/artificial / 4/21/2026

💬 OpinionDeveloper Stack & InfrastructureSignals & Early TrendsTools & Practical UsageModels & Research

Key Points

  • The author built a multilingual RAG system meant to answer in German or English based on the query language, but it frequently “drifted” into French mid-answer when French legal terms appeared in retrieved context.
  • Language detection by the LLM itself proved unreliable, including cases where the model inferred the query was French due to a named French court case.
  • A simple regex-based approach for determining the query language worked better: the system forces German if enough common German markers are found, otherwise it uses English.
  • The prompt included a strict constraint to output only the target language and explicitly forbade French, which the author says was the key factor preventing ongoing language contamination.
  • The author notes this source-document language contamination is a frustrating real-world issue in multilingual RAG and that few people discuss it.

I built a RAG system that needs to answer in German or English depending on the query language. Sounds simple. It was not.

The source documents are mostly in German but some contain French legal terminology, Latin phrases, and occasional English citations. What kept happening was the LLM would start answering in German, hit a French passage in the context, and just.. switch to French mid-paragraph. Sometimes it would blend German and French in the same sentence. Once it answered entirely in Italian and I still have no idea why.

I tried letting the LLM detect the query language itself. Unreliable. It would sometimes decide the query was in French because the user mentioned a French court case by name.

What actually worked was a dumb regex detector. I check the query for common German words (der, die, das, und, ist, nicht, mit, für, datenschutz, verletzung, etc). If enough German markers are present the response language is forced to German. Otherwise English. No fancy language detection library. Just pattern matching.

Then in the prompt I added a hard constraint: "Write your entire answer ONLY in {language}. Output must be German or English only. Never French, Spanish, Italian, or any other language. If the retrieved context is partly in another language, translate your answer into {language} only."

The "never French" part is doing heavy lifting. Without that explicit prohibition the model would drift back into French within a few days of testing. It's like the model sees French legal text in context and thinks "oh we're doing French now."

Anyone else building multilingual RAG systems running into this? The language contamination from source documents was the most annoying bug I dealt with and I've seen almost nobody write about it.

submitted by /u/Fabulous-Pea-5366
[link] [comments]