﻿# Recognize OpenRouter keepalive comments without treating them as model text

Parse OpenRouter SSE streams with comment handling, event buffering and endpoint-specific completion, keeping transport keepalives separate from model answer text.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/learn-openrouter-stream-comments/
Language: en

A chat interface can crash even when the model and network behave correctly if its stream parser treats every received line as JSON. OpenRouter documents occasional SSE comments used to keep connections alive. A robust client parses the event framing first, then interprets the selected API’s data events and completion state.

## Recognize comment lines

The documented keepalive example begins with a colon: `: OPENROUTER PROCESSING`. Such a line is an SSE comment and can be ignored for answer assembly. It may support a loading indicator, but it is not a generated token or a final response. Sending it directly to a JSON parser creates a client-side error unrelated to the content the model is producing.

## Buffer transport chunks into events

Network chunks need not align with complete SSE events or complete character sequences. Use a parser that handles framing and buffered decoding rather than assuming each read is one JSON object. For a hypothetical code-explanation UI, a text delta may arrive across several transport reads. The parser assembles an event before the application decides whether its data contains text, a tool update or another supported payload.

## Interpret completion by endpoint

The current streaming guide distinguishes Chat Completions accounting chunks from Responses completion events. Chat Completions includes a final usage frame before its done marker, with a content-free choice that repeats the finish reason. Treat that as accounting rather than a second answer. Responses reports usage through its completed event. A shared client can normalize its UI while preserving those endpoint-specific lifecycle rules internally.

## Handle errors after headers are committed

An error before streaming can return a normal JSON error response. Once response headers are committed, a later failure can arrive as a data event while the HTTP status remains successful. Inspect the documented failure events for the chosen API. A network response marked OK therefore cannot be the only success criterion. Preserve partial output as partial when a later event reports failure.

## Test framing and lifecycle separately

Use fixtures with a keepalive comment, split event data, ordinary deltas and a mid-stream failure. Confirm that comments never enter the displayed answer and that completion is recorded only for the appropriate event. These tests validate the client’s protocol handling without making provider latency claims. The resulting UI can show activity honestly while retaining enough event information to explain a failed or interrupted generation.

## Documentation and further study

- [API Streaming - Real-time Model Response Integration](https://openrouter.ai/docs/api-reference/streaming).

- [OpenRouter API Reference - Complete Documentation](https://openrouter.ai/docs/api-reference/overview).

- [Provider Routing - Smart Multi-Provider Request Management](https://openrouter.ai/docs/guides/routing/provider-selection).

Related engineering guides: [Reading an OpenRouter streaming response incrementally](/journal/openrouter-streaming-client/); [Errors inside an OpenRouter stream: why HTTP 200 is insufficient](/journal/openrouter-stream-errors/).
