hiyouga/LlamaFactory · error · HTTPException

Only supports u/a/u/a/u...

Error message

Only supports u/a/u/a/u...

What it means

_check_backend_available (workflow.py:98) gates the Megatron Bridge workflow on is_megatron_bridge_available(); the wheel needs matching Megatron-Core/Nemo toolchain, hence the `--no-build-isolation` hint (build deps must come from the already-installed environment) or the prebuilt NeMo Framework container. Missing package -> ImportError at workflow start.

Source

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

    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")
        elif i % 2 == 1 and message.role not in [Role.ASSISTANT, Role.FUNCTION]:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid role")

        if message.role == Role.ASSISTANT and isinstance(message.tool_calls, list) and len(message.tool_calls):
            tool_calls = [
                {"name": tool_call.function.name, "arguments": tool_call.function.arguments}
                for tool_call in message.tool_calls
            ]
            content = json.dumps(tool_calls, ensure_ascii=False)
            input_messages.append({"role": ROLE_MAPPING[Role.FUNCTION], "content": content})
        elif isinstance(message.content, list):
            text_content = ""

View on GitHub (pinned to f28afaf635)

Solutions

  1. pip install --no-build-isolation megatron-bridge (with Megatron-Core build deps already present)
  2. Or run inside the NeMo Framework container which bundles megatron-bridge
  3. If Megatron is not required, remove the megatron_bridge backend selection and use the standard path

Example fix

# before
# megatron bridge selected, wheel missing -> ImportError

# after
pip install --no-build-isolation megatron-bridge
Defensive patterns

Strategy: validation

Validate before calling

from llamafactory.extras.packages import is_megatron_bridge_available
if backend == 'megatron_bridge':
    assert is_megatron_bridge_available(), (
        'pip install --no-build-isolation megatron-bridge (or use the NeMo Framework container)'
    )

Type guard

def megatron_bridge_ready() -> bool:
    try:
        import megatron_bridge  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    run_exp()
except ImportError as e:
    if 'megatron-bridge' in str(e):
        raise SystemExit('pip install --no-build-isolation megatron-bridge or use the NeMo container') from e
    raise

Prevention

When it happens

Trigger: Selecting the megatron_bridge backend without the wheel installed; or installing it with build isolation so its Megatron build requirements are unavailable and the import probe fails.

Common situations: Trying Megatron training on a vanilla LlamaFactory install; pip silently failing to build megatron-core inside isolation; environments without the CUDA/Megatron toolchain.

Related errors


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