Mental model
A role-aware transcript keeps authorship attached to each message from transport parsing through display. It labels user input, assistant output, and tool results as different speakers, while preserving source parts as references. These visual cues help people follow a conversation, but they do not establish that a message is correct, trustworthy, or authorized.
Theory
A message role answers “who supplied this item?” A content part answers “what kind of data is inside it?” Those are related fields, but they are not interchangeable. A streamed assistant message can contain text deltas, a refusal part, citations, or tool activity; a tool result has its own identity and often arrives between two assistant messages. Keep the role and part type in structured data until the renderer decides how each is presented. The AI SDK UI protocol, for example, models text and source references as separate stream parts instead of requiring the client to flatten them into one string.
Normalize only the roles your product intentionally shows. A public conversation may render user, assistant, and tool; API roles such as system and developer generally belong to request construction or a restricted debugging view, not a customer transcript. If the stream adds a role the interface does not recognize, preserve it in diagnostic data and render a neutral “Unknown message type” state instead of silently styling it as assistant text. This prevents a schema change from making hidden or third-party content look like a first-party answer.
Tool identity should remain visible beside tool output. “Search results from catalog” communicates more than a green bubble that happens to look different. The assistant’s later summary is a new assistant message; it should not replace or rewrite the tool record. Likewise, a source URL or document part should remain a reference attached to the message or claim that supplied it. A neighboring link does not prove that every sentence is supported by that source.
Use headings, readable speaker labels, and semantic groupings along with visual styling. Color can reinforce a role distinction, but it cannot carry the distinction alone. The WAI-ARIA Authoring Practices Guide describes interaction patterns and keyboard behavior; choose native controls and appropriate semantics for your transcript rather than inventing a special ARIA role for “chat.” A screen reader should be able to identify the speaker and navigate the message without needing to infer meaning from a background color or icon.
Quantitative model
A useful renderer contract is role coverage:
Here, is the number of rendered messages in a sampled transcript and is the number whose displayed speaker agrees with the normalized role metadata. Both are message counts; is a percentage. In a trace of 200 messages, if 197 labels match their roles, coverage is . Inspect the three mismatches by type; one missing tool name may matter more than several cosmetic label typos. This measures rendering fidelity, not answer quality.
Trade-offs
Showing a speaker heading on every short delta can make a transcript noisy, so coalesce deltas into the same message while preserving stable message and part IDs. Do not merge a tool result into an assistant paragraph just to save vertical space. A compact header can contain a readable name, an optional tool name, and a timestamp; the message body can stay visually simple. Keep role treatment consistent across desktop, narrow screens, high-contrast settings, and exported transcripts.
A role label also should not become a trust badge. “Assistant” identifies the authoring role, not a verified source. “Tool result” identifies a producer, not a guarantee that the result is complete or safe. If a product needs to show verification, cite the actual verification process separately and keep that status independent from role styling.
Failure modes and misconceptions
Flattening every part into a string erases the boundary between model text, tool output, and source metadata. A later renderer cannot recover which spans came from a tool once that provenance is discarded. Defaulting unknown roles to “Assistant” creates a similar false attribution after a protocol change. Preserve the original value and make the fallback explicit.
A tool result can contain markup or an untrusted title. Render message text with text nodes, validate link schemes, and do not interpret a tool’s name as HTML. Attach citations to stable message or claim IDs; if stream updates replace content, resolve IDs before inserting annotations so a citation does not drift onto the wrong sentence.
Color-only bubbles, unlabeled icons, and hover-only speaker names leave gaps for keyboard and screen-reader users. A transcript-wide live region can also announce every partial token and overwhelm assistive technology. Keep the full transcript navigable, but announce meaningful state changes separately and at a deliberate cadence.
Decision scenario
A research assistant streams a sentence, invokes a web-search tool, receives three source URLs, and then writes a summary. Render the initial sentence and final summary as assistant messages, the search output with the tool’s name, and the URLs as source parts attached to the response that returned them. Do not style the tool as “verified” or append its sources to every sentence unless the response contract maps those sources to those claims. The role boundary preserves authorship; separate evidence metadata describes what is linked.
Learning outcomes
- Preserve roles and typed content parts across incremental stream updates.
- Render tool output and source references without attributing them to assistant prose.
- Explain why speaker styling communicates authorship but cannot certify correctness.
- Audit role-label coverage and accessibility using representative transcripts.
Implementation pattern
This renderer accepts only public transcript roles, builds DOM elements with text content, and adds source links only for explicit source parts. A product adapter should map provider events to this stable shape before rendering.
typescripttype Part =| { type: "text"; text: string }| { type: "source"; title: string; url: string };type Message = { id: string; role: "user" | "assistant" | "tool"; toolName?: string; parts: Part[] };export function renderMessage(root: HTMLElement, message: Message) {const article = document.createElement("article");article.dataset.messageId = message.id;article.dataset.role = message.role;const heading = document.createElement("h3");heading.textContent = message.role === "tool"? "Tool result: " + (message.toolName ?? "unnamed tool"): message.role === "user" ? "You" : "Assistant";article.append(heading);for (const part of message.parts) {if (part.type === "text") {const paragraph = document.createElement("p");paragraph.textContent = part.text;article.append(paragraph);} else {try {const url = new URL(part.url);if (url.protocol === "https:") {const link = document.createElement("a");link.href = url.toString();link.textContent = "Source: " + part.title;article.append(link);}} catch { /* Invalid source URLs are omitted and logged by the adapter. */ }}}root.append(article);return article;}