{"record":{"id":"87ce1a4348b52a6e","repo":"hiyouga/LlamaFactory","slug":"invalid-api-key","errorCode":null,"errorMessage":"Invalid API key.","messagePattern":"Invalid API key\\.","errorType":"http","errorClass":"HTTPException","httpStatus":401,"severity":"error","filePath":"src/llamafactory/api/app.py","lineNumber":84,"sourceCode":"    torch_gc()\n\n\ndef create_app(chat_model: \"ChatModel\") -> \"FastAPI\":\n    root_path = os.getenv(\"FASTAPI_ROOT_PATH\", \"\")\n    app = FastAPI(lifespan=partial(lifespan, chat_model=chat_model), root_path=root_path)\n    app.add_middleware(\n        CORSMiddleware,\n        allow_origins=[\"*\"],\n        allow_credentials=True,\n        allow_methods=[\"*\"],\n        allow_headers=[\"*\"],\n    )\n    api_key = os.getenv(\"API_KEY\")\n    security = HTTPBearer(auto_error=False)\n\n    async def verify_api_key(auth: Annotated[HTTPAuthorizationCredentials | None, Depends(security)]):\n        if api_key and (auth is None or auth.credentials != api_key):\n            raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=\"Invalid API key.\")\n\n    @app.get(\n        \"/v1/models\",\n        response_model=ModelList,\n        status_code=status.HTTP_200_OK,\n        dependencies=[Depends(verify_api_key)],\n    )\n    async def list_models():\n        model_card = ModelCard(id=os.getenv(\"API_MODEL_NAME\", \"gpt-3.5-turbo\"))\n        return ModelList(data=[model_card])\n\n    @app.post(\n        \"/v1/chat/completions\",\n        response_model=ChatCompletionResponse,\n        status_code=status.HTTP_200_OK,\n        dependencies=[Depends(verify_api_key)],\n    )\n    async def create_chat_completion(request: ChatCompletionRequest):","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/api/app.py#L66-L102","documentation":"Before building the Megatron-Core adapter model, the workflow resolves model_type either from a local mca_config.json (saved with an mcore checkpoint) or via AutoConfig, and validates it against MCA_SUPPORTED_MODELS (constants.py:58: deepseek_v3, glm4_moe, llama, mistral, mixtral, qwen2, qwen2_vl, qwen2_5_vl, qwen3_vl, qwen3_vl_moe, qwen3, qwen3_moe, qwen3_next, qwen3_5, qwen3_5_moe). Anything else raises ValueError with a hint to upgrade the adapter, since newer adapter releases extend the set.","triggerScenarios":"Pointing model_name_or_path at a model whose HF model_type is not in the set (e.g. gemma3, llama3 with a custom model_type, phi) while using the MCA trainer; or loading a raw mcore checkpoint whose mca_config.json lacks hf_model_type.","commonSituations":"Trying the Megatron path with a newly released model the installed adapter doesn't know; older mcore-adapter installs lacking recently added qwen3_5/qwen3_5_moe entries.","solutions":["Upgrade the adapter: pip install -U mcore-adapter (newer versions support more model types)","Switch to a supported model_type (see MCA_SUPPORTED_MODELS in src/llamafactory/extras/constants.py)","If the checkpoint is mcore-native, ensure mca_config.json contains the correct hf_model_type field"],"exampleFix":"# before\nmodel_name_or_path: google/gemma-3-4b  # model_type gemma3 not in set\n# mca path -> ValueError\n\n# after\npip install -U mcore-adapter\nmodel_name_or_path: Qwen/Qwen3-8B  # qwen3 is supported","handlingStrategy":"validation","validationCode":"from transformers import AutoConfig\nfrom llamafactory.extras.constants import MCA_SUPPORTED_MODELS\nmodel_type = AutoConfig.from_pretrained(model_path).model_type\nassert model_type in MCA_SUPPORTED_MODELS, (\n    f'{model_type} unsupported by mcore_adapter; supported: {sorted(MCA_SUPPORTED_MODELS)}'\n)","typeGuard":"def mca_supports(model_name_or_path: str, trust_remote_code: bool = False) -> bool:\n    from transformers import AutoConfig\n    from llamafactory.extras.constants import MCA_SUPPORTED_MODELS\n    return AutoConfig.from_pretrained(model_name_or_path, trust_remote_code=trust_remote_code).model_type in MCA_SUPPORTED_MODELS","tryCatchPattern":"try:\n    run_exp()\nexcept ValueError as e:\n    if 'not supported by mcore_adapter' in str(e):\n        raise SystemExit('Upgrade mcore-adapter (pip install -U mcore-adapter) or pick a supported model') from e\n    raise","preventionTips":["Check MCA_SUPPORTED_MODELS (src/llamafactory/extras/constants.py) before choosing the Megatron path","After upgrading transformers or mcore-adapter, re-run the support check — the set changes over time"],"tags":["megatron","model-support","version","config"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}