LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
[MISC] Optimize prompt studio list endpoint with lightweight serializer (#1901)
Chandrasekharan M93c06346add0
diff --git a/backend/prompt_studio/prompt_studio_core_v2/serializers.py b/backend/prompt_studio/prompt_studio_core_v2/serializers.pyindex 14ce69be5..4f10ee2aa 100644--- a/backend/prompt_studio/prompt_studio_core_v2/serializers.py+++ b/backend/prompt_studio/prompt_studio_core_v2/serializers.py@@ -34,6 +34,47 @@ except ImportError:from file_management.constants import FileInformationKey as FileKey+class CustomToolListSerializer(serializers.ModelSerializer):+ """Lightweight serializer for the list endpoint.++ Avoids the O(tools x prompts) queries that CustomToolSerializer.to_representation+ causes by skipping profile lookups, prompt fetching, and coverage calculation.+ """++ created_by_email = serializers.SerializerMethodField()+ prompt_count = serializers.SerializerMethodField()++ class Meta:+ model = CustomTool+ fields = [+ "tool_id",+ "tool_name",+ "description",+ "author",+ "created_by",+ "created_at",+ "modified_at",+ "shared_to_org",+ "icon",+ "created_by_email",+ "prompt_count",+ ]++ def get_created_by_email(self, instance):+ return instance.created_by.email if instance.created_by else ""++ def get_prompt_count(self, instance):+ if hasattr(instance, "_prompt_count"):+ return instance._prompt_count or 0+ # Fallback triggers a per-instance query if annotation is missing+ logger.warning(+ "CustomToolListSerializer used without _prompt_count annotation "+ "for tool %s — falling back to per-instance query",+ instance.tool_id,+ )+ return instance.mapped_prompt.count()++class CustomToolSerializer(IntegrityErrorMixin, AuditSerializer):shared_users = serializers.PrimaryKeyRelatedField(queryset=User.objects.filter(is_service_account=False),diff --git a/backend/prompt_studio/prompt_studio_core_v2/views.py b/backend/prompt_studio/prompt_studio_core_v2/views.pyindex 5ad9d4ba7..43d0b0a2a 100644--- a/backend/prompt_studio/prompt_studio_core_v2/views.py+++ b/backend/prompt_studio/prompt_studio_core_v2/views.py@@ -12,7 +12,7 @@ from api_v2.models import APIDeploymentfrom celery import signaturefrom celery.result import AsyncResultfrom django.db import IntegrityError-from django.db.models import QuerySet+from django.db.models import Count, OuterRef, QuerySet, Subqueryfrom django.http import HttpRequest, HttpResponsefrom file_management.constants import FileInformationKey as FileKeyfrom file_management.exceptions import FileNotFound@@ -77,6 +77,7 @@ from unstract.sdk1.utils.common import Utils as CommonUtilsfrom .models import CustomToolfrom .serializers import (+ CustomToolListSerializer,CustomToolSerializer,FileInfoIdeSerializer,FileUploadIdeSerializer,@@ -95,6 +96,11 @@ class PromptStudioCoreView(viewsets.ModelViewSet):serializer_class = CustomToolSerializer+ def get_serializer_class(self):+ if self.action == "list":+ return CustomToolListSerializer+ return CustomToolSerializer+def get_permissions(self) -> list[Any]:if self.action == "destroy":return [IsOwner()]@@ -102,7 +108,20 @@ class PromptStudioCoreView(viewsets.ModelViewSet):return [IsOwnerOrSharedUserOrSharedToOrg()]def get_queryset(self) -> QuerySet | None:- return CustomTool.objects.for_user(self.request.user)+ qs = CustomTool.objects.for_user(self.request.user)+ if self.action == "list":+ # Subquery avoids conflict with distinct("tool_id") from for_user()+ prompt_count_sq = (+ ToolStudioPrompt.objects.filter(tool_id=OuterRef("pk"))+ .order_by()+ .values("tool_id")+ .annotate(cnt=Count("id"))+ .values("cnt")+ )+ qs = qs.select_related("created_by").annotate(+ _prompt_count=Subquery(prompt_count_sq)+ )+ return qsdef get_object(self):"""Override get_object to trigger lazy migration when accessing tools."""