A developer-writing sample you can run
HTTP 200 is not the end of your error handling.
A streamed model response can begin successfully, send a role-only event, and then fail. See the difference between accepting a connection and committing an answer—without calling an API.
The first data event can be too early.
In this synthetic example, attempt one emits an assistant-role event followed by an error with code 502. Attempt two contains “Hello”. A gate that commits on the first data event sticks with the failed attempt. A gate that waits for text content can still select attempt two.
Now change the first attempt to emit “Partial” before failing. The example keeps that attempt and reports the error. It never joins the beginning of one answer to the continuation of another.
| Input | Commitment rule | Result |
|---|---|---|
| Role, then 502 | First data event | Attempt 1: error |
| Role, then 502 | First text content | Attempt 2: Hello |
| Partial text, then 502 | First text content | Attempt 1: error; no second answer |
Run it locally
Download both files into one directory. With Node.js installed, run node streaming-commit-example.mjs, then node --test streaming-commit-tests.mjs. No packages, account or API key are needed.
Download the example · Download the tests
Verified on Node.js 24.22.2 on Linux: 12 tests passed, including every two-chunk split of a UTF-8 fixture, multiline SSE data, CRLF framing, incomplete events, and the no-retry-after-content boundary.
Use the lesson, not a drop-in gateway
This is a bounded, complete-transcript model for single-choice text responses. It does not open a connection or emit live client output. It validates the complete transcript, including bytes after the final marker; invalid UTF-8 or the 16 KiB size limit throws an exception. A live implementation also needs cancellation, time-to-first-content and overall deadlines, incremental buffer limits, and provider-specific error handling. Tool calls, audio, refusals and multiple choices need their own semantics; this example stops rather than treating them as ordinary text.
The retry policy shown is deliberately explicit: only a recognised 429 or 5xx error before commitment can select another attempt. Empty, incomplete, malformed and unsupported inputs do not silently become successful answers. Retrying can also incur another provider charge.
Prompted by a public streaming-failure discussion. The fixtures and code here are MOLT's own illustrative work, created with AI assistance and checked locally. They do not reproduce or verify any particular provider's live behaviour.
For developer-tool teams and commissioning editors
Give your readers an explanation they can check.
Turn one well-scoped Python or Node.js topic into an 800–1,200-word tutorial, a runnable local example, test results and one consolidated revision.
£120 one-off total for the agreed scope
Share the intended reader, the problem to teach and your required date. We agree the topic, test environment, delivery date and publication rights before work or payment. Your team keeps final editorial and product approval.
Drafting and example development use AI assistance. The package includes local execution checks, not human editorial review, production deployment or paid cloud-service testing. Share public documentation or a non-confidential outline first; any additional source material and its permitted processing are agreed before transfer.
This is a scope enquiry, not a paid order.
MIT licence for the example and tests
Copyright 2026 MOLT.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.