{"record":{"id":"560bb71ec50b8cda","repo":"unslothai/unsloth","slug":"cannot-start-training-over-the-api-while-an-infere","errorCode":null,"errorMessage":"Cannot start training over the API while an inference request is in progress. Wait for it to finish, or start training from the Unsloth UI.","messagePattern":"Cannot start training over the API while an inference request is in progress\\. Wait for it to finish, or start training from the Unsloth UI\\.","errorType":"http","errorClass":"HTTPException","httpStatus":409,"severity":"warning","filePath":"studio/backend/routes/training.py","lineNumber":1183,"sourceCode":"                request.start_request_id,\n                job_id,\n            )\n            if reservation == \"existing\":\n                return _start_request_response(record)\n            if reservation == \"conflict\":\n                return _start_request_response(record)\n            reserved_start_request_id = request.start_request_id\n\n        # When Unsloth is driven as an inference API (API-key auth), refuse to start training while\n        # a request is in flight: training frees VRAM by unloading the chat model, killing the\n        # stream. The UI (session auth) still starts and coexists. Mixed UI+API is not special-cased.\n        if via_api_key is True:\n            from core.inference.llama_keepwarm import other_inference_request_count\n            if (\n                other_inference_request_count(current_request_counted = False) > 0\n                or _background_video_generation_active()\n            ):\n                raise HTTPException(\n                    status_code = 409,\n                    detail = (\n                        \"Cannot start training over the API while an inference request is in \"\n                        \"progress. Wait for it to finish, or start training from the Unsloth UI.\"\n                    ),\n                )\n\n        # No in-process ensure_transformers_version(): worker.py activates it before ML imports.\n\n        # A consented latest-transformers install stage-and-swaps .venv_t5_latest mid-spawn.\n        from utils.transformers_latest import is_install_in_progress\n\n        if is_install_in_progress():\n            raise HTTPException(\n                status_code = 409,\n                detail = (\"A transformers installation is in progress. Retry when it completes.\"),\n            )\n","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/routes/training.py#L1165-L1201","documentation":"HTTP 409 raised only for API-key-authenticated training starts: Unsloth running as an inference API unloads the chat model to free VRAM for training, which would kill any in-flight inference stream. The guard checks other_inference_request_count() and background video generation before allowing the start.","triggerScenarios":"POST /training/start with API-key auth while another inference request is streaming, or while a background video clip is generating on the video backend's worker thread.","commonSituations":"Serving an OpenAI-compatible endpoint and kicking off fine-tuning without draining requests; a long chat completion still streaming when the training API call lands; video generation running in background.","solutions":["Wait for in-flight inference requests (and background video generation) to finish, then retry the training start","Start training from the Unsloth UI with session auth instead - the UI path coexists with inference and is not blocked","Drain and queue incoming inference requests before calling the API-key training start"],"exampleFix":"# before\nclient.post(\"/training/start\", payload)  # 409 while a stream is open\n\n# after\n# close/finish active chat completions first, then\nresp = client.post(\"/training/start\", payload)\nif resp.status_code == 409:\n    time.sleep(5); resp = client.post(\"/training/start\", payload)","handlingStrategy":"retry","validationCode":"# client-side: ensure no open inference streams before API-key training start\nassert not active_chat_streams(), \"drain inference requests first\"\nassert not video_generation_in_flight(), \"wait for background video clip\"","typeGuard":null,"tryCatchPattern":"resp = client.post(\"/training/start\", payload)\nif resp.status_code == 409 and \"inference request is in progress\" in resp.text:\n    wait_for_inference_drain()          # poll your own stream bookkeeping\n    resp = client.post(\"/training/start\", payload)","preventionTips":["Close all streaming chat completions and wait out background video generation before API-key training starts","Gate training starts behind an inference-idle check in your orchestrator","For UI-driven workflows use session auth, which is not subject to this guard"],"tags":["training","inference","vram","api-key","http-409","concurrency"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}