python module_error ai_generated true

pydantic.errors.PydanticUserError: `BaseSettings` has been moved to the `pydantic-settings` package.

ID: python/fastapi-pydantic-v1-v2-incompat

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —

Root Cause

Project upgraded to Pydantic v2 (required by FastAPI 0.100+) but code still imports BaseSettings from pydantic.

generic

中文

项目升级到了 Pydantic v2(FastAPI 0.100+ 要求),但代码仍从 pydantic 导入 BaseSettings。

Workarounds

  1. 95% success
    pip install pydantic-settings
    from pydantic_settings import BaseSettings
  2. 85% success
    class Settings(BaseSettings):
        model_config = SettingsConfigDict(env_file='.env')

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Conflicts with FastAPI's dependency requirement and breaks other packages.

  2. 90% fail

    It's a raised exception, not a warning; import still fails.