From 405bdb2808514906a3aa1cffce089db001fe6e96 Mon Sep 17 00:00:00 2001 From: Bentlybro Date: Tue, 7 Apr 2026 17:59:43 +0100 Subject: [PATCH] fix(backend/llm-registry): enforce single recommended model in update_model When setting is_recommended=True on a model, first clears the flag on all other models within the same transaction so only one model can be recommended at a time. --- .../backend/backend/server/v2/llm/db_write.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/autogpt_platform/backend/backend/server/v2/llm/db_write.py b/autogpt_platform/backend/backend/server/v2/llm/db_write.py index d30b685d5a..8d9a246205 100644 --- a/autogpt_platform/backend/backend/server/v2/llm/db_write.py +++ b/autogpt_platform/backend/backend/server/v2/llm/db_write.py @@ -234,7 +234,11 @@ async def update_model( capabilities: dict[str, Any] | None = None, metadata: dict[str, Any] | None = None, ) -> prisma.models.LlmModel: - """Update an existing LLM model.""" + """Update an existing LLM model. + + When is_recommended=True, clears the flag on all other models first so + only one model can be recommended at a time. + """ # Build update data (only include fields that are provided) data: dict[str, Any] = {} if display_name is not None: @@ -266,11 +270,20 @@ async def update_model( if creator_id is not None: data["creatorId"] = creator_id if creator_id else None - model = await prisma.models.LlmModel.prisma().update( - where={"id": model_id}, - data=data, - include={"Costs": True, "Creator": True, "Provider": True}, - ) + async with transaction() as tx: + # Enforce single recommended model: unset all others first. + if is_recommended is True: + await tx.llmmodel.update_many( + where={"id": {"not": model_id}}, + data={"isRecommended": False}, + ) + + model = await tx.llmmodel.update( + where={"id": model_id}, + data=data, + include={"Costs": True, "Creator": True, "Provider": True}, + ) + if not model: raise ValueError(f"Model with id '{model_id}' not found") return model