fix(chart): render chart JSON from json/untagged fences#25
Merged
Conversation
…chart Small on-device models (Gemma E2B, the default) reliably emit valid chart JSON but tag the fence ```json or leave it untagged — never ```chart — even when explicitly instructed. So the chart feature from #23 never rendered in practice; the block fell back to a code block. Now we also attempt ChartParser.parse on json/untagged fences and render a chart when it succeeds. ChartParser only returns non-null for JSON with a recognized chart type (donut/bar/progress/line + synonyms) AND valid data, so ordinary JSON code samples still render as code. The explicit ```chart path is unchanged. Verified on-device (Realme CPH2723, release): historical Gemma answers that emitted untagged chart JSON now re-render as a donut + bar charts. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
#23 added inline charts: the model is meant to emit a
chart-tagged fenced JSON block, which `MarkdownText` detects via `lang == "chart"`. But on-device verification showed the default **Gemma 4 E2B reliably emits valid chart JSON taggedjson (or untagged) — never ```chart**, even when forcefully instructed. So charts never rendered in practice; the block fell back to a code block.Fix
MarkdownTextnow also attemptsChartParser.parseonjson/untagged fences and renders a chart when it succeeds. This is conservative:ChartParseronly returns non-null for JSON with a recognized charttype(donut/bar/progress/line + synonyms) and valid data, so ordinary JSON code samples still render as code. The explicit ```chart path is unchanged.Verified on-device
Realme CPH2723, release build: historical Gemma answers that had emitted untagged chart JSON now re-render as a donut chart (Monthly Budget) and bar charts (Quarterly Sales, Users) — proper on-brand Canvas charts, not code blocks.
🤖 Generated with Claude Code