{"record":{"id":"8d9869f0a95a58d5","repo":"iflytek/astron-agent","slug":"translation-text-cannot-exceed-5000-characters","errorCode":null,"errorMessage":"Translation text cannot exceed 5000 characters","messagePattern":"Translation text cannot exceed 5000 characters","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"core/plugin/aitools/service/translation/translation_service.py","lineNumber":50,"sourceCode":"\n\nclass TranslationInput(BaseModel):\n    \"\"\"Translation input\"\"\"\n\n    text: str  # Original text to be translated\n    target_language: str  # Target language code\n    source_language: str = (\n        CHINESE_LANGUAGE_CODE  # Source language code, default Chinese\n    )\n\n    @field_validator(\"text\")\n    @classmethod\n    def validate_text(cls, value: str) -> str:\n        \"\"\"validate text\"\"\"\n        if not value or not value.strip():\n            raise ValueError(\"Translation text cannot be empty\")\n        if len(value) > 5000:\n            raise ValueError(\"Translation text cannot exceed 5000 characters\")\n        return value\n\n    @field_validator(\"target_language\")\n    @classmethod\n    def validate_target_language(cls, value: str) -> str:\n        \"\"\"validate target language\"\"\"\n        if value not in VALID_LANGUAGE_CODES:\n            raise ValueError(\n                f\"Invalid target language: {value}.\\n\"\n                f\"Valid options: {list(VALID_LANGUAGE_CODES)}\"\n            )\n        return value\n\n    @field_validator(\"source_language\")\n    @classmethod\n    def validate_source_language(cls, value: str) -> str:\n        \"\"\"validate source language\"\"\"\n        if value not in VALID_LANGUAGE_CODES:","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/aitools/service/translation/translation_service.py#L32-L68","documentation":"The same `validate_text` pydantic validator caps translation input at 5000 characters and raises this ValueError when `len(value) > 5000`. Pydantic turns it into a ValidationError before the request reaches the upstream translation API, which itself has a length limit. It protects the service from oversized payloads the backend would reject anyway.","triggerScenarios":"Posting a TranslationInput whose `text` field is longer than 5000 characters (after no trimming — the length check counts the raw value, including leading/trailing whitespace).","commonSituations":"User pastes a long article/document into a translator UI; an upstream node passes a whole LLM response or file dump as text; concatenated paragraph assembly exceeds the limit unnoticed.","solutions":["Split the input into chunks of at most 5000 characters and translate them sequentially or in parallel, then join results.","Trim and optionally truncate input client-side before calling the API, warning the user about truncation.","For long documents, use a document-translation path or summarize first instead of sending raw text.","Count characters (including whitespace) exactly as Python's len() does to predict the limit."],"exampleFix":"# before\nresult = translate(text=huge_document)  # 12000 chars\n# after\nCHUNK = 5000\nchunks = [huge_document[i:i+CHUNK] for i in range(0, len(huge_document), CHUNK)]\nresult = ''.join(translate(text=c) for c in chunks)","handlingStrategy":"validation","validationCode":"const MAX = 5000;\nif (text.length > MAX) {\n  // chunk or reject before calling\n  chunks = splitIntoChunks(text, MAX);\n}","typeGuard":null,"tryCatchPattern":"try:\n    inp = TranslationInput(text=text, source_language=s, target_language=t)\nexcept ValidationError as e:\n    if '5000' in str(e):\n        text = text[:5000]\n        inp = TranslationInput(text=text, source_language=s, target_language=t)\n    else:\n        raise","preventionTips":["Show a live character counter (limit 5000) in the translation UI.","Chunk long documents client-side into <=5000-char pieces.","Remember the limit counts all characters including whitespace."],"tags":["validation","pydantic","translation","payload-size"],"backgroundTag":"value-out-of-range","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}