sgl-project/sglang · error · AttributeError

No encoder method found for modality '{modality_name}'

Error message

No encoder method found for modality '{modality_name}'

What it means

For a multimodal input, the backend probes a list of candidate encoder method names on the HF model (e.g. get_image_features) and fails with AttributeError if none exists.

Source

Thrown at python/sglang/srt/models/transformers.py:1379

                | self.weight_mapper
            )

    def _uses_mrope_positions(self) -> bool:
        rope_scaling = getattr(self.text_config, "rope_scaling", None)
        if isinstance(rope_scaling, Mapping) and "mrope_section" in rope_scaling:
            return True
        rope_type = str(getattr(self.text_config, "rope_type", "")).lower()
        return "mrope" in rope_type

    def pad_input_ids(self, input_ids: list[int], mm_inputs: MultimodalInputs):
        return input_ids

    def _get_modality_encoder(self, modality_name: str):
        for name in self._mm_encoder_candidates[modality_name]:
            fn = getattr(self.model, name, None)
            if fn is not None:
                return fn
        raise AttributeError(f"No encoder method found for modality '{modality_name}'")

    def _get_modality_dtype_device(
        self, modality_name: str
    ) -> tuple[Optional[torch.dtype], Optional[torch.device]]:
        module_candidates = {
            "image": ("vision_tower", "vision_model"),
            "video": ("video_tower", "vision_tower", "vision_model"),
            "audio": ("audio_tower", "audio_model", "audio_encoder"),
        }
        modules = []
        for name in module_candidates.get(modality_name, ()):
            module = getattr(self.model, name, None)
            if module is not None:
                modules.append(module)
        modules.append(self.model)

        for module in modules:
            for param in module.parameters():

View on GitHub (pinned to 0132848349)

Solutions

  1. Check self._mm_encoder_candidates[modality] vs dir(model) and extend the candidate list with the model's actual method name
  2. Upgrade sglang/transformers so the candidate names match the model
  3. Disable multimodal processing if the model is text-only

Example fix

# before
candidates = {"image": ("get_image_features", "encode_image")}
# after
candidates = {"image": ("get_image_features", "encode_image", "get_image_embeddings")}
Defensive patterns

Strategy: fallback

Validate before calling

cands = model._mm_encoder_candidates['image']
assert any(hasattr(model.model, n) for n in cands), f'no encoder among {cands}'

Type guard

def has_encoder(m, modality='image') -> bool:
    return any(hasattr(m, n) for n in ('get_image_features','encode_image'))

Try / catch

try:
    fn = model._get_modality_encoder('image')
except AttributeError:
    fn = find_encoder_by_convention(model.model)

Prevention

When it happens

Trigger: Sending an image (or other modality) to a model whose class exposes none of the candidate encoder methods for that modality — e.g. a text-only model routed through the Transformers multimodal path, or a new encoder naming convention.

Common situations: Newer HF multimodal models renaming their feature methods; multi-image/video models with unlisted method names; accidentally enabling mm processing on a text model.

Related errors


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