sgl-project/sglang · error · AttributeError

'{}' object has no attribute '{}'

Error message

'{}' object has no attribute '{}'

What it means

Generic fallback of Req.__getattr__: the attribute was not found on the Req instance, and either sampling_params is None or does not expose the attribute either, so delegation fails with a standard AttributeError naming the class and attribute.

Source

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

            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)
        """
        if name == "sampling_params":
            object.__setattr__(self, name, value)
            return

        if name in self.__class__.__dataclass_fields__:
            object.__setattr__(self, name, value)
            return

View on GitHub (pinned to 0132848349)

Solutions

  1. Check the Req dataclass/sampling_params class for the correct field name
  2. Guard with getattr(req, 'field', default) since __getattr__ integrates with hasattr
  3. Set sampling_params before accessing delegated fields

Example fix

# before
top = req.top_k
# after
top = getattr(req, 'top_k', 1) if req.sampling_params is not None else 1
Defensive patterns

Strategy: type-guard

Validate before calling

val = getattr(req, 'top_k', None) if req.__dict__.get('sampling_params') is not None else None

Type guard

def req_get(req, field, default=None):
    sp = req.__dict__.get('sampling_params')
    return getattr(sp, field, default) if sp is not None else default

Try / catch

try:
    v = getattr(req, field)
except AttributeError:
    v = default

Prevention

When it happens

Trigger: Accessing any req.<field> that exists neither on Req nor on its sampling_params object, e.g. req.temperature when sampling_params is None or lacks it.

Common situations: Field renamed between versions (e.g. sampling param moved into sampling_params only); accessing LLM-style request fields on a multimodal Req; None sampling_params during early lifecycle.

Related errors


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