diff --git a/backend/beyond_the_loop/routers/openai.py b/backend/beyond_the_loop/routers/openai.py index 70ab29a52..91127b7ae 100644 --- a/backend/beyond_the_loop/routers/openai.py +++ b/backend/beyond_the_loop/routers/openai.py @@ -562,18 +562,18 @@ async def generate_chat_completion( model_info = Models.get_model_by_id(form_data.get("model")) + if model_info is None: + raise HTTPException( + status_code=404, + detail="Model not found. Please check the model ID is correct.", + ) + has_chat_id = "chat_id" in metadata and metadata["chat_id"] is not None # Initialize the credit cost variable model_message_credit_cost = 0 if has_chat_id: - if model_info is None: - raise HTTPException( - status_code=404, - detail="Model not found. Please check the model ID is correct.", - ) - # Check for base_model_id first in case of user defined custom model model_id = model_info.base_model_id if model_info.base_model_id else model_info.id model_message_credit_cost = ModelMessageCreditCosts.get_cost_by_model(model_id) @@ -616,6 +616,9 @@ async def generate_chat_completion( # Check model info and override the payload if model_info: + # Initialize model_id here to ensure it's always defined + model_id = model_info.id + if model_info.base_model_id: payload["model"] = model_info.base_model_id model_id = model_info.base_model_id