{"record":{"id":"92f0f8a04d48101d","repo":"iflytek/astron-agent","slug":"invalid-source-language-value-valid-options-list-valid","errorCode":null,"errorMessage":"Invalid source language: {value}.\nValid options: {list(VALID_LANGUAGE_CODES)}","messagePattern":"Invalid source language: (.+?)\\.\nValid options: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"core/plugin/aitools/service/translation/translation_service.py","lineNumber":69,"sourceCode":"        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\":\n        \"\"\"Validate that at least one language is Chinese (cn)\"\"\"\n        if not is_valid_language_pair(self.source_language, self.target_language):\n            raise ValueError(\n                \"API requires Chinese (cn) as either source or target language. \"\n                f\"Current combination: {self.source_language} → {self.target_language} \"\n                \"is not supported.\"\n            )\n        return self\n\n\n@api_service(","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/plugin/aitools/service/translation/translation_service.py#L51-L87","documentation":"The `validate_source_language` pydantic field validator raises this ValueError when `source_language` is not in VALID_LANGUAGE_CODES. Like the target-language check, it enforces the upstream API's supported language set at model-construction time, and the message enumerates the valid options.","triggerScenarios":"Constructing TranslationInput with source_language such as \"zh\", \"Chinese\", \"JP\", or any value missing from VALID_LANGUAGE_CODES — casing mismatch, full names, or unsupported languages.","commonSituations":"Client-side language detection returns codes in a different scheme; user picks a language the API doesn't support; default values copied from another i18n config; typos in hardcoded strings.","solutions":["Use one of the codes listed in the error message (from VALID_LANGUAGE_CODES).","Normalize detected-language output (e.g. from a detection library) into the API's code scheme before sending.","Validate source_language alongside target_language in one client-side check to catch both errors at once.","If auto-detect is desired, check whether the API supports an explicit auto/detect code in VALID_LANGUAGE_CODES rather than guessing."],"exampleFix":"# before\nTranslationInput(text=t, source_language='zh', target_language='en')\n# after\nsrc = 'zh' if raw_src.startswith('zh') else raw_src\nsrc = 'cn' if src == 'zh' else src  # map to API scheme\nTranslationInput(text=t, source_language=src, target_language='en')","handlingStrategy":"validation","validationCode":"function isValidSourceLang(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 source language' in str(e):\n        return error_response(f'支持的语言: {sorted(VALID_LANGUAGE_CODES)}', 400)\n    raise","preventionTips":["Convert language-detection results into the API's code scheme before sending.","Share a single canonical language-code list between frontend and backend.","Reject unsupported source languages at the UI level."],"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"}