sgl-project/sglang · error · AttributeError

'Req' object has no attribute 'sampling_params'

Error message

'Req' object has no attribute 'sampling_params'

What it means

Req implements __getattr__ delegating unknown attributes to self.sampling_params. When 'sampling_params' itself is missing (never set, e.g. during __init__ before assignment), the delegation guard raises this AttributeError to prevent infinite recursion.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/schedule_batch.py:257

            if name in kwargs:
                object.__setattr__(self, name, kwargs.pop(name))
            elif field.default is not MISSING:
                object.__setattr__(self, name, field.default)
            elif field.default_factory is not MISSING:
                object.__setattr__(self, name, field.default_factory())

        for name, value in kwargs.items():
            setattr(self, name, value)

        self.validate()

    def __getattr__(self, name: str) -> Any:
        """
        Delegate attribute access to sampling_params if not found in Req.
        This is only called when the attribute is not found in the instance.
        """
        if name == "sampling_params":
            raise AttributeError(
                f"'{type(self).__name__}' object has no attribute '{name}'"
            )

        sampling_params = object.__getattribute__(self, "sampling_params")
        if sampling_params is not None and hasattr(sampling_params, name):
            return getattr(sampling_params, name)

        raise AttributeError(
            f"'{type(self).__name__}' object has no attribute '{name}'"
        )

    def __setattr__(self, name: str, value: Any) -> None:
        """
        Smart attribute setting:
        1. If field exists in Req, set it in Req
        2. Else if field exists in sampling_params, set it in sampling_params
        3. Else set it in Req (for dynamic attributes)
        """

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure Req.__init__ always assigns sampling_params (even None) before any attribute access
  2. For pickled/deepcopied reqs, restore attributes in __setstate__/__deepcopy__
  3. Access fields via req.sampling_params.<field> with an explicit None check

Example fix

# before
params = req.sampling_params  # before init finished
# after
params = object.__getattribute__(req, 'sampling_params', ) if hasattr(req, 'sampling_params') else None
Defensive patterns

Strategy: try-catch

Validate before calling

has_sp = 'sampling_params' in req.__dict__
params = req.__dict__.get('sampling_params') if has_sp else None

Type guard

def has_sampling_params(req) -> bool:
    return 'sampling_params' in getattr(req, '__dict__', {})

Try / catch

try:
    v = req.sampling_params
except AttributeError:
    v = None

Prevention

When it happens

Trigger: Accessing req.sampling_params before the attribute was assigned in Req.__init__, or on a Req constructed via __new__/pickle that skipped __init__.

Common situations: Deserializing Req objects (deepcopy/pickle for DP broadcast) that drop instance attrs; accessing fields in code paths that run before init completes; bugs where attribute name 'sampling_param' is misspelled.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/21f547af54edbe3c. Report an issue: GitHub.