pytest-dev/pytest · error · ValueError

{nodeid}: error raised while trying to determine id of param

Error message

{nodeid}: error raised while trying to determine id of parameter '{argname}' at position {idx}

What it means

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'.

Source

Thrown at src/_pytest/python.py:1086

                    f"{prefix}parametrize value for '{argname}' at index {idx} "
                    f"is too long for an auto-generated ID ({len(val)} characters). "
                    f"Use pytest.param(..., id=...) or parametrize(..., ids=...) "
                    f"to set an explicit ID, or change parametrize_long_str_id_strategy.",
                    pytrace=False,
                )

    def _idval_from_function(self, val: object, argname: str, idx: int) -> str | None:
        """Try to make an ID for a parameter in a ParameterSet using the
        user-provided id callable, if given."""
        if self.idfn is None:
            return None
        try:
            id = self.idfn(val)
        except Exception as e:
            prefix = f"{self.nodeid}: " if self.nodeid is not None else ""
            msg = "error raised while trying to determine id of parameter '{}' at position {}"
            msg = prefix + msg.format(argname, idx)
            raise ValueError(msg) from e
        if id is None:
            return None
        return self._idval_from_value(id)

    def _idval_from_hook(self, val: object, argname: str) -> str | None:
        """Try to make an ID for a parameter in a ParameterSet by calling the
        :hook:`pytest_make_parametrize_id` hook."""
        if self.config:
            id: str | None = self.config.hook.pytest_make_parametrize_id(
                config=self.config, val=val, argname=argname
            )
            return id
        return None

    def _idval_from_value(self, val: object) -> str | None:
        """Try to make an ID for a parameter in a ParameterSet from its value,
        if the value type is supported."""
        match val:

View on GitHub (pinned to 98b357f69e)

Solutions

  1. Make the idfn defensive: handle unexpected types and return None or a safe string.
  2. Reproduce by calling idfn(value) directly with each parametrized value to find the failing input.
  3. Use pytest.param(..., id=...) to set explicit IDs and bypass the idfn entirely for problematic values.
  4. Inspect the chained exception (__cause__) for the real error in the idfn.

Example fix

// before
def idfn(val):
    return val.name          # fails if val is None or int
@pytest.mark.parametrize("x", [obj, None, 3], ids=idfn)
def test_it(x): ...
// after
def idfn(val):
    return getattr(val, "name", None)
@pytest.mark.parametrize("x", [obj, None, 3], ids=idfn)
def test_it(x): ...
Defensive patterns

Strategy: try-catch

Validate before calling

def safe_idfn(val):
    try:
        return idfn(val)
    except Exception:
        return None

# then pass ids=safe_idfn, or pre-validate
for v in params:
    try:
        idfn(v)
    except Exception as e:
        raise AssertionError(f"idfn fails for {v!r}: {e}") from e

Try / catch

try:
    @pytest.mark.parametrize("x", params, ids=idfn)
    def test_it(x): ...
except ValueError as e:
    # idfn raised; fall back to explicit ids
    @pytest.mark.parametrize("x", params, ids=[str(i) for i in range(len(params))])
    def test_it(x): ...

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04). Data as JSON: /data/errors/45938909ef6425ae.json. Report an issue: GitHub.