> This page is for version Latest (default).
> For other versions, use one of these documentation indexes:
> - Latest (default): https://docs.nvidia.com/nemo/guardrails/latest/llms.txt
> - v0.25.0: https://docs.nvidia.com/nemo/guardrails/v0.25.0/llms.txt
> - v0.24.1: https://docs.nvidia.com/nemo/guardrails/v0.24.1/llms.txt
> - v0.24.0: https://docs.nvidia.com/nemo/guardrails/v0.24.0/llms.txt
> - v0.23.0: https://docs.nvidia.com/nemo/guardrails/v0.23.0/llms.txt
> - v0.22.0: https://docs.nvidia.com/nemo/guardrails/v0.22.0/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.

# nemoguardrails.server.experimental.providers.openai.chat_completions.stream_hooks

Bind OpenAI Chat Completions protocol hooks to the staged classifier.

## Module Contents

### Classes

| Name                                                                                                                                          | Description                                      |
| --------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| [`ChatCompletionsStreamHooks`](#nemoguardrails-server-experimental-providers-openai-chat_completions-stream_hooks-ChatCompletionsStreamHooks) | Require successful streams to end with `[DONE]`. |

### API

```python
class nemoguardrails.server.experimental.providers.openai.chat_completions.stream_hooks.ChatCompletionsStreamHooks()
```

Require successful streams to end with `[DONE]`.

A provider error may end the stream without a sentinel. Data-less keepalives
are permitted throughout; payload events cannot follow an error or sentinel,
except for a sentinel after an error. State belongs to one upstream stream.

```python
nemoguardrails.server.experimental.providers.openai.chat_completions.stream_hooks.ChatCompletionsStreamHooks.encode_error(
    rendered_body: bytes
) -> tuple[bytes, bytes]
```

Encode an OpenAI-compatible error and terminal chat event.

```python
nemoguardrails.server.experimental.providers.openai.chat_completions.stream_hooks.ChatCompletionsStreamHooks.observe_event(
    event: nemoguardrails.server.experimental.provider.sse.ServerSentEvent,
    classified: nemoguardrails.server.experimental.provider.stream.GuardedStreamEvent
) -> None
```

Track the actual sentinel and reject payloads after termination.

```python
nemoguardrails.server.experimental.providers.openai.chat_completions.stream_hooks.ChatCompletionsStreamHooks.validate_end_of_stream() -> None
```

Reject successful streams that end before their sentinel.