Generating Natural Code-Switched Text With AI
9 min read · updated August 11, 2026
Ask a model to “mix Spanish and English naturally” and it will write a Spanish sentence, then an English sentence, then a Spanish sentence. That is inter-sentential alternation. Bilingual speakers switch inside the clause, and getting a model to do it requires specifying the structure rather than the feeling.
What you get when you ask nicely
The default output has a recognisable shape:
Prompt: "Write a casual message mixing Spanish and English naturally,
about a delayed package."
Typical output:
"Hola! I wanted to let you know that mi paquete no ha llegado.
It was supposed to arrive yesterday. Estoy un poco preocupado.
Can you check on it? Gracias!"Read it as a bilingual speaker and three things are off. Every sentence is internally monolingual, so the switches all sit on sentence boundaries — the one place real switching is least marked. The Spanish sentences are complete translations rather than insertions, so nothing is borrowed. And the mix is roughly balanced, alternating neatly, which no speaker does: real code-switching has a clear matrix language with the other one inserted into it.
What a Spanish-English speaker actually writes looks more like:
"Hola, oye — mi package todavía no llega. Se supone que iba a llegar ayer y nada. Can you check? Ya estoy un poco worried porque lo necesito para el weekend."
Spanish is the matrix throughout. English supplies nouns (package, weekend), an adjective in predicate position (worried), and one short fixed phrase (can you check). The switches are inside clauses, and the English material is inserted into Spanish grammar rather than replacing it.
Why the model does that
Three causes stack, and only the third is really about the prompt.
The training data is mostly parallel, not mixed. Bilingual text on the web is overwhelmingly the same content presented twice — documentation, product pages, subtitles, government notices. Genuinely code-switched text is comparatively rare, lives in messaging and social media, and is under-represented in curated pretraining mixes. The model has seen far more translation than switching, so “two languages in one document” predicts alternating monolingual blocks.
Instruction tuning pushes toward one language per response. Assistant training rewards answering in the user’s language, and consistency of output language is something models are explicitly tuned for. Mid-sentence switching is close to a behaviour that was trained against, so there is real pressure to resolve toward a single language, and long generations tend to drift back to monolingual even when they start mixed.
“Naturally” specifies nothing. It is an adverb the model cannot act on. It carries no matrix language, no switch rate, no indication of which word classes get borrowed. The model fills the gap with the most probable interpretation of “mixed” in its training distribution, which is alternating translation.
The prompt that works
Replace the adverb with structure. The three parameters that matter are the matrix language, the word classes that switch, and an example:
You are writing a casual WhatsApp message from a bilingual Mexican-American speaker in Los Angeles. Rules: - Spanish is the matrix language. All grammar, verb conjugation, determiners and connectors are Spanish. - English supplies inserted nouns and short fixed phrases only: technology words, work words, and time expressions. - Switches happen INSIDE sentences, not between them. Do not write any sentence that is entirely English. - Do not translate anything. Never say the same thing twice in two languages. - Roughly one English insertion every 8-12 words. Example of the target style: "Oye, no pude entrar al meeting porque mi laptop se murió. Lo intenté como three times pero nada." Now write: a message to a friend cancelling weekend plans because of work.
The rule that does the most work is “do not write any sentence that is entirely English”. It is a negative constraint on the exact default behaviour, and it is checkable, so the model can comply with it. “Never say the same thing twice” is the second most useful, because the translation instinct is the other half of the failure.
Constraints worth adding
- Name the variety, not just the language pair. Mexican-American Spanglish, Puerto Rican Spanish in New York, and Miami Cuban Spanish mix differently. “Spanish-English” averages them into something that sounds like nowhere.
- Constrain the borrowed word classes explicitly. Nouns and fixed phrases switch readily; determiners, auxiliaries and inflectional morphology almost never do. Saying so prevents the characteristic artificial output where an English determiner appears in front of a Spanish noun.
- Give two or three examples, not one. A single example gets copied structurally — same switch positions, same insertion count. Several examples of varying density teach the distribution instead of the instance. This is the general point made in few-shot examples in the target language.
- Keep generations short. Mixing degrades with length as the model drifts back toward one language. Generate a few sentences at a time rather than a page.
- Expect worse results outside the best-resourced pairs. Spanish-English and Hindi-English are the pairs with the most public code-switched text. For a pair with little of it, examples carry almost the whole burden and output quality drops accordingly.
Checking the output is actually switched
Judging this by eye does not scale and monolingual reviewers cannot do it at all. Score the generations mechanically with the same token labelling used for analysis, described in detecting code-switching points, and compute two numbers:
- Multilingual Index (M-Index). A published measure of language distribution in a code-switched corpus, derived from the proportion of tokens in each language; 0 means monolingual, 1 means an even split. It tells you whether both languages are present but says nothing about where.
- Integration Index (I-Index). The proportion of token boundaries that are switch points. This is the one that catches the failure mode: alternating monolingual sentences score well on M-Index and badly on I-Index, which is exactly the signature you are trying to detect.
- Count fully monolingual sentences. Split on sentence boundaries and label each. If more than a small minority are entirely one language, the prompt has not taken, regardless of what the indices say.
- Have a speaker of the variety read a sample. The metrics detect structural failure. They cannot detect output that is structurally correct and reads as a parody, which is a real risk when generating a register that belongs to a community.
It is worth being clear about what this is for, because the most defensible use is not customer-facing copy. It is data generation: the reason code-switched models are hard to build is that labelled code-switched text is scarce, and synthetic generation is one of the few ways to enlarge a training or evaluation set. Used that way the quality bar is different — you need structural realism and diversity of switch position far more than you need the output to be charming, and the two indices above measure exactly that. Seed generation from real sentences in your own corpus rather than from nothing, so the topics and the vocabulary match the distribution you will deploy against.
The trap in synthetic training data is that a model trained on another model’s idea of code-switching learns that idea rather than the phenomenon. Keep a held-out evaluation set of real human text and never let synthetic examples into it. If accuracy rises on synthetic validation and stays flat on the real set, the model has learned your generator, and the fix is more real data rather than more generation.