{"id":"b989d356bb8a8835","repo":"pydantic/pydantic","slug":"rootmodel-init-accepts-either-a-single-posit","errorCode":null,"errorMessage":"\"RootModel.__init__\" accepts either a single positional argument or arbitrary keyword arguments","messagePattern":"\"RootModel\\.__init__\" accepts either a single positional argument or arbitrary keyword arguments","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pydantic/root_model.py","lineNumber":66,"sourceCode":"    __pydantic_root_model__ = True\n    __pydantic_private__ = None\n    __pydantic_extra__ = None\n\n    root: RootModelRootType\n\n    def __init_subclass__(cls, **kwargs):\n        extra = cls.model_config.get('extra')\n        if extra is not None:\n            raise PydanticUserError(\n                \"`RootModel` does not support setting `model_config['extra']`\", code='root-model-extra'\n            )\n        super().__init_subclass__(**kwargs)\n\n    def __init__(self, /, root: RootModelRootType = PydanticUndefined, **data) -> None:  # type: ignore\n        __tracebackhide__ = True\n        if data:\n            if root is not PydanticUndefined:\n                raise ValueError(\n                    '\"RootModel.__init__\" accepts either a single positional argument or arbitrary keyword arguments'\n                )\n            root = data  # type: ignore\n        self.__pydantic_validator__.validate_python(root, self_instance=self)\n\n    __init__.__pydantic_base_init__ = True  # pyright: ignore[reportFunctionMemberAccess]\n\n    @classmethod\n    def model_construct(cls, root: RootModelRootType, _fields_set: set[str] | None = None) -> Self:  # type: ignore\n        \"\"\"Create a new model using the provided root object and update fields set.\n\n        Args:\n            root: The root object of the model.\n            _fields_set: The set of fields to be updated.\n\n        Returns:\n            The new model.\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/pydantic/pydantic/blob/2e5f0e2b4218de31709f1cf9c5bc61ea97a68835/pydantic/root_model.py#L48-L84","documentation":"RootModel.__init__ takes either a single positional `root` argument OR arbitrary keyword arguments (which become the root dict). Passing BOTH a positional root and keyword data is ambiguous, so ValueError is raised. When kwargs are given, root must be unset.","triggerScenarios":"MyRoot({'a': 1}, b=2); instantiating a RootModel[dict] with both a mapping positionally and keyword fields; generic factory code that always passes a dict plus kwargs.","commonSituations":"Programmatic construction that forwards both *args and **kwargs; migrating a BaseModel call site to a RootModel.","solutions":["Pass either positional root OR keyword arguments, not both.","For dict-root models, build the dict first then pass it positionally: MyRoot({'a': 1}).","Adjust generic factory code to choose one form based on the model kind."],"exampleFix":"# before\nm = MyRoot({'a': 1}, b=2)\n# after\nm = MyRoot({'a': 1, 'b': 2})\n# or\nm = MyRoot(a=1, b=2)","handlingStrategy":"validation","validationCode":"def build_root_model(cls, root=..., **kwargs):\n    if root is not ... and kwargs:\n        raise ValueError('pass either positional root or kwargs, not both')\n    return cls(root) if root is not ... else cls(**kwargs)","typeGuard":"def root_init_args_valid(root_provided: bool, has_kwargs: bool) -> bool:\n    return not (root_provided and has_kwargs)","tryCatchPattern":"try:\n    m = MyRoot(data, **extra)\nexcept ValueError as e:\n    if 'RootModel.__init__' in str(e):\n        m = MyRoot({**data, **extra})  # merge and retry","preventionTips":["Pass either positional root or keyword args to RootModel, never both.","For dict-root models, merge into one dict before constructing.","Audit generic factory code that forwards both *args and **kwargs."],"tags":["root-model","constructor","argument-mismatch"],"analyzedSha":"2e5f0e2b4218de31709f1cf9c5bc61ea97a68835","analyzedAt":"2026-08-04T19:54:21.281Z","schemaVersion":2}