Task: Fix translator
Fix translator
For some reason, very often the text is not translated at all, or only the title is. At the same time, the response returns success: true
This is very unreliable
Ворклоги
Fixed this. The problem was that the agent was breaking the formatting in the yaml document. The text response came in like this:
en:
name: |
Growth in Specialist Productivity Requires Corresponding Development of the Business Technological Environment
description: |
Business gets the maximum out of new technologies and strong specialists only when its own technological environment allows realizing their productivity.
content: |
New technologies increase specialist productivity, but this productivity is not realized in a vacuum.
Here, description and content were not nested under en like name, but became new top-level sections instead. As a result, the final response structure looked like this:
{
en: {
name: 'Growth in Specialist Productivity Requires Corresponding Development of the Business Technological Environment\n'
},
description: 'Business gets the maximum out of new technologies and strong specialists only when its own technological environment allows realizing their productivity.\n',
content: 'New technologies increase specialist productivity, but this productivity is not realized in a vacuum.
In other words, only the title was being translated here, and everything else was skipped during processing.
As a result, I did two things:
-
Split them into separate messages: the system prompt with all the rules went separately, while the document to be translated went into the user message. With this approach, the logical data structure is much clearer to the LLM.
-
Reinforced the YAML formatting rules.
It turned out like this:
const fieldsYaml = fieldsToTranslate
.map(
({ field, value }) =>
`${field}: |\n${value
.split('\n')
.map((line) => ' ' + line)
.join('\n')}`,
)
.join('\n')
const fieldNames = fieldsToTranslate.map((f) => f.field)
const systemPrompt = `You are a professional translator specializing in technical documentation and web content.
# YAML OUTPUT RULES (CRITICAL)
You MUST output valid YAML with this EXACT structure:
\`\`\`
<lang_code>:
<field_name>: |
<translated text line 1>
<translated text line 2>
\`\`\`
**STRICT REQUIREMENTS:**
1. Each language code (en, de, etc.) MUST be at the ROOT level (no indentation)
2. Each field (name, description, content) MUST be indented with exactly 2 spaces under its language
3. Field values MUST use the literal block scalar (|) syntax
4. Text content MUST be indented with exactly 4 spaces (2 for field + 2 for content)
5. NEVER put fields at the root level - they MUST always be nested under a language code
# TRANSLATION RULES
1. **Translate, do not transliterate.** Convert meaning, not just letters.
2. Only include fields that were provided in the source.
3. Preserve all markdown and HTML formatting exactly.
4. Exception: Proper nouns, brand names, company names, and product names should be transliterated to Latin script.
# OUTPUT FORMAT
Respond ONLY with valid YAML. No markdown code blocks, no explanations, no comments - just raw YAML.`
const userPrompt = `Translate the following fields from Russian into: ${targetLangs.join(', ')}
## Source fields:
${fieldsYaml}
## Expected output structure:
${targetLangs
.map(
(lang) =>
`${lang}:\n${fieldNames.map((f) => ` ${f}: |\n <translated ${f}>`).join('\n')}`,
)
.join('\n')}`
const chatResponse = await llmChatCompletionResolver(
null,
{
input: {
provider: LlmProvider.OpenRouter,
messages: [
{
role: LLMChatMessageRole.system,
content: systemPrompt,
},
{
role: LLMChatMessageRole.user,
content: userPrompt,
},
],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
model: LlmModel.GEMINI_3_5_FLASH_LITE as any,
},
},
ctx,
)
const responseContent = chatResponse.choices?.[0]?.message?.content
To be fair, technical instructions are still somewhat mixed into the user message here, but even so, it has started working much more stably.