# Language Mixing in Responses

## The Problem

AI mixes languages inappropriately—responding in wrong language, switching mid-response, or combining languages confusingly.

### Symptoms

* ❌ English query gets Spanish response
* ❌ Response switches languages mid-sentence
* ❌ Technical terms in wrong language
* ❌ Cannot maintain user's language
* ❌ Inconsistent language across conversation

### Real-World Example

```
User query (Spanish): "¿Cómo autenticar la API?"
Knowledge base: Mixed Spanish and English docs

AI response: "Para autenticar, use the API key in el header
Authorization: Bearer {token}. This permite access a los endpoints."

Mixed Spanish/English in single response
Confusing and unprofessional
```

***

## Deep Technical Analysis

### Language Detection Issues

**Query Language Ambiguity:**

```
Short queries hard to detect:
→ "API key" (Universal term)
→ "Configuration" (English? French? Spanish?)

Misdetection:
→ Treats as wrong language
→ Responds inappropriately
```

**Multilingual Content:**

```
Documents contain:
→ English technical terms
→ Local language explanations
→ Code examples (English keywords)

Retrieved context is already mixed:
→ LLM mirrors the mixing
→ Inconsistent output
```

### Code-Switching Behavior

**Technical Term Handling:**

```
Many technical terms universal:
→ "API", "OAuth", "JSON", "HTTP"
→ Used across all languages

Question: Should response:
→ Keep English terms? "use the API key"
→ Translate? "use the application programming interface key"
→ Most prefer keeping English technical terms
```

***

## How to Solve

**Detect query language and set explicit instruction ("Respond in Spanish") + allow English technical terms in non-English responses + use language-tagged chunks with preference boosting + implement language consistency checks post-generation + provide language selector in UI.** See [Language Handling](/rag-scenarios-and-solutions/llm/language-mixing.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.twig.so/rag-scenarios-and-solutions/llm/language-mixing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
