pydantic/pydantic · error · PydanticUserError

model-field-missing-annotation

model-field-missing-annotation

Error message

Field {var_name!r} requires a type annotation

What it means

A `PydanticUserError` (code `model-field-missing-annotation`) raised when a `FieldInfo`/`Field(...)` is assigned to a class attribute without a type annotation. Pydantic cannot infer the field type from a bare `Field()` assignment.

Source

Thrown at pydantic/_internal/_model_construction.py:500

            )

        elif var_name.startswith('__'):
            continue
        elif is_valid_privateattr_name(var_name):
            if var_name not in raw_annotations or not is_classvar_annotation(raw_annotations[var_name]):
                private_attributes[var_name] = cast(ModelPrivateAttr, PrivateAttr(default=value))
                del namespace[var_name]
        elif var_name in base_class_vars:
            continue
        elif var_name not in raw_annotations:
            if var_name in base_class_fields:
                raise PydanticUserError(
                    f'Field {var_name!r} defined on a base class was overridden by a non-annotated attribute. '
                    f'All field definitions, including overrides, require a type annotation.',
                    code='model-field-overridden',
                )
            elif isinstance(value, FieldInfo):
                raise PydanticUserError(
                    f'Field {var_name!r} requires a type annotation', code='model-field-missing-annotation'
                )
            else:
                raise PydanticUserError(
                    f'A non-annotated attribute was detected: `{var_name} = {value!r}`. All model fields require a '
                    f'type annotation; if `{var_name}` is not meant to be a field, you may be able to resolve this '
                    f"error by annotating it as a `ClassVar` or updating `model_config['ignored_types']`.",
                    code='model-field-missing-annotation',
                )

    for ann_name, ann_type in raw_annotations.items():
        if (
            is_valid_privateattr_name(ann_name)
            and ann_name not in private_attributes
            and ann_name not in ignored_names
            # This condition can be a false negative when `ann_type` is stringified,
            # but it is handled in most cases in `set_model_fields`:
            and not is_classvar_annotation(ann_type)

View on GitHub (pinned to 2e5f0e2b42)

Solutions

  1. Add the type annotation: `name: str = Field(...)`.
  2. If using Python 3.12+ type aliases, ensure the annotation is still syntactically present.

Example fix

# before
from pydantic import BaseModel, Field
class M(BaseModel):
    name = Field(...)

# after
class M(BaseModel):
    name: str = Field(...)
Defensive patterns

Strategy: validation

Type guard

def fields_have_annotations(namespace, annotations):
    from pydantic.fields import FieldInfo
    missing = [k for k, v in namespace.items()
              if isinstance(v, FieldInfo) and k not in annotations]
    return not missing, missing

Prevention

When it happens

Trigger: Writing `class M(BaseModel): name = Field(...)` with no annotation. The Field info is present but the type is unknown.

Common situations: Forgetting the annotation after adding Field(...); copy-pasting from dataclass-style code; renaming and dropping the annotation.

Related errors


AI-assisted analysis of pydantic/pydantic@2e5f0e2b42 (2026-08-04). Data as JSON: /data/errors/23e146b44cff5a5c.json. Report an issue: GitHub.