Skip to main content

KAITO Guardrail Core

kaito-guardrail-core provides KAITO-owned scanners and standard-library-only streaming utilities. guardrail_core.streaming.sse and guardrail_core.streaming.openai frame SSE events and parse/build OpenAI chat chunks; they do not scan or enforce policy.

guardrail_core.streaming.window provides a policy-independent text window for scan-before-release buffering.

Installation and development

Install the published package:

python -m pip install kaito-guardrail-core

For local development:

python -m pip install -e ./guardrail_core
python -m pip install build==1.2.2.post1 twine==6.1.0 pytest==9.1.1
python -m build ./guardrail_core
python -m twine check --strict guardrail_core/dist/*
python -m pytest guardrail_core/tests guardrail_core/guardrail_core

CI tests the installed wheel outside the source tree in a fresh environment on Python 3.12 through 3.14. The distribution name is kaito-guardrail-core; Python imports use guardrail_core.

Streaming window

guardrail_core.streaming.window retains output tails to detect cross-chunk matches, releasing only scanner-approved prefixes.

holdback_len is measured in Unicode code points and must be sufficient for the scanner's cross-chunk matching requirements.

flowchart TD
    Feed["feed(text)"] --> Buffer["Buffer text"]
    Buffer -->|Exceeds holdback| Scan["Scan entire buffer"]
    Buffer -->|Within holdback| Hold["Retain tail"]
    Scan -->|Allow / redact| Emit["Release approved prefix; retain tail"]
    Scan -->|Block| Block["Discard buffer; stop output"]
    Scan -->|Error| Error["Discard buffer; raise WindowScanError"]
    EOF["Normal EOF: flush()"] --> Final["Scan remaining text"]
    Final -->|Allow / redact| Release["Release remaining text"]
    Final -->|Block| Block
    Final -->|Error| Error
    Fail["Parse failure / cancellation"] --> Discard["Discard window; do not flush"]

Example (prints hello world; replace print with downstream emission):

from guardrail_core.streaming.window import (
    StreamingBufferWindow,
    WindowScanError,
    WindowScanResult,
)


class Scanner:
    def scan(
        self, text: str, *, flush: bool = False, preceding_char: str = ""
    ) -> WindowScanResult:
        return WindowScanResult(blocked="secret" in text)


window = StreamingBufferWindow(Scanner(), holdback_len=len("secret") - 1)
chunks = ["hello ", "world"]

try:
    for text in chunks:
        result = window.feed(text)
        print("".join(result.chunks), end="")
        if result.blocked:
            print("[blocked]")
            break
    else:
        result = window.flush()
        print("[blocked]" if result.blocked else "".join(result.chunks), end="")
except WindowScanError:
    print("[scan failed]")

Use chunks = ["safe se", "cret"] to exercise cross-chunk blocking. Parse errors and cancellation must propagate without entering the normal-EOF else branch.

Scanner is illustrative; replace it with your scanner adapter. The caller is responsible for closing the upstream stream and generating product-specific refusal responses.

Byte-oriented SSE framing

from guardrail_core.streaming.sse import SSEFramer
from guardrail_core.streaming.openai import parse_openai_chat_sse_event

framer = SSEFramer(max_event_bytes=1024 * 1024)
for chunk in [b'data: {"choices":[]}\r', b"\n\r", b"\n"]:
    for event in framer.feed(chunk):
        result = parse_openai_chat_sse_event(event)
        print(result.status, result.payload)
for event in framer.finish():
    print(event.data)

Call feed(bytes) in stream order and finish() exactly once at EOF. Both return lists of complete SSEEvent objects. LF, CRLF, CR, mixed endings, and arbitrary byte boundaries are supported. Complete events use strict UTF-8 decoding. EOF never supplies a missing event terminator.

  • event.raw_bytes: exact received bytes, including line endings and the terminating blank line, suitable for lossless forwarding.
  • event.raw: decoded event body, excluding its final line ending and blank line.
  • event.lines: body lines, including comments and non-data fields.
  • event.data: joined data fields, or None when no data fields exist.
  • SSEEvent.from_raw(text): parse an already decoded event body, without framing; its raw_bytes defaults to the UTF-8 encoding of that body.

Errors and resource bounds

SSEFramingError is a ValueError with these subclasses:

Exception Meaning
SSEDecodeError A complete event is not valid UTF-8; the original decode error is chained.
SSEIncompleteEventError EOF left an unterminated event, including an incomplete UTF-8 sequence.
SSEEventTooLargeError An event exceeded max_event_bytes (default 1 MiB).

max_event_bytes must be a positive integer and counts wire bytes, including delimiters, per event. Invalid limits and non-byte input raise ValueError/TypeError.

On failure, error.raw_bytes contains the failing event's buffered bytes (at most the limit); error.completed_events retains earlier complete events from the same call. Callers choose whether to forward them and how to handle failure. After a framing error or finish(), the framer is unusable; further calls raise ValueError.

OpenAI parsing contract

parse_openai_chat_sse_event(event) returns OpenAIChatChunkParseResult, retaining RAGEngine's status names and convenience fields:

Input OpenAIChatChunkParseStatus
No data field, empty data, or whitespace-only data NO_DATA
Data equal to [DONE] after stripping whitespace DONE
Invalid JSON MALFORMED_JSON, with error
Non-object payload, non-list choices, or non-object choice INVALID_PAYLOAD, with error
Missing/non-integer choice index (including bool) INVALID_PAYLOAD, with error
Missing/null delta PARSED, treating delta as an empty object
Non-object, non-null delta INVALID_PAYLOAD, with error
Missing/null content PARSED, no text
Non-string, non-null content or finish_reason INVALID_PAYLOAD, with error
Missing choices PARSED, treating choices as an empty list
Valid choices, including empty, role-only, and multi-choice chunks PARSED

All choices are retained in order, including role-only and tool-call-only chunks; indexes need not be zero or contiguous. Use parsed_choices for aligned content and finish reasons, not the filtered contents/finish_reasons lists. payload preserves the original fields, including invalid object payloads.

SSE builders

The functions in guardrail_core.streaming.openai return UTF-8-encodable strings with LF delimiters, matching RAGEngine:

  • build_openai_chat_delta_sse_chunk(content, *, choice_index=0)
  • build_openai_chat_finish_reason_sse_chunk(*, finish_reason="content_filter", choice_index=0)
  • build_sse_done_chunk()
  • build_sse_data_chunk(payload) for arbitrary JSON object fields and choices

Text builders create new minimal chunks; use the generic builder with a retained payload to preserve metadata, or forward event.raw_bytes for byte-exact replay.

Publishing

Build only guardrail_core/, using pyproject.toml for metadata and version. Wheels exclude tests; source distributions include tests and build inputs.

Publisher setup

Confirm maintainer access and configure:

  • A project or pending publisher on both TestPyPI and PyPI for kaito-guardrail-core.
  • A GitHub Trusted Publisher on each index: owner kaito-project, repository kaito, workflow filename release-guardrail-core.yaml, and the corresponding GitHub environment below. Publishing uses GitHub OIDC, not an API token.
  • GitHub environments guardrail-core-testpypi and guardrail-core-pypi, restricted to main. Require maintainer approval for guardrail-core-pypi before enabling production publishing.

Release workflow

After the intended package version merges into main, run the Release KAITO Guardrail Core workflow:

  1. Select main. Set release_version to the metadata version and release_commit to its full lowercase 40-character SHA on main; the package tag must not exist.
  2. Build and validate the source distribution and wheel; test the installed wheel outside the source checkout.
  3. Publish to TestPyPI and verify the downloaded wheel byte-for-byte. Install its dependencies separately from PyPI; do not mix indexes.
  4. Approve guardrail-core-pypi, then publish the same stored artifacts to PyPI without rebuilding.
  5. Create kaito-guardrail-core-v<version> and a GitHub Release for that source commit, attaching the artifacts. Do not mark it as the repository's latest release.

Recovery

For transient failures, rerun only failed jobs in the same run to reuse artifacts. Never overwrite an existing version or rebuild different content under it. Coordinate partial-upload recovery with maintainers; bump the version in a new PR before the next release.

Consumer adoption

Consumers must pin a version already published on production PyPI. Production images install requirements, not library source; editable installs are for development.

Metadata

Release files for kaito-guardrail-core 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kaito-guardrail-core 0.1.1
File Size Uploaded Provenance Actions
kaito_guardrail_core-0.1.1.tar.gz 36.4 kB With provenance Details for kaito_guardrail_core-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kaito-guardrail-core 0.1.1
File Interpreter ABI Platform Actions
kaito_guardrail_core-0.1.1-py3-none-any.whl Python 3 none any Details for kaito_guardrail_core-0.1.1-py3-none-any.whl

Total release size: 62.7 kB

Release files / kaito_guardrail_core-0.1.1.tar.gz

Download URL kaito_guardrail_core-0.1.1.tar.gz
Size 36.4 kB
Tags Source
SHA-256 checksum
How to use checksums
38ac28b84a7bdef0dcab201601c25c58659f5667d839703c6c29d5ea6d270d09
BLAKE2b-256 checksum
How to use checksums
3a58a3acb41917e0396cdf14c508a6b89c964fbb9b7e1b51050f7d1d5821abff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / kaito_guardrail_core-0.1.1-py3-none-any.whl

Download URL kaito_guardrail_core-0.1.1-py3-none-any.whl
Size 26.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1006a0d46960e7c1b782c33f634c9adb4ccac25c04cadeb47fc54fdedc15fee
BLAKE2b-256 checksum
How to use checksums
a425fc8cbcde21acf93953a3eebffc6c8ab47c470330cb21b54110e0a444bc15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for kaito-guardrail-core 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for kaito_guardrail_core-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page