iflytek/astron-agent · warning · ValueError

Invalid source language

Error message

Invalid source language: {value}.
Valid options: {list(VALID_LANGUAGE_CODES)}

What it means

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.

Solutions

  1. Use one of the codes listed in the error message (from VALID_LANGUAGE_CODES).
  2. Normalize detected-language output (e.g. from a detection library) into the API's code scheme before sending.
  3. Validate source_language alongside target_language in one client-side check to catch both errors at once.
  4. If auto-detect is desired, check whether the API supports an explicit auto/detect code in VALID_LANGUAGE_CODES rather than guessing.

Example fix

# before
TranslationInput(text=t, source_language='zh', target_language='en')
# after
src = 'zh' if raw_src.startswith('zh') else raw_src
src = 'cn' if src == 'zh' else src  # map to API scheme
TranslationInput(text=t, source_language=src, target_language='en')
Defensive patterns

Strategy: validation

Validate before calling

function isValidSourceLang(code) {
  return VALID_LANGUAGE_CODES.includes(code);
}

Type guard

function isApiLangCode(v: string): v is ApiLangCode {
  return (VALID_LANGUAGE_CODES as readonly string[]).includes(v);
}

Try / catch

try:
    inp = TranslationInput(**payload)
except ValidationError as e:
    if 'Invalid source language' in str(e):
        return error_response(f'支持的语言: {sorted(VALID_LANGUAGE_CODES)}', 400)
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/92f0f8a04d48101d. Report an issue: GitHub.

Appendix: source

Thrown at core/plugin/aitools/service/translation/translation_service.py:69

        return value

    @field_validator("target_language")
    @classmethod
    def validate_target_language(cls, value: str) -> str:
        """validate target language"""
        if value not in VALID_LANGUAGE_CODES:
            raise ValueError(
                f"Invalid target language: {value}.\n"
                f"Valid options: {list(VALID_LANGUAGE_CODES)}"
            )
        return value

    @field_validator("source_language")
    @classmethod
    def validate_source_language(cls, value: str) -> str:
        """validate source language"""
        if value not in VALID_LANGUAGE_CODES:
            raise ValueError(
                f"Invalid source language: {value}.\n"
                f"Valid options: {list(VALID_LANGUAGE_CODES)}"
            )
        return value

    @model_validator(mode="after")
    def validate_language_combination(self) -> "TranslationInput":
        """Validate that at least one language is Chinese (cn)"""
        if not is_valid_language_pair(self.source_language, self.target_language):
            raise ValueError(
                "API requires Chinese (cn) as either source or target language. "
                f"Current combination: {self.source_language} → {self.target_language} "
                "is not supported."
            )
        return self


@api_service(

View on GitHub (pinned to 5e758547a8)