:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): declare order_by on the prompt list query serializer (#74233)
Juraj Majerikbf342ab50003
diff --git a/posthog/api/llm_prompt.py b/posthog/api/llm_prompt.pyindex a986317fb705..54b6a07a8334 100644--- a/posthog/api/llm_prompt.py+++ b/posthog/api/llm_prompt.py@@ -15,6 +15,7 @@ from rest_framework.serializers import BaseSerializerfrom posthog.api.capture import capture_internalfrom posthog.api.llm_prompt_serializers import (+ ALLOWED_LIST_ORDERINGS,LLMPromptDuplicateSerializer,LLMPromptFetchQuerySerializer,LLMPromptGetByNameQuerySerializer,@@ -72,24 +73,6 @@ from products.ai_observability.backend.models.llm_prompt import LLMPrompt, LLMPrPROMPT_FETCHED_EVENT = "$llm_prompt_fetched"PROMPT_FETCHED_EVENT_SOURCE = "llm_prompt_management"-ALLOWED_LIST_ORDERINGS = {- "name": "name",- "-name": "-name",- "created_at": "created_at",- "-created_at": "-created_at",- "updated_at": "updated_at",- "-updated_at": "-updated_at",- "version": "version",- "-version": "-version",- "latest_version": "latest_version",- "-latest_version": "-latest_version",- "version_count": "version_count",- "-version_count": "-version_count",- "first_version_created_at": "first_version_created_at",- "-first_version_created_at": "-first_version_created_at",- "prompt_size_bytes": "prompt_size_bytes",- "-prompt_size_bytes": "-prompt_size_bytes",-}@extend_schema(extensions={"x-product": "llm_analytics"})@@ -226,7 +209,7 @@ class LLMPromptViewSet(if created_by_id:queryset = queryset.filter(created_by_id=created_by_id)- order_by = request.query_params.get("order_by", "-created_at")+ order_by = params.get("order_by", "-created_at")queryset = queryset.order_by(ALLOWED_LIST_ORDERINGS.get(order_by, "-created_at"), "-id")return querysetdiff --git a/posthog/api/llm_prompt_serializers.py b/posthog/api/llm_prompt_serializers.pyindex ec937c6a053f..df0e6eb647a7 100644--- a/posthog/api/llm_prompt_serializers.py+++ b/posthog/api/llm_prompt_serializers.py@@ -83,6 +83,27 @@ def validate_prompt_label_name_value(value: str) -> str:return value+# Maps accepted order_by values to queryset ordering fields. Lives here so the list+# query serializer can declare the choices; the viewset imports it for the lookup.+ALLOWED_LIST_ORDERINGS = {+ "name": "name",+ "-name": "-name",+ "created_at": "created_at",+ "-created_at": "-created_at",+ "updated_at": "updated_at",+ "-updated_at": "-updated_at",+ "version": "version",+ "-version": "-version",+ "latest_version": "latest_version",+ "-latest_version": "-latest_version",+ "version_count": "version_count",+ "-version_count": "-version_count",+ "first_version_created_at": "first_version_created_at",+ "-first_version_created_at": "-first_version_created_at",+ "prompt_size_bytes": "prompt_size_bytes",+ "-prompt_size_bytes": "-prompt_size_bytes",+}+CONTENT_MODE_CHOICES = ["full", "preview", "none"]CONTENT_MODE_HELP = ("Controls how much prompt content is included in the response. "@@ -137,6 +158,12 @@ class LLMPromptListQuerySerializer(serializers.Serializer):required=False,help_text="Filter prompts by the ID of the user who created them.",)+ order_by = serializers.ChoiceField(+ choices=list(ALLOWED_LIST_ORDERINGS),+ required=False,+ default="-created_at",+ help_text="Field to sort the prompt list by. Prefix with '-' for descending order.",+ )content = serializers.ChoiceField(choices=CONTENT_MODE_CHOICES,required=False,diff --git a/posthog/api/test/test_llm_prompt.py b/posthog/api/test/test_llm_prompt.pyindex f822d6091278..adcf1d078d25 100644--- a/posthog/api/test/test_llm_prompt.py+++ b/posthog/api/test/test_llm_prompt.py@@ -16,6 +16,7 @@ from posthog.api.llm_prompt import LLMPromptViewSetfrom posthog.api.llm_prompt_serializers import (MAX_PROMPT_PAYLOAD_BYTES,LLMPromptDuplicateSerializer,+ LLMPromptListQuerySerializer,validate_prompt_label_name_value,)from posthog.api.services.llm_prompt import MAX_PROMPT_VERSION@@ -1036,6 +1037,15 @@ class TestLLMPromptDuplicateSerializerValidationNoDB(SimpleTestCase):assert serializer.is_valid(), serializer.errors+class TestLLMPromptListQuerySerializerValidationNoDB(SimpleTestCase):+ def test_rejects_unknown_order_by(self) -> None:+ # order_by used to be read from raw query params with a silent fallback; the serializer+ # now owns the contract, so an unknown value must fail validation (and 400 at the endpoint).+ serializer = LLMPromptListQuerySerializer(data={"order_by": "prompt"})+ assert not serializer.is_valid()+ assert "order_by" in serializer.errors++class TestLLMPromptLabelsAPI(APIBaseTest):def create_prompt_version(self,diff --git a/products/ai_observability/frontend/generated/api.schemas.ts b/products/ai_observability/frontend/generated/api.schemas.tsindex d366f36c0020..e4dec7a3c1af 100644--- a/products/ai_observability/frontend/generated/api.schemas.ts+++ b/products/ai_observability/frontend/generated/api.schemas.ts@@ -3016,6 +3016,28 @@ export type LlmPromptsListParams = {* The initial index from which to return the results.*/offset?: number+ /**+ * Field to sort the prompt list by. Prefix with '-' for descending order.+ *+ * * `name` - name+ * * `-name` - -name+ * * `created_at` - created_at+ * * `-created_at` - -created_at+ * * `updated_at` - updated_at+ * * `-updated_at` - -updated_at+ * * `version` - version+ * * `-version` - -version+ * * `latest_version` - latest_version+ * * `-latest_version` - -latest_version+ * * `version_count` - version_count+ * * `-version_count` - -version_count+ * * `first_version_created_at` - first_version_created_at+ * * `-first_version_created_at` - -first_version_created_at+ * * `prompt_size_bytes` - prompt_size_bytes+ * * `-prompt_size_bytes` - -prompt_size_bytes+ * @minLength 1+ */+ order_by?: string/*** Optional substring filter applied to prompt names and prompt content.*/diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 0d636bade5f0..f6b185dd013f 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -76593,6 +76593,28 @@ export namespace Schemas {* The initial index from which to return the results.*/offset?: number;+ /**+ * Field to sort the prompt list by. Prefix with '-' for descending order.+ *+ * * `name` - name+ * * `-name` - -name+ * * `created_at` - created_at+ * * `-created_at` - -created_at+ * * `updated_at` - updated_at+ * * `-updated_at` - -updated_at+ * * `version` - version+ * * `-version` - -version+ * * `latest_version` - latest_version+ * * `-latest_version` - -latest_version+ * * `version_count` - version_count+ * * `-version_count` - -version_count+ * * `first_version_created_at` - first_version_created_at+ * * `-first_version_created_at` - -first_version_created_at+ * * `prompt_size_bytes` - prompt_size_bytes+ * * `-prompt_size_bytes` - -prompt_size_bytes+ * @minLength 1+ */+ order_by?: string;/*** Optional substring filter applied to prompt names and prompt content.*/diff --git a/services/mcp/src/generated/ai_observability/api.ts b/services/mcp/src/generated/ai_observability/api.tsindex 8289651baf44..cdc23f847302 100644--- a/services/mcp/src/generated/ai_observability/api.ts+++ b/services/mcp/src/generated/ai_observability/api.ts@@ -1657,6 +1657,7 @@ export const LlmPromptsListParams = /* @__PURE__ */ zod.object({})export const llmPromptsListQueryContentDefault = `full`+export const llmPromptsListQueryOrderByDefault = `-created_at`export const LlmPromptsListQueryParams = /* @__PURE__ */ zod.object({content: zod@@ -1668,6 +1669,13 @@ export const LlmPromptsListQueryParams = /* @__PURE__ */ zod.object({created_by_id: zod.number().optional().describe('Filter prompts by the ID of the user who created them.'),limit: zod.number().optional().describe('Number of results to return per page.'),offset: zod.number().optional().describe('The initial index from which to return the results.'),+ order_by: zod+ .string()+ .min(1)+ .default(llmPromptsListQueryOrderByDefault)+ .describe(+ "Field to sort the prompt list by. Prefix with '-' for descending order.\n\n\* `name` - name\n\* `-name` - -name\n\* `created_at` - created_at\n\* `-created_at` - -created_at\n\* `updated_at` - updated_at\n\* `-updated_at` - -updated_at\n\* `version` - version\n\* `-version` - -version\n\* `latest_version` - latest_version\n\* `-latest_version` - -latest_version\n\* `version_count` - version_count\n\* `-version_count` - -version_count\n\* `first_version_created_at` - first_version_created_at\n\* `-first_version_created_at` - -first_version_created_at\n\* `prompt_size_bytes` - prompt_size_bytes\n\* `-prompt_size_bytes` - -prompt_size_bytes"+ ),search: zod.string().optional().describe('Optional substring filter applied to prompt names and prompt content.'),})