Diagrams
Quizzes, exam papers and Ask replies can include figures: graphs, geometry, charts, number lines, science diagrams. They are delivered as complete, self-contained SVG strings inside the existing JSON responses. There are no image URLs, extra endpoints or extra round trips. Diagrams are an opt-in entitlement: ask us to enable svg_diagrams on your account. It takes effect on the next request, is reversible, and can be granted per environment. While it is off, no diagram fields appear.
Diagrams are additive and uncommon: most questions and replies carry none, so treat every diagram field as optional.
| Surface | Field | Location |
|---|---|---|
| Quiz | diagram_data |
On each object in questions[] in Retrieve a quiz |
| Exams | diagram_data |
On each paper question, same shape as Quiz |
| Ask | visualisations |
Top level of Send a message and GET /api/v1/messages/:id; inside chat on Retrieve a chat (latest assistant message only); in the final done event when streaming (see Streaming below) |
Quiz diagram_data
Omitted entirely when the question has no diagram or the entitlement is off. It is never null and never {}, so a presence check is enough.
| Key | Type | Description |
|---|---|---|
question_diagram |
object, optional | The figure belonging to the question stem |
option_diagrams |
object, optional | Map of option key (A–E) to a diagram object |
.svg |
string | Complete SVG document. Render inline |
.alt_text |
string | Description of the figure. Always present; use it as the accessible name |
.diagram_type |
string | What produced the figure (see below). Descriptive only; treat unknown values as normal |
Read only the keys above and ignore any key you do not recognise; do not store, display or fail on it.
Pick-the-graph questions: option_diagrams
Some multiple-choice questions ask the student to identify the correct graph or shape. Each option is a diagram, delivered in option_diagrams keyed by option letter. These are rare, but they are the one case where a missing render makes the question unanswerable.
When option_diagrams is present, the options array holds the placeholder strings "Graph A", "Graph B" and so on. That text is not answer content: render each option’s figure in place of its text label and submit the option key. Iterate the object’s own keys rather than assuming four options. A question may have both a stem figure and option figures; handle them independently.
Ask visualisations
An array, omitted when empty. A reply can carry several figures.
format is svg. If you ever receive another value, show alt_text as a caption and skip the figure; never inject unknown content as HTML. As a defensive measure, strip any inline
content before display; the figures are supplied in the array. This regex allows > inside quoted attribute values:
Streaming
With stream: true, figures are not sent in the text chunks. They arrive once, in the final done event, after all text, with the same shape as the array above. A client that only reads unnamed data: frames will never see them.
| Event | Meaning | Client action |
|---|---|---|
start |
The reply has begun. On Create a chat a first start carries the new chat, and the reply opens with message_start |
Open a reply bubble |
| (unnamed) | A text chunk in message.content |
Append to the reply |
restart |
The text so far is being replaced | Clear the reply’s text; the chunks that follow are the whole answer |
done |
The reply is complete. Holds usage, and visualisations when the reply has a figure (omitted otherwise) |
Render each figure |
error |
The turn failed; a done with {"error":true} follows |
Show an error; no figures |
One reply can span two message ids. When the tutor draws a figure partway through a reply, the text before and after the figure are stored as separate messages, so chunks change message.id mid-stream. Show all chunks in order as one reply; do not start a new bubble or drop chunks when the id changes. The figures in done belong to the reply as a whole.
The SVG
- Each SVG is a complete
<svg>document, safe to inline: no scripts, no event-handler attributes and no external references. Rendering makes no network requests, so inlining needs noimg-src,connect-srcorfont-srcCSP allowance. - The root has
viewBox,width="100%",role="img"and an inlinemax-widthstyle; fonts aresystem-uistacks. - Each SVG is a few KB and never more than 50 KB.
diagram_typedescribes the kind of figure, for examplecoordinate_graph,geometry,bar_chart,pie_chart,line_chart,number_line,box_whisker,physics,chemistry,biology,economics,contextual,primary,solidornetwork. Other values may be added; treat unknown values as normal and render the SVG.- A diagram returned for a question or message is identical on every later read, so cache it with the record.
Integration checklist
- One shared figure component taking
{ svg, alt_text }. Inline the markup (do not wrap it in adata:image) and setaria-labelfromalt_text. - Guard against empty renders. Occasionally a stored SVG draws nothing but a background. Before injecting, test for at least one drawn element and fall back to
alt_textotherwise:/<(path|circle|ellipse|line|polygon|polyline|text|image)b/.test(svg). - Handle
option_diagramsin the MCQ renderer (above). This is the only change that affects answer selection. - Treat diagrams as optional everywhere; design layouts so no figure is the default.
- Use a light surface. Figures are dark-on-white with meaningful colour; do not apply
filter: invert()for dark mode. - Presence can vary. Diagrams are added to content generated after enablement, but treat any quiz as possibly carrying one and handle presence normally.