“Just ask the model for translation”

Wait 5 sec.

[This article was first published on Rtask, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.You can read the original post in its original format on Rtask website by ThinkR here: “Just ask the model for translation”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-deterministicYou 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 equalSome 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 answersOne 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 predictableAnother 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 outputThe 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-outputsThe schema is deliberately boring: one key, one string.translation_schema