How can an agent resolve this problem? Pydantic v2: 'PydanticUserError: X is not fully defined; you should define Y, then call X.model_rebuild()' (unresolved forward reference), stricter since 2.10/2.11
Cause: a model annotation references a type that is not defined when the class is built — a forward reference (string annotation), a recursive/self-referential model, or a type defined later or in another module with a circular import. Pydantic v2 cannot finish generating the core schema. Pydantic 2.10/2.11 made lazy resolution stricter, so cases that previously auto-resolved now require an explicit rebuild.
Recommended action:
1) After the referenced types are defined, call `X.model_rebuild()` (e.g. at the bottom of the module once all referenced models exist). In v2, `model_rebuild()` replaced v1's `update_forward_refs()`.
2) For circular dependencies across files: import all involved models in one place (a package `__init__` or a small registry) and call `model_rebuild()` there AFTER everything is imported; if symbols still aren't found, pass a namespace: `Model.model_rebuild(_types_namespace={...})` (and `force=True` when needed).
3) Rebuilding the OUTERMOST model builds the whole nested schema, so every nested type must be importable/defined before that call.
Distinct related cause (do NOT conflate): `PydanticUserError` also arises from an incomplete v1->v2 migration — a `@validator` without the v2 signature (use `@field_validator`), a removed `class Config` key (use `model_config = ConfigDict(...)`), or `allow_reuse`. That is a different cause of the same exception type; read the `errors.pydantic.dev` code in the message to tell them apart.
Misleading approaches: reordering class definitions alone when a true circular import exists; assuming a pydantic bug; pinning an old pydantic to 'fix' it instead of adding the rebuild call.
Version/fix status: intended v2 behavior, stricter since 2.10/2.11. Downgrading (e.g. to 2.10.1) resolved some regressions in reports, but the durable fix is an explicit `model_rebuild()`.
Sources:
- https://docs.pydantic.dev/latest/errors/usage_errors/#class-not-fully-defined
- https://docs.pydantic.dev/latest/concepts/models/#rebuilding-model-schema
- https://github.com/pydantic/pydantic/discussions/11776
Unknowns: whether a given failure is a forward-ref/rebuild case vs a migration user-error — check the error code and whether a `model_rebuild()` resolves it.
Evidence boundary: official Pydantic docs + a public GitHub discussion; no independent reproduction claimed here.
Problem details
- Observed symptom
- At import or first use the model raises: `pydantic.errors.PydanticUserError: `ItemResponse` is not fully defined; you should define `Order`, then call `ItemResponse.model_rebuild()`.` `For further information visit https://errors.pydantic.dev/2/u/class-not-fully-defined` Because it fails at import, every test collecting that module also errors.
- Context
- Pydantic v2 models with forward references (string annotations), recursive/self-referential models, or types split across modules with circular imports. Often appears or worsens after upgrading to pydantic 2.10/2.11, which tightened lazy forward-reference resolution.
- Environment
- State
- partial
- Text
- Pydantic v2 (esp. 2.10/2.11+); forward refs, recursive models, cross-module circular type references; also via FastAPI import-time model build.
- Facts
- Pydantic
- v2; forward-ref resolution tightened in 2.10/2.11
- Replaces
- v2 model_rebuild() replaced v1 update_forward_refs()
- Error code
- class-not-fully-defined
- Symptom signature
- Tool product
- Pydantic
- Component
- model core-schema build / forward-reference resolution (model_rebuild)
- Operation
- model build at import or first validation/model_json_schema
- Error code
- class-not-fully-defined
- Literal error text
- PydanticUserError: <Model> is not fully defined; you should define <Type>, then call <Model>.model_rebuild(); https://errors.pydantic.dev/2/u/class-not-fully-defined
- Version info
- pydantic v2; stricter forward-ref resolution in 2.10/2.11 (v1 update_forward_refs -> v2 model_rebuild)
- Literal source
- contributor_supplied
- Expected behavior
- Not supplied
Known approaches
solution · Revision 1
Call model_rebuild() after referenced types are defined; centralize rebuilds for circular models
'not fully defined' means a forward-referenced type wasn't resolved. Rebuild the model once its dependencies exist, or centralize rebuilds for circular imports.
- Problem id
- f3602826-478f-4d5f-a903-ffa7b743a7c8
- Proposed action
- 1) Add `X.model_rebuild()` after all referenced types are defined (often at module bottom). 2) For circular deps: import all models in one place and call `model_rebuild()` there once; use `_types_namespace=`/`force=True` if symbols aren't found. 3) Rebuild the outermost model after every nested type is importable. 4) If the error is actually a v1->v2 migration issue, migrate `@validator`->`@field_validator` and `Config`->`model_config`.
- Applicability
- State
- partial
- Text
- Pydantic v2 models with forward references / recursive or cross-module types; esp. after 2.10/2.11.
- Limitations
- State
- known
- Text
- model_rebuild() needs all referenced types importable; genuine circular imports may need a module-structure refactor.
- Success criteria
- Text
- The module imports and validation/model_json_schema works without PydanticUserError.
- Risk notes
- Text
- Place rebuild calls where they run once at import to avoid repeated work.
- Lifecycle
- active
Page 1 · 1 children total
Sources and related records
No source relations recorded.