hiyouga/LlamaFactory · error · HTTPException

Invalid length

Error message

Invalid length

What it means

The Megatron Bridge PT/SFT path (gpt_step) only supports text LLMs listed in MEGATRON_BRIDGE_SUPPORTED_MODELS (constants.py:78: deepseek_v3, deepseek_v4, llama, mistral, qwen2, qwen3, qwen3_5, qwen3_5_moe, qwen3_5_moe_text, qwen3_5_text, qwen3_moe, qwen3_next). _check_model_support loads the HF config and raises ValueError for any other model_type, explicitly noting multimodal/audio/omni models are not enabled in v0.

Source

Thrown at src/llamafactory/api/chat.py:87

    Role.TOOL: DataRole.OBSERVATION.value,
}


def _process_request(
    request: "ChatCompletionRequest",
) -> tuple[
    list[dict[str, str]],
    Optional[str],
    Optional[str],
    Optional[list["ImageInput"]],
    Optional[list["VideoInput"]],
    Optional[list["AudioInput"]],
]:
    if is_env_enabled("API_VERBOSE", "1"):
        logger.info_rank0(f"==== request ====\n{json.dumps(dictify(request), indent=2, ensure_ascii=False)}")

    if len(request.messages) == 0:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid length")

    if request.messages[0].role == Role.SYSTEM:
        content = request.messages.pop(0).content
        if isinstance(content, list):
            system = content[0].text if content else ""
        else:
            system = content
    else:
        system = None

    if len(request.messages) % 2 == 0:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Only supports u/a/u/a/u...")

    input_messages = []
    images, videos, audios = [], [], []
    for i, message in enumerate(request.messages):
        if i % 2 == 0 and message.role not in [Role.USER, Role.TOOL]:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid role")

View on GitHub (pinned to f28afaf635)

Solutions

  1. Use a supported text model type (see MEGATRON_BRIDGE_SUPPORTED_MODELS in src/llamafactory/extras/constants.py)
  2. For multimodal models, switch off the Megatron Bridge backend to the standard HF trainer path or the MCA path which supports qwen*_vl
  3. For a text backbone inside a multimodal release, point at the text-only checkpoint variant (e.g. qwen3_5_text)

Example fix

# before
model_name_or_path: Qwen/Qwen2.5-VL-7B  # model_type qwen2_5_vl
# megatron bridge path -> ValueError

# after
model_name_or_path: Qwen/Qwen2.5-7B  # qwen2 supported (or drop megatron bridge)
Defensive patterns

Strategy: validation

Validate before calling

from transformers import AutoConfig
from llamafactory.extras.constants import MEGATRON_BRIDGE_SUPPORTED_MODELS
model_type = AutoConfig.from_pretrained(model_path).model_type
assert model_type in MEGATRON_BRIDGE_SUPPORTED_MODELS, (
    f'{model_type} unsupported by Megatron Bridge; supported: {sorted(MEGATRON_BRIDGE_SUPPORTED_MODELS)}'
)

Type guard

def megatron_bridge_supports(model_name_or_path: str, trust_remote_code: bool = False) -> bool:
    from transformers import AutoConfig
    from llamafactory.extras.constants import MEGATRON_BRIDGE_SUPPORTED_MODELS
    return AutoConfig.from_pretrained(model_name_or_path, trust_remote_code=trust_remote_code).model_type in MEGATRON_BRIDGE_SUPPORTED_MODELS

Try / catch

try:
    run_exp()
except ValueError as e:
    if 'Megatron Bridge' in str(e):
        raise SystemExit('Use a supported text model type or switch off the megatron_bridge backend') from e
    raise

Prevention

When it happens

Trigger: Running the megatron_bridge workflow with model_name_or_path whose AutoConfig.model_type is outside the set — e.g. qwen2_5_vl, qwen3_omni_moe, gemma3, internvl — during _check_model_support before dataset export/training.

Common situations: Assuming the Megatron path handles VL/omni models because the MCA path does; using a text model with an exotic model_type; model_type naming changes after a transformers upgrade.

Related errors


AI-assisted analysis of hiyouga/LlamaFactory@f28afaf635 (2026-08-14). Data as JSON: /api/errors/05fa142792f124d6. Report an issue: GitHub.