huggingface/transformers · error · HTTPException
Missing `model` field in the request body.
Error message
Missing `model` field in the request body.
What it means
Raised as an HTTP 422 by the /load_model endpoint of the transformers CLI serving server. The endpoint expects a JSON body such as {"model": "gpt2"} and refuses to proceed when the 'model' key is absent or null. It exists so clients fail fast with a clear message instead of triggering a KeyError deep inside model loading.
Source
Thrown at src/transformers/cli/serving/server.py:125
@app.post("/v1/completions")
async def completions(request: Request, body: dict):
return await completion_handler.handle_request(body, request.state.request_id)
@app.post("/v1/responses")
async def responses(request: Request, body: dict):
return await response_handler.handle_request(body, request.state.request_id)
@app.post("/v1/audio/transcriptions")
async def audio_transcriptions(request: Request):
return await transcription_handler.handle_request(request)
@app.post("/load_model")
async def load_model(body: dict):
from fastapi import HTTPException
model = body.get("model")
if model is None:
raise HTTPException(status_code=422, detail="Missing `model` field in the request body.")
model_id_and_revision = model_manager.process_model_name(model)
return StreamingResponse(
model_manager.load_model_streaming(model_id_and_revision), media_type="text/event-stream"
)
@app.post("/reset")
def reset():
model_manager.shutdown()
return JSONResponse({"status": "ok"})
@app.get("/v1/models")
@app.options("/v1/models")
def list_models():
return JSONResponse({"object": "list", "data": model_manager.get_gen_models()})
@app.get("/health")
def health():
if not generation_state.is_cb_alive():View on GitHub (pinned to a597f97485)
Solutions
- Send a JSON body containing the model field: curl -X POST http://host:port/load_model -H 'Content-Type: application/json' -d '{"model": "openai-community/gpt2"}'
- Check that the variable holding the model id is set before building the request body
- Verify you are hitting /load_model with POST and a JSON Content-Type, not a form or query string
Example fix
// before
curl -X POST http://localhost:8000/load_model -d '{}'
// after
curl -X POST http://localhost:8000/load_model -H 'Content-Type: application/json' -d '{"model": "openai-community/gpt2"}' Defensive patterns
Strategy: validation
Validate before calling
body = {"model": model_id}
assert isinstance(body.get("model"), str) and body["model"], "POST /load_model requires a non-empty 'model' string" Try / catch
resp = requests.post(f'{base}/load_model', json=body)
if resp.status_code == 422 and 'Missing `model` field' in resp.text:
raise ValueError('request body must include the model id') from None Prevention
- Always build the /load_model body as {'model': model_id} from a validated variable
- Log the request body (minus secrets) before sending when debugging 422s
When it happens
Trigger: POST /load_model with an empty body, a JSON body lacking the "model" key, or "model": null. Any HTTP client (curl, requests, fetch) that forgets to serialize the model id into the payload.
Common situations: Scripts that build the request body dynamically and skip the model field when a variable is unset; clients assuming the server has a default model; typos like "model_name" or "modelId" instead of "model".
Related errors
- 'input' must be a string or list
- Unsupported input item type: {item_type!r}
- Unexpected fields in the request: {unexpected}
- Expected file upload, got string
- Expected model name as string
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/cc343ed7f930c9d0.
Report an issue: GitHub.