TencentTranslator sends chunks larger than the API limit for single lines longer than 5000 chars
Author: Morningstar202604Created Sep 29, 2026Updated Sep 29, 2026
Description
TencentTranslator.do_translate splits long texts into chunks of at most _MAX_CHARS = 5000 characters, but only between newlines:
for line in text.splitlines(keepends=True):
if len(current) + len(line) > self._MAX_CHARS and current:
chunks.append(current)
current = line
...A single line longer than 5000 characters (e.g. a very long paragraph without line breaks) is never split: it accumulates into current and is sent as one oversized request, exceeding the Tencent API request-size limit (6000 chars).
Reproduction
from pdf2zh.translator import TencentTranslator
text = "a" * 6000 # one line, no newlines
chunks = TencentTranslator._split_chunks(text) # current main: no such helper yetOn current main, do_translate(text) calls _translate_chunk with the full 6000-char string.
Expected
Every chunk sent to the API is at most _MAX_CHARS characters; lines longer than the limit are split into fixed-size pieces while shorter lines are still grouped by line.
Source: PDFMathTranslate/PDFMathTranslate