python type_error ai_generated true

TypeError: BaseModel.__init__() takes 1 positional argument but 2 were given

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

Pydantic v1 code calling a v2 model with positional args, or using v1-only APIs like .dict() vs .model_dump().

generic

中文

Pydantic v1 代码以位置参数调用 v2 模型,或使用了仅 v1 支持的 API(如 .dict() 而 v2 应为 .model_dump())。

Workarounds

  1. 94% success
    item = Item(name='x')
    data = item.model_dump()
  2. 85% success
    from pydantic.v1 import BaseModel

Dead Ends

Common approaches that don't work:

  1. 85% fail

    FastAPI 0.100+ requires pydantic v2; other deps break.

  2. 80% fail

    Does not address .dict/.json API changes elsewhere.