{"record":{"id":"326e1452b8b1b00b","repo":"langchain-ai/langchain","slug":"when-specifying-root-no-other-fields-should-be","errorCode":null,"errorMessage":"When specifying __root__ no other fields should be provided. Got {field_definitions}","messagePattern":"When specifying __root__ no other fields should be provided\\. Got (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/utils/pydantic.py","lineNumber":589,"sourceCode":"        model_name: The name of the model.\n        module_name: The name of the module where the model is defined.\n\n            This is used by Pydantic to resolve any forward references.\n        field_definitions: The field definitions for the model.\n        root: Type for a root model (`RootModel`)\n\n    Returns:\n        The created model.\n    \"\"\"\n    field_definitions = field_definitions or {}\n\n    if root:\n        if field_definitions:\n            msg = (\n                \"When specifying __root__ no other \"\n                f\"fields should be provided. Got {field_definitions}\"\n            )\n            raise NotImplementedError(msg)\n\n        if isinstance(root, tuple):\n            kwargs = {\"type_\": root[0], \"default_\": root[1]}\n        else:\n            kwargs = {\"type_\": root}\n\n        try:\n            named_root_model = _create_root_model_cached(\n                model_name, module_name=module_name, **kwargs\n            )\n        except TypeError:\n            # something in the arguments into _create_root_model_cached is not hashable\n            named_root_model = _create_root_model(\n                model_name,\n                module_name=module_name,\n                **kwargs,\n            )\n        return named_root_model","sourceCodeStart":571,"sourceCodeEnd":607,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/utils/pydantic.py#L571-L607","documentation":"Raised by `_create_model` in `langchain_core.utils.pydantic` when `root` is specified (pydantic v1-style `__root__` model) at the same time as additional field definitions. A root model has exactly one value, so mixing `root=` with `field_definitions` is ambiguous and rejected with `NotImplementedError`.","triggerScenarios":"Calling `create_model(\"Name\", root=str, **{\"extra\": (int, 0)})` or `create_model(\"Name\", root=(list[str], []), field1=(int, ...))` — any invocation that supplies both `root` and non-empty field definitions (including a leftover default like `field_definitions={\"x\": ...}`).","commonSituations":"Wrapping scalar/array types as models for structured output (root models for lists of objects) while also passing convenience fields; refactoring a normal model to a root model and forgetting to remove old field definitions; generic helper functions that always merge extra kwargs into field definitions.","solutions":["Remove the extra field definitions — a root model may only define `root`.","If you need both the root value and named fields, model it explicitly instead: define a `BaseModel` with a normal field (e.g. `items: list[T]`) rather than a root model.","Audit helper code that injects defaults/metadata into `field_definitions` before calling `create_model`."],"exampleFix":"# before\ncreate_model(\"Tags\", root=list[str], **{\"sep\": (str, \",\")})  # NotImplementedError\n\n# after\ncreate_model(\"Tags\", root=list[str])\n# or, if named fields are required, drop root:\nclass Tags(BaseModel):\n    items: list[str]\n    sep: str = \",\"","handlingStrategy":"validation","validationCode":"def build_model(name, root=None, **fields):\n    if root is not None and fields:\n        raise ValueError(\"a root model cannot have additional fields; pass either root= or fields, not both\")\n    return create_model(name, root=root, **fields)","typeGuard":null,"tryCatchPattern":"try:\n    create_model(\"M\", root=root, field_definitions=fields)\nexcept NotImplementedError:\n    fields = None  # degrade to a pure root model\n    model = create_model(\"M\", root=root)","preventionTips":["Keep root-model construction on its own code path, separate from multi-field models.","If named fields are needed alongside the value, use a normal BaseModel field instead of root."],"tags":["pydantic","root-model","dynamic-model"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}