: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
feat(llm): accept refusal answers from system one servers (#113396)
Rafael Audibert2e29ea110430
diff --git a/posthog/llm/system_one.py b/posthog/llm/system_one.pyindex 2557d3517b49..8dce896bfbfb 100644--- a/posthog/llm/system_one.py+++ b/posthog/llm/system_one.py@@ -3,7 +3,8 @@A System One model such as Jev answers typed questions about a ``state`` and returns judgmentsrather than text. A noul question returns the probability that a yes/no statement holds. A choicequestion picks one option from the criteria the caller supplies, with a probability for every-option. A score question returns a fractional index into an ordered list of rubric levels.+option. A score question returns a fractional index into an ordered list of rubric levels. A model+can refuse any question, which answers it with a refusal instead.Keep user text in ``state`` and refer to it from the instructions by name. Never interpolate itinto ``instructions``, so user text cannot become an instruction.@@ -113,7 +114,12 @@ class ScoreAnswer:probabilities: Mapping[str, float]-type Answer = NoulAnswer | ChoiceAnswer | ScoreAnswer+@frozen+class RefusalAnswer:+ """The model declined to answer this question. Other answers in the same result stay usable."""+++type Answer = NoulAnswer | ChoiceAnswer | ScoreAnswer | RefusalAnswer@frozen@@ -150,6 +156,8 @@ def _parse_answer(question_id: str, question: Question, raw: object) -> Answer:answer = _as_mapping(raw)if answer is None:raise SystemOneRequestFailed(f"The System One server returned no answer for {question_id!r}")+ if answer.get("type") == "refusal":+ return RefusalAnswer()if isinstance(question, NoulQuestion):probability = _as_probability(answer.get("noul"))diff --git a/posthog/llm/test_system_one.py b/posthog/llm/test_system_one.pynew file mode 100644index 000000000000..da020c34e3d2--- /dev/null+++ b/posthog/llm/test_system_one.py@@ -0,0 +1,30 @@+from parameterized import parameterized++from posthog.llm.system_one import (+ ChoiceQuestion,+ NoulAnswer,+ NoulQuestion,+ Question,+ RefusalAnswer,+ ScoreQuestion,+ parse_system_one_response,+)+++ [+ ("noul", NoulQuestion(instructions="Urgent?")),+ ("choice", ChoiceQuestion(instructions="Which team?", criteria={"billing": None, "support": None})),+ ("score", ScoreQuestion(instructions="How severe?", criteria=["low", "high"])),+ ]+)+def test_a_refused_question_keeps_the_other_answers(_name: str, refused: Question) -> None:+ result = parse_system_one_response(+ {+ "model": "gpt-6-luna",+ "answers": {"refused": {"type": "refusal"}, "answered": {"type": "noul", "noul": 0.7}},+ },+ {"refused": refused, "answered": NoulQuestion(instructions="Polite?")},+ )++ assert result.answers == {"refused": RefusalAnswer(), "answered": NoulAnswer(probability=0.7)}