python config_error ai_generated true

pydantic.errors.ConfigError: A field validator must return the value

ID: python/pydantic-field-validator-missing-return

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-02-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

A @field_validator decorated function in Pydantic v2 must return the value; if it returns None or doesn't return, Pydantic raises ConfigError because the validation chain breaks.

generic

中文

在 Pydantic v2 中,@field_validator 装饰的函数必须返回值;如果返回 None 或不返回,Pydantic 会因验证链断裂而抛出 ConfigError。

Workarounds

  1. 95% success
    Ensure validator returns the value: @field_validator('field') def validate_field(cls, v): return v
  2. 90% success
    Use mode='before' and still return the value, e.g., return v.strip()

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Pydantic expects a value to continue processing; None causes ConfigError.

  2. 60% fail

    Raising exceptions is fine, but if no exception and no return, it fails.