: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(posthog-ai): keep reading chats whose checkpoints hold a retired message type (#104646)
Jake Ruthf2bd8d190bdd
diff --git a/ee/hogai/django_checkpoint/checkpointer.py b/ee/hogai/django_checkpoint/checkpointer.pyindex 0277c0f9f62d..ff61f78b0c98 100644--- a/ee/hogai/django_checkpoint/checkpointer.py+++ b/ee/hogai/django_checkpoint/checkpointer.py@@ -19,6 +19,7 @@ from langgraph.checkpoint.base import ()from langgraph.checkpoint.serde.jsonplus import JsonPlusSerializer, _msgpack_ext_hook_to_jsonfrom langgraph.checkpoint.serde.types import TASKS, ChannelProtocol+from prometheus_client import Counterfrom posthog.sync import database_sync_to_async@@ -28,6 +29,34 @@ from products.posthog_ai.backend.models.assistant import (ConversationCheckpointWrite,)+# A checkpoint written before a message type left the schema still holds that message. Every member of+# the message union rejects the unknown literal, so the whole state fails to validate and the+# conversation reads as unsupported. `ai/router` held the 2025 router node's insight-type pick, such as+# "trends"; no surface rendered it, and the visualization message after it carries what the pick produced.+RETIRED_MESSAGE_TYPES = frozenset({"ai/router"})++MESSAGES_CHANNEL = "messages"++RETIRED_MESSAGES_DROPPED_COUNTER = Counter(+ "max_ai_retired_checkpoint_messages_dropped_total",+ "Messages dropped on checkpoint load because the schema no longer has their type.",+ labelnames=["message_type"],+)+++def drop_retired_messages(messages: Any) -> Any:+ """Remove messages whose type the schema dropped, so the rest of the conversation still loads."""+ if not isinstance(messages, list):+ return messages+ kept = []+ for message in messages:+ message_type = message.get("type") if isinstance(message, dict) else None+ if message_type in RETIRED_MESSAGE_TYPES:+ RETIRED_MESSAGES_DROPPED_COUNTER.labels(message_type=message_type).inc()+ continue+ kept.append(message)+ return kept+class DjangoCheckpointer(BaseCheckpointSaver[str]):jsonplus_serde = JsonPlusSerializer()@@ -163,6 +192,9 @@ class DjangoCheckpointer(BaseCheckpointSaver[str]):else {})+ if MESSAGES_CHANNEL in channel_values:+ channel_values[MESSAGES_CHANNEL] = drop_retired_messages(channel_values[MESSAGES_CHANNEL])+# langgraph-checkpoint dropped `pending_sends` from the Checkpoint TypedDict in 2.1, but the langgraph runtime still consumes it via `.get()`, so keep emitting it as an extra keycheckpoint_dict = cast(Checkpoint,diff --git a/ee/hogai/django_checkpoint/test/test_checkpointer.py b/ee/hogai/django_checkpoint/test/test_checkpointer.pyindex 8cc9d66b066c..68d731bd7f52 100644--- a/ee/hogai/django_checkpoint/test/test_checkpointer.py+++ b/ee/hogai/django_checkpoint/test/test_checkpointer.py@@ -665,6 +665,32 @@ class TestDjangoCheckpointer(NonAtomicBaseTest):}self.assertEqual(loaded.checkpoint, expected_checkpoint)+ async def test_message_type_dropped_from_schema_is_removed_on_load(self):+ thread = await Conversation.objects.acreate(user=self.user, team=self.team)+ saver = DjangoCheckpointer()++ human = HumanMessage(content="bring me all the leads")+ answer = AssistantMessage(content="228 lead creation events")+ retired = {"type": "ai/router", "content": "trends", "id": str(uuid4())}+ checkpoint_id = str(uuid6(clock_seq=-2))+ checkpoint = {+ "v": 1,+ "ts": "2025-02-01T20:14:19.804150+00:00",+ "id": checkpoint_id,+ "channel_values": {"messages": [human, retired, answer]},+ "channel_versions": {"messages": "1"},+ "versions_seen": {},+ "pending_sends": [],+ }+ config = {"configurable": {"thread_id": str(thread.id), "checkpoint_ns": ""}}+ await saver.aput(config, checkpoint, {"source": "input"}, {"messages": "1"})++ loaded = await saver.aget_tuple(config)+ messages = loaded.checkpoint["channel_values"]["messages"]+ self.assertEqual(messages, [human, answer])+ state = AssistantState.model_validate({"messages": messages})+ self.assertEqual([message.content for message in state.messages], [human.content, answer.content])+async def test_unknown_constructor_marker_returned_as_plain_dict(self):thread = await Conversation.objects.acreate(user=self.user, team=self.team)saver = DjangoCheckpointer()