sgl-project/sglang · error · ValueError

KernelSpec.target must be 'module:attr', got {self.target!r}

Error message

KernelSpec.target must be 'module:attr', got {self.target!r}

What it means

KernelSpec.load() parses the spec's target string as 'module.path:attr.path' using partition(':'). If the target has no colon or nothing after it, the format is invalid and this ValueError is raised before any import is attempted. This is an authoring bug in the KernelSpec, not a runtime environment issue.

Source

Thrown at python/sglang/kernels/spec.py:258

    @property
    def name(self) -> str:
        return self.op.split(".", 1)[1] if "." in self.op else self.op

    def is_available(self, platform: PlatformInfo) -> bool:
        """Whether this backend can run on ``platform`` (metadata-only check)."""
        return capabilities_satisfied(self.capabilities, platform)

    def load(self) -> Callable:
        """Import and return the backing callable.

        Raises the underlying ``ImportError`` / ``AttributeError`` if the
        backend is not installed on this platform — call sites decide how to
        handle that.
        """
        module_path, sep, attr = self.target.partition(":")
        if not sep or not attr:
            raise ValueError(
                f"KernelSpec.target must be 'module:attr', got {self.target!r}"
            )
        obj = importlib.import_module(module_path)
        for part in attr.split("."):
            obj = getattr(obj, part)
        return obj

View on GitHub (pinned to 0132848349)

Solutions

  1. Fix the target string to the exact 'module:attr' form, e.g. 'sglang.srt.layers.moe.fused_moe_triton:fused_experts'.
  2. For nested attributes use dotted attr path: 'pkg.mod:obj.method'.
  3. Add a unit test asserting each registered spec parses (call load() or partition check) at registration time.

Example fix

# before
KernelSpec(target="sglang.srt.layers.mykernel")

# after
KernelSpec(target="sglang.srt.layers.mykernel:my_kernel_fn")
Defensive patterns

Strategy: validation

Validate before calling

def valid_target(t: str) -> bool:
    mod, sep, attr = t.partition(":")
    return bool(sep and mod and attr)

assert valid_target(spec.target), f"bad target {spec.target!r}"

Type guard

def is_valid_target(target: object) -> bool:
    if not isinstance(target, str):
        return False
    mod, sep, attr = target.partition(":")
    return bool(sep and mod and attr and all(part.isidentifier() for part in attr.split(".")))

Try / catch

try:
    fn = spec.load()
except ValueError as e:
    raise RuntimeError(f"Misconfigured KernelSpec {spec!r}: fix target format") from e

Prevention

When it happens

Trigger: Registering a KernelSpec with target='mymodule.my_kernel' (missing ':attr') or target='mymodule:' (empty attr); triggered when _resolve() -> load() runs for that op.

Common situations: Hand-writing kernel registrations or porting specs from another format; copy-paste errors that drop the colon; typos in the attr half.

Related errors


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