{"record":{"id":"5077e4f7ebca12eb","repo":"pola-rs/polars","slug":"iterable-passed-to-pl-schema-contained-duplicate-n","errorCode":null,"errorMessage":"iterable passed to pl.Schema contained duplicate name '{name}'","messagePattern":"iterable passed to pl\\.Schema contained duplicate name '(.+?)'","errorType":"exception","errorClass":"DuplicateError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/schema.py","lineNumber":145,"sourceCode":"    ) -> None:\n        if _is_arrow_schema_exportable(schema) and not isinstance(schema, Schema):\n            init_polars_schema_from_arrow_c_schema(self, schema)\n            return\n\n        # `Mapping[tuple[str, SchemaInitDataType]]` is not valid at runtime, even\n        # though it is a `Iterable[tuple[str, SchemaInitDataType]]`.\n        input: Iterable[tuple[str, SchemaInitDataType] | ArrowSchemaExportable]\n        input = schema.items() if isinstance(schema, Mapping) else (schema or ())  # type: ignore[assignment]\n        for v in input:\n            name, tp = (\n                polars_schema_field_from_arrow_c_schema(v)\n                if _is_arrow_schema_exportable(v)\n                else v\n            )\n\n            if name in self:\n                msg = f\"iterable passed to pl.Schema contained duplicate name '{name}'\"\n                raise DuplicateError(msg)\n\n            if not check_dtypes:\n                super().__setitem__(name, tp)  # type: ignore[assignment]\n            elif is_polars_dtype(tp):\n                super().__setitem__(name, _check_dtype(tp))\n            else:\n                self[name] = tp\n\n    def __eq__(self, other: object) -> bool:\n        if not isinstance(other, Mapping):\n            return False\n        if len(self) != len(other):\n            return False\n        for (nm1, tp1), (nm2, tp2) in zip(self.items(), other.items(), strict=True):\n            if nm1 != nm2 or not tp1.is_(tp2):\n                return False\n        return True\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/schema.py#L127-L163","documentation":"Duplicate-name check in pl.Schema.__init__: while ingesting an iterable/Mapping of (name, dtype) pairs, a field name appears more than once, which is illegal because a schema is a name-keyed mapping; construction raises.","triggerScenarios":"pl.Schema([(\"a\", pl.Int64), (\"a\", pl.Utf8)]) — an iterable (generator/list of tuples) containing the same name twice.","commonSituations":"Programmatically zipping a names list with a dtypes list where names repeat; concatenating pairs from multiple sources.","solutions":["Deduplicate the (name, dtype) pairs before constructing the Schema","De-duplicate the source names list, keeping the desired occurrence","Use a dict literal, which enforces unique keys at write time"],"exampleFix":"// before\npl.Schema(zip(names, dtypes))  # names contains 'a' twice\n// after\npl.Schema(dict(zip(names, dtypes)))  # or dedupe names first","handlingStrategy":"validation","validationCode":"seen = set()\npairs = [(n, t) for n, t in pairs if not (n in seen or seen.add(n))]\nschema = pl.Schema(pairs)","typeGuard":null,"tryCatchPattern":"try:\n    schema = pl.Schema(pairs)\nexcept pl.exceptions.DuplicateError as e:\n    schema = pl.Schema(dict(pairs))  # last-wins dedupe","preventionTips":["Deduplicate name/dtype pairs before building a Schema","Prefer dict literals, which enforce unique keys","Validate upstream sources (joins, concatenations) for duplicate names"],"tags":["python","polars","schema","duplicate"],"backgroundTag":"schema-validation-failed","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"}