Why can a free LLM API stream show text before content_filter?
When text appears in a streaming answer and the stream later ends with finish_reason: content_filter, the provider may have delivered text before its moderation result arrived. Microsoft Foundry documents this behavior for Asynchronous Filter and notes a separate last-segment edge case in Default streaming. Treat the text as provisional when the selected route documents delayed filtering; buffer until its moderation watermark advances, or redact the flagged range when its annotation arrives. This is specific to providers that expose these fields, not a promise about every free API.
Check the provider route and filter mode first
“OpenAI compatible” describes an API shape, not one shared moderation policy. Record the exact host, API family, model or deployment, API version, SDK, and any gateway in between. A proxy may omit annotation-only events or translate the final reason. Do not infer a filter mode from a model name or a generic SDK setting.
For Microsoft Foundry Chat Completions, the current documentation describes two streaming modes. Default filtering buffers output chunks for evaluation before returning them. Asynchronous Filter sends generated tokens without waiting for those moderation results, then returns annotations and policy signals later. The asynchronous mode trades lower latency for a period when text is visible before its filter result is known.
The documentation also records a narrower Default-mode edge case: a final segment can arrive before filtering is applied in one tested custom-blocklist configuration. The same term was blocked in a non-streamed request and when more text followed in streaming. Treat that as a documented example for that deployment, not proof that every Default stream leaks text.
Read annotation events separately from text events
A text delta and a filter annotation are different kinds of stream event. In Microsoft's example, an annotation event carries no new text; its offsets refer back to text sent earlier. An SDK that only concatenates delta.content can discard the moderation signal while leaving the text on screen.
| Field or event | What it can tell you | What it cannot establish alone |
|---|---|---|
delta.content |
A new text fragment arrived. | That the fragment has already been moderated in an asynchronous route. |
content_filter_results |
A provider-reported moderation result, when supplied for that deployment and API version. | That every compatible provider sends the field, or that a missing field means safe. |
content_filter_offsets.check_offset |
The documented character boundary through which text has been checked; it does not decrease in the Azure example. | A token count, or a portable offset format for every gateway and programming language. |
start_offset / end_offset |
The text range associated with an annotation. | A safe reason to slice the displayed string until you have confirmed the provider's offset convention and your runtime's character indexing. |
Final finish_reason: content_filter |
The choice ended because filtering stopped it, under the documented Chat Completions contract. | Whether an earlier delta was already rendered, or why a compatible proxy might have changed the upstream result. |
[DONE] |
The SSE stream's terminal marker in the OpenAI example. | That a turn whose final choice says content_filter is an ordinary complete answer. |
A completed HTTP connection and a completed answer are separate facts. Preserve the HTTP status, every SSE frame, the final choice reason, and any annotation frames. Redact credentials and private prompts before sharing a trace.
Choose a display policy that matches the contract
If users must never see unreviewed text, do not render every token immediately. Keep incoming text in a pending buffer, track the provider's moderation watermark, and release only the prefix the documented contract says has been checked. Apply annotation ranges using the provider's own offset convention; do not treat character offsets as token counts or pass them blindly to a language runtime whose string indices use different units.
If the product streams text immediately for latency, visibly treat the answer as provisional. When a filtered annotation arrives, stop the stream, remove or replace the flagged range, and tell the user the provider stopped the answer. Avoid saving the provisional version as a completed assistant turn. A terminal content_filter with no category details is still a filter outcome; missing optional metadata should remain unknown.
For a route whose filtering fields or mode are undocumented, do not apply Azure's offset rules to it. If the application cannot tolerate an unreviewed segment and the provider offers no usable annotation contract, use a non-streamed request or an independently documented moderation gate. Confirm what the exact upstream and gateway actually return before choosing a fallback.
Keep four states instead of one “done” flag
A common integration mistake is to let one done boolean stand for the socket, generation, filtering, and visible answer. Those can end at different times. Keep the raw transport state separate from the user-facing turn state:
| State | Values to retain | Transition example |
|---|---|---|
| Transport | open, ended, interrupted |
[DONE] closes the OpenAI-style stream; a dropped connection without a final event is interrupted. |
| Generation | running, stopped, filtered, unknown |
The choice's final finish_reason updates this field; it does not rewrite earlier deltas. |
| Moderation | unknown, pending, checked-through, flagged |
Annotation frames advance a verified boundary or mark a range. |
| Display | held, provisional, released, redacted |
The product decides when pending text reaches the screen and whether a flagged span is removed. |
Apply each event to the matching state. A text delta appends to the current choice's pending buffer. An annotation-only event updates moderation even though it contributes no text. Move the checked boundary forward only after validating the provider's offset basis; keep it monotonic and store flagged ranges alongside it. For a strict display policy, publish only the mapped prefix the route documents as checked. For a provisional policy, remember exactly what was rendered so a later annotation can retract that range from both the screen and the saved turn.
When a final choice says content_filter, change generation to filtered immediately and stop accepting new answer text. Do not wait for [DONE] before showing the filter state. When [DONE] arrives, close transport, but do not turn the filtered answer into a normal successful completion. If the socket ends before a final choice, mark the generation reason unknown or interrupted. Neither a closed socket nor an HTTP 200 fills in a missing moderation result.
This separation makes the offline fixture testable. A passing stream must end with the expected terminal choice and leave no pending text. A delayed flagged stream must either never display its unreviewed suffix or visibly retract the reported range; it must finish as filtered-partial. A fixture with no annotation must leave moderation unknown. A fixture with no final choice must remain interrupted. If a gateway changes any of these event orders or fields, the test fails before the UI calls the answer complete.
Walk through a synthetic stream locally
This handwritten example is an acceptance fixture, not a captured provider response or a live safety test:
1. delta.content = "The sample begins with neutral text. "
2. delta.content = "A later span remains pending."
3. annotation = { check_offset: 28, start_offset: 28, end_offset: 47,
content_filter_results: { sample_category: { filtered: true } } }
4. final choice = { finish_reason: "content_filter", delta: {} }
5. data: [DONE]
Run the same fixture through two renderers. An immediate renderer exposes all text at step 1 and must retract the flagged span at step 3. A gated renderer retains the suffix past its checked boundary, releases only the confirmed prefix, and ends in a filtered-partial state at step 4. The sample offsets are invented for a test; a real implementation must use the exact provider's reported units and fields.
A useful offline acceptance checklist is:
- Preserve annotation-only events even when they contain no content delta.
- Advance the checked boundary monotonically; do not move it backwards when frames repeat.
- Keep pending text hidden when the task requires checked-only display.
- When a flagged range arrives, redact it from visible text and stored turn state where the product can do so.
- Mark
content_filteras filtered or partial, never as a normal completed answer. - If the signal or annotation is absent, record “unknown”; do not convert absence into “safe”.
- Do not retry the identical prompt automatically. Ask the user to revise a legitimate request under the provider's rules instead.
This fixture uses no key, model call, real prompt, policy evasion, or provider benchmark. It checks the client state machine only.
Questions developers ask
Does finish_reason: content_filter mean the HTTP request failed?
Not necessarily. Read the route's status and completion fields together. Microsoft documents a streamed output-filter outcome with HTTP 200 and content_filter for Azure; prompt-filter rejection can follow a different status path. Other providers may map these cases differently.
Does a missing content_filter_results field prove the text passed?
No. Microsoft's troubleshooting guidance lists API-version and deployment-configuration reasons annotation results may not appear. For an unfamiliar compatible route, missing metadata means the moderation result is unconfirmed, not that it passed.
If the final reason is stop, is every streamed character cleared?
Do not assume that. Microsoft documents a Default-mode custom-blocklist example where the final segment arrived before filtering and ended with stop. Verify the exact deployment behavior; use non-streamed output or a stricter app-side check if that edge matters to your interface.
Can I use these offsets with any free LLM API?
No. The offsets and mode described here are from Microsoft's documented Azure Chat Completions stream. An OpenAI-shaped endpoint may omit them, rename them, or pass through different upstream events. Confirm the contract at the actual host before parsing.
Sources checked 12 November 2026
- Microsoft Foundry: Content streaming — default and asynchronous filtering, annotations, offsets, terminal filtering and documented edge cases; page updated 15 October 2026.
- OpenAI Chat Completions API reference —
finish_reasonvalues and the streamed final-choice/[DONE]shape. - The example event sequence and UI acceptance checks above are original synthetic material; they are not provider output.
The site’s existing Chat Completions field guide covers response-field meanings. Its incomplete-output guide covers SSE completion and missing endings. This page addresses a different question: what to do when a provider's moderation annotation arrives after some stream text.