{"record":{"id":"44d5212750b01dd9","repo":"pola-rs/polars","slug":"mapping-item-must-be-a-datatype-or-datatype-expres","errorCode":null,"errorMessage":"mapping item must be a datatype or datatype expression; found {qualified_type_name(dtype_expr)!r}","messagePattern":"mapping item must be a datatype or datatype expression; found (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/datatype.py","lineNumber":115,"sourceCode":"    \"\"\"\n    Create a new datatype expression that represents a Struct datatype.\n\n    .. warning::\n        This functionality is considered **unstable**. It may be changed\n        at any point without it being considered a breaking change.\n    \"\"\"\n    from polars._plr import PyDataTypeExpr\n\n    def preprocess(dtype_expr: PolarsDataType | pl.DataTypeExpr) -> PyDataTypeExpr:\n        if isinstance(dtype_expr, pl.DataType):\n            return dtype_expr.to_dtype_expr()._pydatatype_expr\n        if isinstance(dtype_expr, pl.DataTypeClass):\n            return dtype_expr.to_dtype_expr()._pydatatype_expr\n        elif isinstance(dtype_expr, pl.DataTypeExpr):\n            return dtype_expr._pydatatype_expr\n        else:\n            msg = f\"mapping item must be a datatype or datatype expression; found {qualified_type_name(dtype_expr)!r}\"\n            raise TypeError(msg)\n\n    fields = [(name, preprocess(dtype_expr)) for (name, dtype_expr) in mapping.items()]\n\n    return pl.DataTypeExpr._from_pydatatype_expr(\n        PyDataTypeExpr.struct_with_fields(fields)\n    )\n","sourceCodeStart":97,"sourceCodeEnd":122,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/datatype.py#L97-L122","documentation":"DataTypeExpr.struct_with_fields (pl.DataTypeExpr.struct_with_fields / mapping-based struct construction) requires each mapping value to be a polars DataTypeClass or DataTypeExpr. Any other value raises TypeError naming the qualified type found.","triggerScenarios":"Building a struct DataTypeExpr from a mapping whose values include plain strings like 'Int64', Python builtins (int, float), or None instead of polars dtypes.","commonSituations":"Confusing dtype-name strings with dtype classes; copying schemas defined as {name: 'Int64'} string dictionaries from other frameworks.","solutions":["Use polars dtype classes, e.g. pl.Int64 instead of 'Int64'","Use DataTypeExpr objects (e.g. from pl.dtype_expr helpers) when dynamic","Validate mapping values with `isinstance(v, pl.DataTypeClass) or isinstance(v, pl.DataTypeExpr)`"],"exampleFix":"// before\npl.DataTypeExpr.struct_with_fields({'a': 'Int64'})\n// after\npl.DataTypeExpr.struct_with_fields({'a': pl.Int64})","handlingStrategy":"type-guard","validationCode":"for k, v in mapping.items():\n    if not (isinstance(v, pl.DataTypeClass) or isinstance(v, pl.DataTypeExpr)):\n        raise TypeError(f'{k}: {v!r} is not a polars dtype or DataTypeExpr')","typeGuard":"def is_dtype_or_expr(v) -> TypeGuard[pl.DataTypeClass | pl.DataTypeExpr]:\n    return isinstance(v, (pl.DataTypeClass, pl.DataTypeExpr))","tryCatchPattern":"try:\n    dt = pl.DataTypeExpr.struct_with_fields(mapping)\nexcept TypeError:\n    fixed = {k: getattr(pl, str(v)) for k, v in mapping.items()}\n    dt = pl.DataTypeExpr.struct_with_fields(fixed)","preventionTips":["Never use dtype-name strings; always reference pl.Int64-style classes","Define schemas in one typed module to avoid stringly-typed mappings"],"tags":["polars","type-error","datatype","python"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}