pydantic/pydantic · error · ValueError

Field {ann_name!r} conflicts with member {getattr(b, ann_nam

Error message

Field {ann_name!r} conflicts with member {getattr(b, ann_name)} of protected namespace {protected_namespace!r}.

What it means

Pydantic raises this ValueError (no PydanticUserError code; raised in _check_protected_namespaces at line 96) when a field name starts with a configured protected-namespace prefix AND a base class already defines an attribute with that exact name that is not itself a pydantic field. The default protected_namespaces are ('model_validate', 'model_dump'). This guard prevents you from silently shadowing critical BaseModel methods like model_dump or model_validate, which would break serialization and validation.

Source

Thrown at pydantic/_internal/_fields.py:96

    protected_namespaces: tuple[str | Pattern[str], ...],
    ann_name: str,
    bases: tuple[type[Any], ...],
    cls_name: str,
) -> None:
    BaseModel = import_cached_base_model()

    for protected_namespace in protected_namespaces:
        ns_violation = False
        if isinstance(protected_namespace, Pattern):
            ns_violation = protected_namespace.match(ann_name) is not None
        elif isinstance(protected_namespace, str):
            ns_violation = ann_name.startswith(protected_namespace)

        if ns_violation:
            for b in bases:
                if hasattr(b, ann_name):
                    if not (issubclass(b, BaseModel) and ann_name in getattr(b, '__pydantic_fields__', {})):
                        raise ValueError(
                            f'Field {ann_name!r} conflicts with member {getattr(b, ann_name)}'
                            f' of protected namespace {protected_namespace!r}.'
                        )
            else:
                valid_namespaces: list[str] = []
                for pn in protected_namespaces:
                    if isinstance(pn, Pattern):
                        if not pn.match(ann_name):
                            valid_namespaces.append(f're.compile({pn.pattern!r})')
                    else:
                        if not ann_name.startswith(pn):
                            valid_namespaces.append(f"'{pn}'")

                valid_namespaces_str = f'({", ".join(valid_namespaces)}{",)" if len(valid_namespaces) == 1 else ")"}'

                warnings.warn(
                    f'Field {ann_name!r} in {cls_name!r} conflicts with protected namespace {protected_namespace!r}.\n\n'
                    f"You may be able to solve this by setting the 'protected_namespaces' configuration to {valid_namespaces_str}.",

View on GitHub (pinned to 2e5f0e2b42)

Solutions

  1. Rename the field so it does not start with 'model_validate' or 'model_dump' (or whatever protected prefix is configured).
  2. If the collision is with a method you do not need, override protected_namespaces in model_config to remove the offending prefix — but understand you may shadow BaseModel methods.
  3. If you intentionally want to override a BaseModel method, do so by defining the method, not a field, with that name.

Example fix

// before
class MyModel(BaseModel):
    model_dump: str  # conflicts with BaseModel.model_dump

// after
class MyModel(BaseModel):
    dump_output: str
Defensive patterns

Strategy: validation

Validate before calling

def check_no_protected_method_shadow(cls, protected=('model_validate', 'model_dump')) -> None:
    from pydantic import BaseModel
    for name in cls.__annotations__:
        if any(name.startswith(p) for p in protected):
            for base in cls.__bases__:
                if hasattr(base, name) and not (issubclass(base, BaseModel) and name in getattr(base, '__pydantic_fields__', {})):
                    raise ValueError(f'{name} shadows protected method on {base.__name__}')

check_no_protected_method_shadow(MyModel)

Type guard

def is_safe_field_name(name: str, protected=('model_validate', 'model_dump')) -> bool:
    return not any(name.startswith(p) for p in protected)

Try / catch

try:
    class MyModel(BaseModel):
        model_dump: str
except ValueError as e:
    if 'conflicts with member' in str(e):
        # rename the field away from the protected namespace
        ...

Prevention

When it happens

Trigger: Declaring a field named exactly 'model_dump' or 'model_validate' on a BaseModel subclass (BaseModel has methods with those names). Declaring a field that starts with 'model_dump' or 'model_validate' when a parent class introduced a non-field attribute with that identical name.

Common situations: Migrating from pydantic v1 where 'dict'/'json'/'copy' were not protected. Naming a field model_dump_results or model_validate_cache thinking it is safe (that case only warns, but an exact name match errors). Inheriting from a custom base that added attributes under a protected prefix.

Related errors


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