: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(llma): coerce stringified ph-query attrs in tiptap_doc_to_text (#58765)
Georgiy Tarasov008d27339110
diff --git a/ee/hogai/tools/create_notebook/test_tiptap.py b/ee/hogai/tools/create_notebook/test_tiptap.pyindex 891be16471ef..e37cf8c23c66 100644--- a/ee/hogai/tools/create_notebook/test_tiptap.py+++ b/ee/hogai/tools/create_notebook/test_tiptap.py@@ -411,3 +411,134 @@ class TestTiptapDocToText(SimpleTestCase):assert "# Report" in resultassert "Summary below." in resultassert "- item" in result++ def test_ph_query_node_with_stringified_query(self):+ # Some persisted notebooks store `attrs.query` as a JSON-encoded string rather than a dict.+ # The formatter must parse it instead of crashing with AttributeError.+ doc = {+ "type": "doc",+ "content": [+ {+ "type": "ph-query",+ "attrs": {+ "title": "Pages les plus touchées",+ "query": '{"kind": "InsightVizNode", "source": {"kind": "TrendsQuery"}}',+ },+ }+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert '<insight title="Pages les plus touchées"' in result+ assert 'query_kind="InsightVizNode"' in result+ assert 'source_kind="TrendsQuery"' in result++ def test_ph_query_node_with_unparseable_query_string(self):+ doc = {+ "type": "doc",+ "content": [+ {+ "type": "ph-query",+ "attrs": {"title": "Broken", "query": "not-json"},+ }+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert '<insight title="Broken"' in result+ assert 'query_kind="unknown"' in result+ assert "source_kind=" not in result++ def test_ph_query_node_with_string_source(self):+ doc = {+ "type": "doc",+ "content": [+ {+ "type": "ph-query",+ "attrs": {+ "title": "Edgy",+ "query": {"kind": "InsightVizNode", "source": "TrendsQuery"},+ },+ }+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert 'query_kind="InsightVizNode"' in result+ assert "source_kind=" not in result++ def test_ph_query_node_missing_attrs(self):+ doc = {"type": "doc", "content": [{"type": "ph-query"}]}+ result = tiptap_doc_to_text(doc)+ assert '<insight title="Untitled"' in result+ assert 'query_kind="unknown"' in result++ @parameterized.expand(+ [+ ("string_node", "stray string"),+ ("none_node", None),+ ("number_node", 42),+ ("list_node", ["unexpected"]),+ ]+ )+ def test_non_dict_top_level_node_is_skipped(self, _name, bad_node):+ doc = {+ "type": "doc",+ "content": [+ {"type": "paragraph", "content": [{"type": "text", "text": "before"}]},+ bad_node,+ {"type": "paragraph", "content": [{"type": "text", "text": "after"}]},+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert "before" in result+ assert "after" in result++ def test_non_dict_inline_node_is_skipped(self):+ doc = {+ "type": "doc",+ "content": [+ {+ "type": "paragraph",+ "content": [+ {"type": "text", "text": "kept"},+ "stray inline string",+ {"type": "text", "text": " also kept"},+ ],+ },+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert result == "kept also kept"++ def test_non_dict_list_item_is_skipped(self):+ doc = {+ "type": "doc",+ "content": [+ {+ "type": "bulletList",+ "content": [+ "stray",+ {+ "type": "listItem",+ "content": [{"type": "paragraph", "content": [{"type": "text", "text": "real"}]}],+ },+ ],+ }+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert "- real" in result++ def test_string_attrs_are_coerced_or_ignored(self):+ # Even if `attrs` itself is a string, we should not crash.+ doc = {+ "type": "doc",+ "content": [+ {"type": "ph-recording", "attrs": "not-a-dict"},+ {+ "type": "ph-recording",+ "attrs": '{"id": "sess-json"}',+ },+ ],+ }+ result = tiptap_doc_to_text(doc)+ assert '<session_replay id="unknown" />' in result+ assert '<session_replay id="sess-json" />' in resultdiff --git a/ee/hogai/tools/create_notebook/tiptap.py b/ee/hogai/tools/create_notebook/tiptap.pyindex b7f3764f54ca..0aee447f3b7f 100644--- a/ee/hogai/tools/create_notebook/tiptap.py+++ b/ee/hogai/tools/create_notebook/tiptap.py@@ -323,11 +323,27 @@ def tiptap_doc_to_text(doc: dict | None) -> str:return "\n\n".join(parts)-def _tiptap_node_to_text(node: dict) -> str:+def _coerce_to_dict(value: Any) -> dict:+ if isinstance(value, dict):+ return value+ if isinstance(value, str):+ try:+ parsed = json.loads(value)+ except (ValueError, TypeError):+ return {}+ return parsed if isinstance(parsed, dict) else {}+ return {}+++def _tiptap_node_to_text(node: Any) -> str:+ if not isinstance(node, dict):+ return ""+node_type = node.get("type", "")+ attrs = _coerce_to_dict(node.get("attrs"))if node_type == "heading":- level = node.get("attrs", {}).get("level", 1)+ level = attrs.get("level", 1)inline = _tiptap_inline_to_text(node.get("content", []))return f"{'#' * level} {inline}"@@ -335,7 +351,7 @@ def _tiptap_node_to_text(node: dict) -> str:return _tiptap_inline_to_text(node.get("content", []))if node_type == "codeBlock":- lang = node.get("attrs", {}).get("language", "")+ lang = attrs.get("language", "")code = _tiptap_inline_to_text(node.get("content", []))return f"```{lang}\n{code}\n```"@@ -354,12 +370,13 @@ def _tiptap_node_to_text(node: dict) -> str:return "\n".join(items)if node_type == "ph-query":- attrs = node.get("attrs", {})title = attrs.get("title", "Untitled")- query = attrs.get("query", {})+ # `attrs.query` is sometimes persisted as a JSON-encoded string rather than a dict,+ # which used to crash the LangGraph root node when the agent surfaced a notebook.+ query = _coerce_to_dict(attrs.get("query"))query_kind = query.get("kind", "unknown")- source = query.get("source", {})- source_kind = source.get("kind", "") if isinstance(source, dict) else ""+ source = _coerce_to_dict(query.get("source"))+ source_kind = source.get("kind", "")parts = [f'<insight title="{title}" query_kind="{query_kind}"']if source_kind:parts[0] += f' source_kind="{source_kind}"'@@ -369,7 +386,6 @@ def _tiptap_node_to_text(node: dict) -> str:return "\n".join(parts)if node_type == "ph-recording":- attrs = node.get("attrs", {})session_id = attrs.get("id", "unknown")return f'<session_replay id="{session_id}" />'@@ -380,7 +396,9 @@ def _tiptap_node_to_text(node: dict) -> str:return ""-def _tiptap_list_item_to_text(item: dict) -> str:+def _tiptap_list_item_to_text(item: Any) -> str:+ if not isinstance(item, dict):+ return ""children = item.get("content", [])parts = []for child in children:@@ -388,9 +406,13 @@ def _tiptap_list_item_to_text(item: dict) -> str:return " ".join(p for p in parts if p)-def _tiptap_inline_to_text(nodes: list[dict]) -> str:+def _tiptap_inline_to_text(nodes: Any) -> str:+ if not isinstance(nodes, list):+ return ""parts: list[str] = []for node in nodes:+ if not isinstance(node, dict):+ continueif node.get("type") == "text":text = node.get("text", "")marks = node.get("marks", [])