Skip to content

Fix invalid JSON in AI translate request bodies (#11965)#11974

Merged
niksedk merged 1 commit into
mainfrom
fix/chatgpt-json-control-chars
Jun 29, 2026
Merged

Fix invalid JSON in AI translate request bodies (#11965)#11974
niksedk merged 1 commit into
mainfrom
fix/chatgpt-json-control-chars

Conversation

@niksedk

@niksedk niksedk commented Jun 29, 2026

Copy link
Copy Markdown
Member

Fixes #11965

Problem

A user hit a 400 Bad Request from the OpenAI API when translating:

"We could not parse the JSON body of your request… what was sent was not valid JSON"

This is SE5 (stack trace shows Features.Translate.AutoTranslateViewModel / MergeAndSplitHelper). The reported model gpt-4o-mini is a red herring — a bad model returns model_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 replaced Environment.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, \f and \u00XX for 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 \r so the SE JSON subtitle format still round-trips.
  • ChatGptTranslate: also encode the model name (defense in depth).
  • Added unit tests for tab/stray-newline/control-char encoding and round-trip.

All 485 libse tests pass.

🤖 Generated with Claude Code

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>
@niksedk niksedk merged commit 76ff7cb into main Jun 29, 2026
2 of 3 checks passed
@niksedk niksedk deleted the fix/chatgpt-json-control-chars branch June 29, 2026 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chatgpt API error (gpt-4o-mini)

1 participant