iflytek/astron-agent · warning · ValueError
Translation text cannot be empty
Error message
Translation text cannot be empty
What it means
A pydantic field_validator on the TranslationInput model's `text` field raises this ValueError when the text is empty, None, or whitespace-only. Pydantic wraps it into a ValidationError during request-model construction, so the request never reaches the translation service. It is an input-schema guard ensuring the translation API has something to translate.
Solutions
- Provide a non-empty, non-whitespace `text` value in the request body.
- Trim input client-side and reject empty input before calling the API.
- Check the client's field name casing/structure matches the model so text is actually deserialized.
- If text comes from another service, add a guard there to fail earlier with a clearer message.
Example fix
// before
await translate({ text: '', source_language: 'en', target_language: 'cn' });
// after
const trimmed = sourceText.trim();
if (!trimmed) throw new Error('Please enter text to translate');
await translate({ text: trimmed, source_language: 'en', target_language: 'cn' }); Defensive patterns
Strategy: validation
Validate before calling
function canTranslate(input) {
return typeof input.text === 'string' && input.text.trim().length > 0;
} Type guard
function hasTranslationText(v: unknown): v is { text: string } {
return typeof v === 'object' && v !== null && typeof (v as any).text === 'string' && (v as any).text.trim() !== '';
} Try / catch
try:
inp = TranslationInput(**payload)
except ValidationError as e:
return JSONResponse(status_code=400, content={'error': e.errors()}) Prevention
- Disable the submit button until the text area has non-whitespace content.
- Trim text on the client before sending.
- Surface pydantic validation errors to the user instead of logging-only.
When it happens
Trigger: Constructing TranslationInput (or POSTing to the translation endpoint) with `text` = "", " ", or None; the validator `if not value or not value.strip()` rejects both.
Common situations: User submits the translation form with an empty box; upstream component passes an empty string from a failed fetch; client sends JSON without the text field so pydantic coerces/defaults it; automated tests omit text.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Translation text cannot exceed 5000 characters
- ragflow_ext is only allowed when ragType='Ragflow-RAG', got…
- Invalid group: . Valid options
- audio_data cannot be empty
- Invalid target language
AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12).
Data as JSON: /api/errors/b731e1d4ac62c968.
Report an issue: GitHub.
Appendix: source
Thrown at core/plugin/aitools/service/translation/translation_service.py:48
TRANSLATION_AUTH_ERROR = (45255, "翻译服务认证失败")
TRANSLATION_NETWORK_ERROR = (45256, "翻译服务网络连接失败")
class TranslationInput(BaseModel):
"""Translation input"""
text: str # Original text to be translated
target_language: str # Target language code
source_language: str = (
CHINESE_LANGUAGE_CODE # Source language code, default Chinese
)
@field_validator("text")
@classmethod
def validate_text(cls, value: str) -> str:
"""validate text"""
if not value or not value.strip():
raise ValueError("Translation text cannot be empty")
if len(value) > 5000:
raise ValueError("Translation text cannot exceed 5000 characters")
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:View on GitHub (pinned to 5e758547a8)