From f823e7d31414281d48209cace746ecbb770d04e5 Mon Sep 17 00:00:00 2001 From: Oliver Hofmann Date: Fri, 8 May 2026 08:04:52 +0200 Subject: [PATCH] Return 422 when model field is missing and no force_model is set Prevents an opaque Ollama error from reaching the client by failing fast with a clear message before the request is forwarded. --- backend/main.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/main.py b/backend/main.py index 642d7ae..b95c850 100644 --- a/backend/main.py +++ b/backend/main.py @@ -92,6 +92,8 @@ async def generate(request: Request, db: Session = Depends(get_db)): force_model = crud.get_setting(db, "force_model") or None if force_model: body = {**body, "model": force_model} + if not body.get("model"): + raise HTTPException(status_code=422, detail="Field 'model' is required") prompt_tokens = crud.count_tokens(body.get("prompt", "")) if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1): @@ -119,6 +121,8 @@ async def chat(request: Request, db: Session = Depends(get_db)): force_model = crud.get_setting(db, "force_model") or None if force_model: body = {**body, "model": force_model} + if not body.get("model"): + raise HTTPException(status_code=422, detail="Field 'model' is required") messages = body.get("messages", []) prompt_tokens = sum(crud.count_tokens(_content_to_str(msg.get("content"))) for msg in messages) @@ -165,13 +169,15 @@ async def openai_chat_completions(request: Request, db: Session = Depends(get_db force_model = crud.get_setting(db, "force_model") or None if force_model: body = {**body, "model": force_model} + if not body.get("model"): + raise HTTPException(status_code=422, detail="Field 'model' is required") messages = body.get("messages", []) prompt_tokens = sum(crud.count_tokens(_content_to_str(msg.get("content"))) for msg in messages) if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1): raise HTTPException(status_code=429, detail="Quota exceeded") - model_name = body.get("model", "?") + model_name = body["model"] usage_log.info('%s | /v1/chat/completions | %s | ~%d tokens | "%s"', request.state.api_key_name, model_name, prompt_tokens, _last_user_msg(messages))