pytest-dev/pytest · error · ValueError

error raised while trying to determine id of parameter '{arg

Error message

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

What it means

Thrown by IdMaker._idval_from_function() when the user-supplied `ids` callable (idfn) raises an exception while computing an id for a parameter value. The original exception is chained as `from e` and the argname/idx are included for context.

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 0d6fbdeffa)

Solutions

  1. Make the idfn defensive: return None for unsupported types so pytest falls back to auto-generated ids.
  2. Fix the bug in the idfn that raised.
  3. Use pytest.param(..., id='name') to set ids explicitly for problematic values.

Example fix

// before
@pytest.mark.parametrize("x", [None, "a", 1], ids=lambda v: v.lower())
// after
@pytest.mark.parametrize("x", [None, "a", 1], ids=lambda v: v.lower() if isinstance(v, str) else None)
Defensive patterns

Strategy: try-catch

Validate before calling

def safe_idfn(v):
    try:
        return idfn(v)
    except Exception:
        return None  # fall back to auto-generated id

Try / catch

try:
    @pytest.mark.parametrize('x', values, ids=my_idfn)
    def test_x(x): ...
except ValueError as e:
    if 'error raised while trying to determine id' in str(e):
        # fix idfn to be defensive, then retry
        pass
    raise

Prevention

When it happens

Trigger: Passing ids=lambda v: v.lower() to @pytest.mark.parametrize where some parameter value is None or an int; an idfn that does dict lookups on missing keys; an idfn that calls a buggy helper.

Common situations: Parametrizing with mixed types and a naive idfn; idfn that assumes all values share a shape; refactor introducing None as a param value.

Related errors


AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11). Data as JSON: /api/errors/44edb7cd7aba8bd1. Report an issue: GitHub.