Streaming AI Responses: Partial Text Is Not a Completed Result
Streaming improves perceived speed but adds cancellation and partial-output states. Keep tool actions, structured results and saved answers behind a verified completion boundary.

An assistant starts responding immediately, which feels faster than a blank loading screen. Halfway through, the connection drops. The application saves the unfinished answer as complete and enables an action based on a sentence the model never finished.
Streaming changes delivery, not correctness. The interface needs to distinguish arriving text from an accepted result and handle the gap deliberately.
Define the lifecycle
Use states such as connecting, streaming, completed, interrupted and cancelled. A stream closing is not always evidence of successful model completion; the provider protocol may include a finish event or reason you need to inspect.
Record the request identity and relevant workflow version. If the user retries, decide whether they are continuing a supported operation or starting a new generation. Do not append a second answer to the first merely because both arrived in the same text box.
The exact event format depends on the SDK and provider. Use maintained stream parsers rather than assuming every network chunk is one complete message.
Decode incrementally
UTF-8 characters and protocol records can split across network chunks. A raw chunk can contain half a character or several events. Parse at the protocol boundary, not at the transport boundary.
const decoder = new TextDecoder();
let text = class="text-code-string">"";
for await (const chunk of byteStream) {
text += decoder.decode(chunk, { stream: true });
}
text += decoder.decode();This demonstrates incremental text decoding only. It does not parse server-sent events, tool calls or a model-specific stream. Use the chosen SDK for those structures and validate the final output separately.
Avoid rendering unfinished markup with unsafe HTML insertion. Incremental Markdown can contain incomplete syntax and untrusted links. Keep sanitisation and safe rendering active throughout the stream.
Separate display from acceptance
Showing draft text can be useful. Saving it as a completed report or acting on it requires a different decision. Mark partial content visibly when interruption occurs and give the user a retry or discard path.
For structured output, accumulate the response and validate the complete schema after the provider reports completion. A prefix resembling valid JSON is not a valid business record.
Never execute a tool action just because its arguments appeared in partial text. Use the provider's structured tool-call mechanism, validate the completed arguments and enforce authorization outside the model.
Handle cancellation honestly
An AbortController or SDK cancellation mechanism can stop local waiting. It may not guarantee that the provider stops all computation or billing. Check the product's documented behaviour.
Stop rendering subsequent events for the cancelled request and prevent stale completion from replacing a newer answer. Keep a request identity check in the state transition.
If the workflow already created an external side effect, cancelling the text stream does not undo it. Consequential tools need their own transaction and confirmation design.
Control rendering work
Updating the entire transcript for every tiny token can create excessive main-thread work. Buffer updates at an appropriate interval or use the UI framework's efficient streaming pattern.
Maintain stable layout and preserve the reader's scroll choice. If they scroll upward to review earlier text, do not force them back to the bottom on every update. Offer a clear way to return to the newest content.
Avoid announcing every token to screen readers. Use a deliberate announcement strategy and provide the completed answer in navigable content. Constant live-region updates can make the interface unusable.
Plan for timeouts and stalled streams
Distinguish initial connection timeout, total operation deadline and an idle period with no events. Some workflows legitimately pause during tool execution, so a silence timeout must reflect the task rather than a universal guess.
Show the known stage without fabricated progress. If a tool is running, indicate that only when the application has evidence. Do not claim the model is checking facts simply because time has passed.
Keep partial text available for inspection when useful, but do not automatically treat it as final. The user should understand whether the operation completed.
Test the awkward endings
Interrupt the connection, cancel after partial output, start a second request before the first finishes and deliver a stale completion event. Test malformed protocol data and a provider refusal.
Verify that no partial record enters the authoritative store and no unauthorised tool action occurs. Check mobile scrolling and assistive technology behaviour during a long answer.
Streaming is successful when it reduces perceived waiting without blurring the outcome. The first words can arrive early; the application still needs a reliable moment when it can say the result is complete and accepted.
Paj Digital Labs
The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.
Related reading

Human Review Queues for AI: Confidence Is Not a Permission Slip
A model’s self-reported confidence does not establish accuracy. Route uncertain and consequential cases using tested rules, show reviewers the evidence and record their decisions without rubber-stamping.

AI Evaluation Sets: Keep the Test Data Out of the Tuning Loop
An evaluation stops being independent when every prompt revision is shaped around its answers. Separate development examples from held-out cases and measure meaningful failure categories.

AI Search Across Tenants: Permissions Belong in Retrieval, Not the Prompt
A model cannot enforce account boundaries with a polite instruction. Scope documents, caches and tool calls to the authenticated user before private text enters the context.