Fix invalid JSON in AI translate request bodies (#11965)#11974
Merged
Conversation
Json.EncodeJsonText only escaped backslash and quote and replaced the platform newline, leaving tabs and stray \n/\r as raw control characters. The OpenAI/ChatGPT (and sibling) translators build their request bodies by hand through this helper, so subtitle text containing a tab or a non-platform line ending produced an invalid JSON payload, which the API rejected with "We could not parse the JSON body of your request" (400). Escape all JSON control characters (\t, \b, \f and \u00XX) and normalize all line-break variants up front. Keep DecodeJsonText symmetric so the SE JSON subtitle format still round-trips, and also encode the model name in ChatGptTranslate for good measure. Fixes all AI translators that share the helper (Anthropic, Gemini, Groq, Mistral, DeepSeek, ...). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #11965
Problem
A user hit a
400 Bad Requestfrom the OpenAI API when translating:This is SE5 (stack trace shows
Features.Translate.AutoTranslateViewModel/MergeAndSplitHelper). The reported modelgpt-4o-miniis a red herring — a bad model returnsmodel_not_found, not a JSON-parse error.Root cause
The AI translators build their request bodies by hand via
Json.EncodeJsonText, which only escaped\and"and replacedEnvironment.NewLine. JSON forbids literal control characters (U+0000–U+001F) inside strings, but tabs and stray\n/\r(e.g. a non-platform line ending) passed through raw — producing an invalid JSON payload that OpenAI rejects. Because every AI translator (ChatGPT, Anthropic, Gemini, Groq, Mistral, DeepSeek, …) funnels through this one helper, they were all affected.Fix
Json.EncodeJsonText: normalize all line-break variants up front, and escape\t,\b,\fand\u00XXfor remaining control chars so the output is always valid JSON. Normal text (platform newlines, no control chars) encodes identically to before.Json.DecodeJsonText: handle\t \b \f \n \rso the SE JSON subtitle format still round-trips.ChatGptTranslate: also encode the model name (defense in depth).All 485 libse tests pass.
🤖 Generated with Claude Code