Skip to content

: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.

fix: reject choice questions without choices in Max tools (#54283)

Luke Belton45d376616c03
diff --git a/products/surveys/backend/max_tools.py b/products/surveys/backend/max_tools.py
index 6ce7805c46d4..d3d8a4836bd2 100644
--- a/products/surveys/backend/max_tools.py
+++ b/products/surveys/backend/max_tools.py
@@ -77,6 +77,8 @@ def get_team_survey_config(team: Team) -> dict[str, Any]:
def _build_question(q: SimpleSurveyQuestion) -> dict[str, Any]:
"""Convert a SimpleSurveyQuestion to the internal question dict."""
+ if q.type in ("single_choice", "multiple_choice") and not q.choices:
+ raise serializers.ValidationError(f"Question of type '{q.type}' must include a non-empty 'choices' list")
result = dict(QUESTION_TYPE_MAP[q.type])
result["question"] = q.question
if q.description is not None:
diff --git a/products/surveys/backend/test_max_tools.py b/products/surveys/backend/test_max_tools.py
index bc6caf6d01f4..e0188cda35ac 100644
--- a/products/surveys/backend/test_max_tools.py
+++ b/products/surveys/backend/test_max_tools.py
@@ -86,6 +86,21 @@ class TestSurveyCreatorTool(BaseTest):
assert artifact["error"] == "validation_failed"
assert "No questions provided" in artifact["error_message"]
+ @parameterized.expand([("single_choice",), ("multiple_choice",)])
+ @pytest.mark.django_db
+ @pytest.mark.asyncio
+ async def test_arun_impl_choice_question_without_choices_validation(self, question_type):
+ tool = self._setup_tool()
+
+ content, artifact = await tool._arun_impl(
+ name="Test Survey",
+ questions=[SimpleSurveyQuestion(type=question_type, question="Pick one")],
+ )
+
+ assert "Survey validation failed" in content
+ assert artifact["error"] == "validation_failed"
+ assert "choices" in artifact["error_message"]
+
@pytest.mark.django_db
@pytest.mark.asyncio
async def test_arun_impl_with_launch(self):
@@ -621,6 +636,22 @@ class TestEditSurveyTool(BaseTest):
assert updated_survey.questions[0]["type"] == "rating"
assert updated_survey.questions[0]["scale"] == 5
+ @parameterized.expand([("single_choice",), ("multiple_choice",)])
+ @pytest.mark.django_db
+ @pytest.mark.asyncio
+ async def test_edit_survey_choice_question_without_choices_validation(self, question_type):
+ tool = self._setup_tool()
+ survey = await self._create_test_survey()
+
+ content, artifact = await tool._arun_impl(
+ survey_id=str(survey.id),
+ questions=[SimpleSurveyQuestion(type=question_type, question="Pick one")],
+ )
+
+ assert "Survey validation failed" in content
+ assert artifact["error"] == "validation_failed"
+ assert "choices" in artifact["error_message"]
+
@pytest.mark.django_db
@pytest.mark.asyncio
async def test_edit_survey_questions_preserves_ids_with_numeric_labels(self):