{"id":"7bc74f0940482a8e","repo":"pydantic/pydantic","slug":"field-ann-name-r-conflicts-with-member-getattr","errorCode":null,"errorMessage":"Field {ann_name!r} conflicts with member {getattr(b, ann_name)} of protected namespace {protected_namespace!r}.","messagePattern":"Field (.+?) conflicts with member (.+?) of protected namespace (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pydantic/_internal/_fields.py","lineNumber":96,"sourceCode":"    protected_namespaces: tuple[str | Pattern[str], ...],\n    ann_name: str,\n    bases: tuple[type[Any], ...],\n    cls_name: str,\n) -> None:\n    BaseModel = import_cached_base_model()\n\n    for protected_namespace in protected_namespaces:\n        ns_violation = False\n        if isinstance(protected_namespace, Pattern):\n            ns_violation = protected_namespace.match(ann_name) is not None\n        elif isinstance(protected_namespace, str):\n            ns_violation = ann_name.startswith(protected_namespace)\n\n        if ns_violation:\n            for b in bases:\n                if hasattr(b, ann_name):\n                    if not (issubclass(b, BaseModel) and ann_name in getattr(b, '__pydantic_fields__', {})):\n                        raise ValueError(\n                            f'Field {ann_name!r} conflicts with member {getattr(b, ann_name)}'\n                            f' of protected namespace {protected_namespace!r}.'\n                        )\n            else:\n                valid_namespaces: list[str] = []\n                for pn in protected_namespaces:\n                    if isinstance(pn, Pattern):\n                        if not pn.match(ann_name):\n                            valid_namespaces.append(f're.compile({pn.pattern!r})')\n                    else:\n                        if not ann_name.startswith(pn):\n                            valid_namespaces.append(f\"'{pn}'\")\n\n                valid_namespaces_str = f'({\", \".join(valid_namespaces)}{\",)\" if len(valid_namespaces) == 1 else \")\"}'\n\n                warnings.warn(\n                    f'Field {ann_name!r} in {cls_name!r} conflicts with protected namespace {protected_namespace!r}.\\n\\n'\n                    f\"You may be able to solve this by setting the 'protected_namespaces' configuration to {valid_namespaces_str}.\",","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/pydantic/pydantic/blob/2e5f0e2b4218de31709f1cf9c5bc61ea97a68835/pydantic/_internal/_fields.py#L78-L114","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Rename the field so it does not start with 'model_validate' or 'model_dump' (or whatever protected prefix is configured).","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.","If you intentionally want to override a BaseModel method, do so by defining the method, not a field, with that name."],"exampleFix":"// before\nclass MyModel(BaseModel):\n    model_dump: str  # conflicts with BaseModel.model_dump\n\n// after\nclass MyModel(BaseModel):\n    dump_output: str","handlingStrategy":"validation","validationCode":"def check_no_protected_method_shadow(cls, protected=('model_validate', 'model_dump')) -> None:\n    from pydantic import BaseModel\n    for name in cls.__annotations__:\n        if any(name.startswith(p) for p in protected):\n            for base in cls.__bases__:\n                if hasattr(base, name) and not (issubclass(base, BaseModel) and name in getattr(base, '__pydantic_fields__', {})):\n                    raise ValueError(f'{name} shadows protected method on {base.__name__}')\n\ncheck_no_protected_method_shadow(MyModel)","typeGuard":"def is_safe_field_name(name: str, protected=('model_validate', 'model_dump')) -> bool:\n    return not any(name.startswith(p) for p in protected)","tryCatchPattern":"try:\n    class MyModel(BaseModel):\n        model_dump: str\nexcept ValueError as e:\n    if 'conflicts with member' in str(e):\n        # rename the field away from the protected namespace\n        ...","preventionTips":["Avoid field names starting with 'model_validate' or 'model_dump'.","If you need a custom protected prefix, set protected_namespaces explicitly in model_config and document it.","Run a quick grep for protected prefixes when adding fields to models that subclass BaseModel."],"tags":["pydantic","protected-namespace","fields","schema-build"],"analyzedSha":"2e5f0e2b4218de31709f1cf9c5bc61ea97a68835","analyzedAt":"2026-08-04T19:54:21.281Z","schemaVersion":2}