python config_error ai_generated true

pydantic.errors.PydanticUserError: Field name 'model_config' shadows an attribute in parent 'BaseModel'

ID: python/pydantic-model-field-name-shadowing

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —

Root Cause

A user-defined field name collides with a reserved BaseModel attribute such as model_config, model_fields, or model_dump.

generic

中文

自定义字段名与 BaseModel 的保留属性(如 model_config、model_fields、model_dump)冲突。

Workarounds

  1. 95% success
    from pydantic import BaseModel, Field
    class Settings(BaseModel):
        config: dict = Field(alias="model_config")
  2. 90% success
    class Settings(BaseModel):
        app_config: dict = Field(serialization_alias="model_config")

Dead Ends

Common approaches that don't work:

  1. 95% fail

    BaseModel is from the Pydantic library; you cannot rename its attributes without forking.

  2. 80% fail

    protected_namespaces controls the model_ prefix warning, not reserved attribute collisions.

  3. 70% fail

    The Python attribute name still shadows the parent, so the error persists at class creation.