{"record":{"id":"15975616b37104db","repo":"hiyouga/LlamaFactory","slug":"only-supports-u-a-u-a-u","errorCode":null,"errorMessage":"Only supports u/a/u/a/u...","messagePattern":"Only supports u/a/u/a/u\\.\\.\\.","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"src/llamafactory/api/chat.py","lineNumber":99,"sourceCode":"    Optional[list[\"AudioInput\"]],\n]:\n    if is_env_enabled(\"API_VERBOSE\", \"1\"):\n        logger.info_rank0(f\"==== request ====\\n{json.dumps(dictify(request), indent=2, ensure_ascii=False)}\")\n\n    if len(request.messages) == 0:\n        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=\"Invalid length\")\n\n    if request.messages[0].role == Role.SYSTEM:\n        content = request.messages.pop(0).content\n        if isinstance(content, list):\n            system = content[0].text if content else \"\"\n        else:\n            system = content\n    else:\n        system = None\n\n    if len(request.messages) % 2 == 0:\n        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=\"Only supports u/a/u/a/u...\")\n\n    input_messages = []\n    images, videos, audios = [], [], []\n    for i, message in enumerate(request.messages):\n        if i % 2 == 0 and message.role not in [Role.USER, Role.TOOL]:\n            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=\"Invalid role\")\n        elif i % 2 == 1 and message.role not in [Role.ASSISTANT, Role.FUNCTION]:\n            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=\"Invalid role\")\n\n        if message.role == Role.ASSISTANT and isinstance(message.tool_calls, list) and len(message.tool_calls):\n            tool_calls = [\n                {\"name\": tool_call.function.name, \"arguments\": tool_call.function.arguments}\n                for tool_call in message.tool_calls\n            ]\n            content = json.dumps(tool_calls, ensure_ascii=False)\n            input_messages.append({\"role\": ROLE_MAPPING[Role.FUNCTION], \"content\": content})\n        elif isinstance(message.content, list):\n            text_content = \"\"","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/api/chat.py#L81-L117","documentation":"_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.","triggerScenarios":"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.","commonSituations":"Trying Megatron training on a vanilla LlamaFactory install; pip silently failing to build megatron-core inside isolation; environments without the CUDA/Megatron toolchain.","solutions":["pip install --no-build-isolation megatron-bridge (with Megatron-Core build deps already present)","Or run inside the NeMo Framework container which bundles megatron-bridge","If Megatron is not required, remove the megatron_bridge backend selection and use the standard path"],"exampleFix":"# before\n# megatron bridge selected, wheel missing -> ImportError\n\n# after\npip install --no-build-isolation megatron-bridge","handlingStrategy":"validation","validationCode":"from llamafactory.extras.packages import is_megatron_bridge_available\nif backend == 'megatron_bridge':\n    assert is_megatron_bridge_available(), (\n        'pip install --no-build-isolation megatron-bridge (or use the NeMo Framework container)'\n    )","typeGuard":"def megatron_bridge_ready() -> bool:\n    try:\n        import megatron_bridge  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    run_exp()\nexcept ImportError as e:\n    if 'megatron-bridge' in str(e):\n        raise SystemExit('pip install --no-build-isolation megatron-bridge or use the NeMo container') from e\n    raise","preventionTips":["Install Megatron-family deps in dedicated envs/containers; don't mix with light training envs","Always use --no-build-isolation for megatron-bridge so it links against the installed Megatron-Core"],"tags":["dependency","megatron-bridge","installation","nemo"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}