python config_error ai_generated true

pydantic.errors.ConfigError: model is immutable

ID: python/pydantic-model-config-immutable

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

Attempting to modify a field of a model that has frozen=True in config, raising ConfigError.

generic

中文

尝试修改配置了 frozen=True 的模型的字段,引发 ConfigError。

Workarounds

  1. 95% success
    Remove frozen=True from model config
  2. 90% success
    Use model_copy(update={'field': new_value}) to create a new instance

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Frozen models disallow attribute setting.

  2. 40% fail

    This works but doesn't modify original; may be acceptable.