I was recently in charge of ensuring the migration from a piece of code that translated text with a translation API to a setup where only LLMs were available. When I asked how to set up the translation, I was told to “just ask the model for translation”. I wish it was that simple 😅
Side note: this blogpost wasn’t written by an LLM, and to prove it, here is a picture of me writing it:

But yes, Claude proof-read it, given that, you know, I’m a French guy writing in English. And fun fact: the French translation was performed by Claude, but that’s easier as I’m able to re-read it 🫣
Context
(Yep, the real client context is not that one but I kept confidentiality)
The product is a Shiny app that allows you to search for a metal band, get the list of available albums, and print the lyrics of all the songs inside a web page. You can then download a pdf of the lyrics on your laptop. The content is stored inside the app as YAML files, where entries are in markdown.
We display on the page both an entry with the band bio (very simple, just {band} is a {style} band from {country}), and the lyrics, in their original language, and a translation in a handful of other languages. Given that these are metal lyrics, the topics can be very specific, and contain names that need to be kept in their original language, and other specificity like places, references, etc.
The content arrives in R as a list of markdown strings, so that it can be both displayed on the page and written to pdf with Quarto. Yes, there might be better solutions to do that, but we’re not here to discuss that. We’re here to talk about the transition from sending this R list of markdown strings to the Google Translation API, to sending it to an LLM.
The issue with LLM translation
Notes: for the sake of demonstration, the code in this section is just a prompt to the API with the model version and “Translate the following text from English to French.”
Notes bis: the outputs below are illustrative.
LLMs are non-deterministic
You can ask them twice the same thing, they might not respond the same. Which is not what we want for our use case, both because of the content and because of the technical format:
- The markdown should always be kept as is so that it’s rendered correctly,
- Some parts of the text should stay as is. For example, we might not want the lyrics of “We Are Motörhead” to translate the “Motörhead” part.
For example, here is a long sentence, passed twice to the same model:
> translate(
"The manager told the developer that she had to review the report with her team before the deadline,
so the client waiting by the bank asked whether he could bring his own light.",
model = "claude-sonnet-5-5"
) |> extract()
[1] "Le manager a dit à la développeuse qu'elle devait revoir le rapport avec son équipe avant la date limite, alors le client qui attendait près de la banque a demandé s'il pouvait apporter sa propre lumière."
> translate(
"The manager told the developer that she had to review the report with her team before the deadline,
so the client waiting by the bank asked whether he could bring his own light.",
model = "claude-sonnet-5-5"
) |> extract()
[1] "Le responsable a dit à la développeuse qu'elle devait revoir le rapport avec son équipe avant la date limite, donc le client attendant près de la banque a demandé s'il pouvait apporter sa propre lumière."
See? “manager” is translated “manager” the first time, and “responsable” the second time, and “so” “alors” then “donc”.
Another example that is crucial to what I was trying to do: the output might return a json object with names that vary:
# First time
> translate_section(
"Hello world."
)
$text
[1] "Bonjour le monde"
# Second time
> translate_section(
"Hello world."
)
$translation
[1] "Bonjour le monde"
Which makes it, well, hard to handle in R because when receiving the JSON and converting it to a list, we want the structure to always be the same.
All models are not made equal
Some LLMs can determine that something is ambiguous. Others don’t. Let’s play with the sentence: “I saw her duck”, which is ambiguous by design. Depending on the model you call, you’ll get one version (the animal), or the other (the action), or a large text stating that this is ambiguous.
With Claude Haiku 5.5
> translate(
+ "I saw her duck.",
+ model = "claude-haiku-5-5"
+ ) |> extract()
[1] "J'ai vu son canard."
With Claude Sonnet 5.5
> translate(
+ "I saw her duck.",
+ model = "claude-sonnet-5-5"
+ ) |> extract()
[1] "Je l'ai vu se baisser."
With Claude Opus 5.5
> translate(
+ "I saw her duck.",
+ model = "claude-opus-5-5"
+ ) %>% cat()
Possible translations:
- **Je l'ai vue se baisser.** — if "duck" means *lower her head/body*.
- **J'ai vu son canard.** — if "duck" means the animal belonging to her.
The fact that we receive different translations, for now, is fine: this is the behaviour we could have from a standard translation service. But getting the results from the last call is a bit more problematic.
LLM can add elements in their answers
One known issue with LLMs is that they can add context to a sentence, or just noise, like “sure”, or “here is”. That’s what “claude-haiku-5-5” did:
> translate(
+ "Hello",
+ model = "claude-haiku-5-5"
+ ) |> extract()
[1] "Hello can be translated to French as \"Bonjour.\""
Which is not what we want in a translation function. Another, subtle element, but most models will add punctuation if you send it a sentence without a final dot.
> translate(
+ "I see here dog",
+ model = "claude-sonnet-5-5"
+ ) |> extract()
[1] "Je vois son chien."
Output might not be predictable
Another issue that extends from the previous section: I found a case where asking for JSON returned the markdown fences:
> translate(
+ "I see here dog",
+ model = "claude-sonnet-5-5"
+ ) |> extract()
[1] "```json {'result':''..."
So, what do we do?
We would expect from a generative service to get different answers. I mean, we’re kind of used to the idea that if we ask Claude “generate me lyrics for a death metal song”, we’ll get a different answer.
But that’s not what we want from a translator: we need the response to be, if not fully predictable, at least structured, and constrained enough so that it can go through the rest of the pipeline (markdown rendering, Quarto, PDF) without anything breaking.
Spoiler: nothing below makes the model deterministic. It only shrinks what the model is allowed to answer, and detects when it goes out of bounds anyway.
Structured output
The first thing we need to be sure of: the output has a predictable structure, so that it can be handled in R without any problem.
The output of the LLM will be a string, so we need to be sure we use a structured, interoperable format. In other words, we need a JSON string, with fixed names. You can prompt your model all you want (“answer with a JSON object with a translation key”), you’ll get translation today, text tomorrow, and a JSON wrapped in markdown fences the day after. This is a real challenge, and Claude offers what is called structured outputs: you send a JSON schema with your request, and the API constrains the generation to match it. The documentation is here: https://platform.claude.com/docs/en/build-with-claude/structured-outputs
The schema is deliberately boring: one key, one string.
translation_schema <- list(
type = "object",
properties = list(
translation = list(
type = "string"
)
),
required = list(
"translation"
),
additionalProperties = FALSE
)
translate_text <- function(
text,
system_prompt,
model = "claude-sonnet-5-5"
) {
# ...
httr2::req_body_json(
list(
model = model,
max_tokens = 4096,
system = system_prompt,
messages = list(
list(
role = "user",
content = text
)
),
output_config = list(
format = list(
type = "json_schema",
schema = translation_schema
)
)
)
)
# ...
}
That way, the output Claude returns will be constrained to the given schema: a valid JSON with a translation key, in the text block of the response (content[[1]]$text in R), ready to be parsed with jsonlite::fromJSON().
Engineering the prompt to enforce things
A precise prompt can’t guarantee anything, but it removes most of the annoying behaviours we’ve seen above:
system_prompt <- paste(
c(
"You are a professional translator from English into French.",
"Return only the translation: no comment, no explanation, no alternative.",
"If the text is ambiguous, pick the most likely reading without mentioning it.",
"Keep the Markdown syntax as is (**bold**, _italic_, links) and translate the text inside it.",
"Do not add or remove punctuation.",
"Keep every [[KEEP_n]] token unchanged.",
"Always translate 'manager' as 'responsable'."
),
collapse = "\n"
)
The fun part was iterating over the model, sending things to be translated, and getting the error from it, so that I could enrich the prompt.
As you can see, each rule answers one failure from the previous section: “Hello can be translated to French as…” (rule 2), the “Possible translations” answer (rule 3), the missing final dot of “I see here dog” (rule 5), and the manager/responsable flip-flop (the last one: a mini glossary, for terms that must always be translated the same way).
Keeping some terms as they are
Remember “We Are Motörhead”? Telling the model “don’t translate band names” works most of the time anyway, but again, we rely on the model to guess them. The app already knows the band name and the album titles, so instead of asking the model to remember them, I take them out of the text before the call, as tokens ([[KEEP_1]], [[KEEP_2]]…), and put them back after, with a small dictionary:
|key | value |
|--- |--- |
|KEEP_1| Motörhead|
Checking what comes back
A prompt is a request, not a guarantee. So the last piece is a function that compares the source that is sent, and the translation that comes back: same number of markdown tokens, and all [[KEEP_n]] still there. I tried to make word count work as a metric, but the app actually has languages like german, in which the size can vary a lot compared to english.
Of course, this is a purely syntactical check, and won’t see wrong meanings. For that, we need the human eye anyway.
Why not translate once?
We could imagine a translation for the content for each combination of band/language/song and store it in the app. But of course, the real use case is a bit more complicated, and storing is practically impossible, given that the user can input text in the application that are included in the translation.
Conclusion
“Just ask the model for translation.”
Yes, that’s doable, but not as easy as it sounds. You can’t just throw a translation request at a model and expect things to work, and that’s inherently linked to the way LLMs work.
So I did my best, so that, you know, we can be “Motörhead” worldwide.


