python config_error ai_generated true

pydantic.errors.ConfigError: default_factory must be a callable

ID: python/pydantic-field-default-factory-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-04-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

The default_factory argument in Field is not a callable (e.g., a list or int), causing ConfigError.

generic

中文

Field 中的 default_factory 参数不是可调用对象(例如列表或整数),导致 ConfigError。

Workarounds

  1. 95% success
    Use default_factory=list or lambda: []
  2. 85% success
    Use default=[] instead of default_factory

Dead Ends

Common approaches that don't work:

  1. 90% fail

    List is not callable, must be a function like list.

  2. 60% fail

    Lambda must be callable, but syntax errors possible.