OpenRouter 结构化输出在翻译质量之前出现问题 — 生产环境的三层防御
📄 中文摘要
首次生产事故并非由于翻译质量不佳,而是因为 Markdown 代码块包裹了 JSON 响应。某天,错误通知蜂拥而至,用户界面在应显示翻译的地方渲染了空白块。原因在于模型开始“友好地”将其 JSON 响应包裹在代码块中,导致 JSON.parse() 立即失败,翻译功能因此中断。为了解决这一问题,构建了一个防御系统,以稳定 OpenRouter API 的结构化输出,主要针对格式错误的 JSON 响应,同时也涉及重试/回退机制和语言检测。
📄 English Summary
OpenRouter Structured Output Broke Before Translation Quality Did — 3 Layers of Defense for Production
The first production incident was not due to poor translation quality, but rather because a Markdown code fence wrapped the JSON response. One day, error notifications flooded in as the UI rendered blank blocks where translations should appear. The cause was that the model had started being 'helpful' by wrapping its JSON responses in code fences, causing JSON.parse() to fail immediately and bringing down the translation feature. To address this issue, a defense system was built to stabilize structured output from the OpenRouter API in production, focusing on malformed JSON responses while also covering retry/fallback mechanisms and language detection.
Powered by Cloudflare Workers + Payload CMS + Claude 3.5
数据源: OpenAI, Google AI, DeepMind, AWS ML Blog, HuggingFace 等