{"id":"45938909ef6425ae","repo":"pytest-dev/pytest","slug":"nodeid-error-raised-while-trying-to-determine-i","errorCode":null,"errorMessage":"{nodeid}: error raised while trying to determine id of parameter '{argname}' at position {idx}","messagePattern":"(.+?): error raised while trying to determine id of parameter '(.+?)' at position (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/python.py","lineNumber":1086,"sourceCode":"                    f\"{prefix}parametrize value for '{argname}' at index {idx} \"\n                    f\"is too long for an auto-generated ID ({len(val)} characters). \"\n                    f\"Use pytest.param(..., id=...) or parametrize(..., ids=...) \"\n                    f\"to set an explicit ID, or change parametrize_long_str_id_strategy.\",\n                    pytrace=False,\n                )\n\n    def _idval_from_function(self, val: object, argname: str, idx: int) -> str | None:\n        \"\"\"Try to make an ID for a parameter in a ParameterSet using the\n        user-provided id callable, if given.\"\"\"\n        if self.idfn is None:\n            return None\n        try:\n            id = self.idfn(val)\n        except Exception as e:\n            prefix = f\"{self.nodeid}: \" if self.nodeid is not None else \"\"\n            msg = \"error raised while trying to determine id of parameter '{}' at position {}\"\n            msg = prefix + msg.format(argname, idx)\n            raise ValueError(msg) from e\n        if id is None:\n            return None\n        return self._idval_from_value(id)\n\n    def _idval_from_hook(self, val: object, argname: str) -> str | None:\n        \"\"\"Try to make an ID for a parameter in a ParameterSet by calling the\n        :hook:`pytest_make_parametrize_id` hook.\"\"\"\n        if self.config:\n            id: str | None = self.config.hook.pytest_make_parametrize_id(\n                config=self.config, val=val, argname=argname\n            )\n            return id\n        return None\n\n    def _idval_from_value(self, val: object) -> str | None:\n        \"\"\"Try to make an ID for a parameter in a ParameterSet from its value,\n        if the value type is supported.\"\"\"\n        match val:","sourceCodeStart":1068,"sourceCodeEnd":1104,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/python.py#L1068-L1104","documentation":"Raised by IdMaker._idval_from_function when the user-supplied ids callable (idfn) throws an exception while generating an ID for a parametrized value. pytest wraps the call so that a buggy id function is reported with the node id, argname, and index rather than failing opaquely. The original exception is chained via 'from e'.","triggerScenarios":"Passing ids=my_func to @pytest.mark.parametrize where my_func raises (e.g. AttributeError on unexpected types, TypeError, KeyError) for one or more parameter values.","commonSituations":"An idfn that assumes all params are a certain type but receives mixed types; an idfn that indexes into a dict with a missing key; an idfn that calls str() on an object whose __str__ raises.","solutions":["Make the idfn defensive: handle unexpected types and return None or a safe string.","Reproduce by calling idfn(value) directly with each parametrized value to find the failing input.","Use pytest.param(..., id=...) to set explicit IDs and bypass the idfn entirely for problematic values.","Inspect the chained exception (__cause__) for the real error in the idfn."],"exampleFix":"// before\ndef idfn(val):\n    return val.name          # fails if val is None or int\n@pytest.mark.parametrize(\"x\", [obj, None, 3], ids=idfn)\ndef test_it(x): ...\n// after\ndef idfn(val):\n    return getattr(val, \"name\", None)\n@pytest.mark.parametrize(\"x\", [obj, None, 3], ids=idfn)\ndef test_it(x): ...","handlingStrategy":"try-catch","validationCode":"def safe_idfn(val):\n    try:\n        return idfn(val)\n    except Exception:\n        return None\n\n# then pass ids=safe_idfn, or pre-validate\nfor v in params:\n    try:\n        idfn(v)\n    except Exception as e:\n        raise AssertionError(f\"idfn fails for {v!r}: {e}\") from e","typeGuard":null,"tryCatchPattern":"try:\n    @pytest.mark.parametrize(\"x\", params, ids=idfn)\n    def test_it(x): ...\nexcept ValueError as e:\n    # idfn raised; fall back to explicit ids\n    @pytest.mark.parametrize(\"x\", params, ids=[str(i) for i in range(len(params))])\n    def test_it(x): ...","preventionTips":["Make idfn defensive (return None for unknown types).","Test idfn against every parametrized value before using it.","Use pytest.param(..., id=...) for hard-to-name values."],"tags":["parametrize","ids","user-callback"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}