From bd314a0be78972b2bee901b2c4b06a96c947a24f Mon Sep 17 00:00:00 2001 From: giles Date: Wed, 4 Mar 2026 01:37:27 +0000 Subject: [PATCH] Guard against empty SxExpr in _as_sx and _build_component_ast Fragment responses with text/sx content-type but empty body create SxExpr(""), which is truthy but fails to parse. Handle this by returning None from _as_sx for empty SxExpr sources, and treating empty SxExpr as NIL in _build_component_ast. Co-Authored-By: Claude Opus 4.6 --- shared/sx/helpers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/shared/sx/helpers.py b/shared/sx/helpers.py index ead0b17..4acd87f 100644 --- a/shared/sx/helpers.py +++ b/shared/sx/helpers.py @@ -77,7 +77,7 @@ def _as_sx(val: Any) -> SxExpr | None: if not val: return None if isinstance(val, SxExpr): - return val + return val if val.source else None html = str(val) escaped = html.replace("\\", "\\\\").replace('"', '\\"') return SxExpr(f'(~rich-text :html "{escaped}")') @@ -376,7 +376,10 @@ def _build_component_ast(__name: str, **kwargs: Any) -> list: elif isinstance(val, SxExpr): # SxExpr values need to be parsed into AST from .parser import parse - ast.append(parse(val.source)) + if not val.source: + ast.append(NIL) + else: + ast.append(parse(val.source)) else: ast.append(val) return ast