{"record":{"id":"1f8e4a248733d564","repo":"iflytek/astron-agent","slug":"invalid-target-language-value-valid-options-list-valid","errorCode":null,"errorMessage":"Invalid target language: {value}.\nValid options: {list(VALID_LANGUAGE_CODES)}","messagePattern":"Invalid target language: (.+?)\\.\nValid options: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"core/plugin/aitools/service/translation/translation_service.py","lineNumber":58,"sourceCode":"        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:\n            raise ValueError(\n                f\"Invalid source language: {value}.\\n\"\n                f\"Valid options: {list(VALID_LANGUAGE_CODES)}\"\n            )\n        return value\n\n    @model_validator(mode=\"after\")\n    def validate_language_combination(self) -> \"TranslationInput\":","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/aitools/service/translation/translation_service.py#L40-L76","documentation":"The `validate_target_language` pydantic field validator raises this ValueError when `target_language` is not one of the keys in VALID_LANGUAGE_CODES. The upstream translation API only accepts a fixed set of language codes, so invalid codes are rejected at request-validation time. The error message itself lists all accepted codes.","triggerScenarios":"Constructing TranslationInput with target_language like \"zh\", \"chinese\", \"EN\", \"fr\" or any code absent from VALID_LANGUAGE_CODES — wrong casing, full language names, or codes for languages the API doesn't support.","commonSituations":"Client uses ISO 639-1 codes (\"zh\", \"en\") while the API expects its own scheme (\"cn\"); UI sends human-readable names; typos like \"chn\"; hardcoded defaults from another product's code set.","solutions":["Use a code from the list printed in the error message (VALID_LANGUAGE_CODES), e.g. \"cn\" for Chinese.","Map your client's codes to the API's scheme before calling (e.g. zh → cn).","Populate the language dropdown in the UI from the same VALID_LANGUAGE_CODES source to prevent drift.","Normalize casing/whitespace on the client since the membership check is exact."],"exampleFix":"// before\nawait translate({ text, target_language: 'zh' });\n// after\nconst CODE_MAP = { zh: 'cn', 'zh-cn': 'cn', en: 'en' };\nawait translate({ text, target_language: CODE_MAP[target] ?? target });","handlingStrategy":"validation","validationCode":"function isValidTargetLang(code) {\n  return VALID_LANGUAGE_CODES.includes(code);\n}","typeGuard":"function isApiLangCode(v: string): v is ApiLangCode {\n  return (VALID_LANGUAGE_CODES as readonly string[]).includes(v);\n}","tryCatchPattern":"try:\n    inp = TranslationInput(**payload)\nexcept ValidationError as e:\n    if 'Invalid target language' in str(e):\n        return error_response(f'支持的语言: {sorted(VALID_LANGUAGE_CODES)}', 400)\n    raise","preventionTips":["Populate UI language dropdowns from VALID_LANGUAGE_CODES, never hardcoded lists.","Add a mapping layer for common aliases (zh → cn) at the client boundary.","Normalize case/whitespace of language codes before sending."],"tags":["validation","translation","enum","language-codes"],"backgroundTag":"invalid-enum-value","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"}