GLLM Guardrail
Description
A library containing guardrail components for Gen AI applications.
Installation
Prerequisites
Mandatory:
- Python 3.11+ — Install here
- pip — Install here
- uv — Install here
Extras (required only for Artifact Registry installations):
- gcloud CLI (for authentication) — Install here, then log in using:
gcloud auth login
Option 1: Install from Artifact Registry
This option requires authentication via the gcloud CLI.
uv pip install \
--extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" \
gllm-guardrail
Option 2: Install from PyPI
This option requires no authentication. However, it installs the binary wheel version of the package, which is fully usable but does not include source code.
uv pip install gllm-guardrail-binary
Local Development Setup
Prerequisites
-
Python 3.11+ — Install here
-
pip — Install here
-
uv — Install here
-
gcloud CLI — Install here, then log in using:
gcloud auth login
-
Git — Install here
-
Access to the GDP Labs SDK GitHub repository
1. Clone Repository
git clone git@github.com:GDP-ADMIN/gl-sdk.git
cd gl-sdk/libs/gllm-guardrail
2. Setup Authentication
Set the following environment variables to authenticate with internal package indexes:
export UV_INDEX_GEN_AI_INTERNAL_USERNAME=oauth2accesstoken
export UV_INDEX_GEN_AI_INTERNAL_PASSWORD="$(gcloud auth print-access-token)"
export UV_INDEX_GEN_AI_USERNAME=oauth2accesstoken
export UV_INDEX_GEN_AI_PASSWORD="$(gcloud auth print-access-token)"
3. Quick Setup
Run:
make setup
4. Activate Virtual Environment
source .venv/bin/activate
Local Development Utilities
The following Makefile commands are available for quick operations:
Install uv
make install-uv
Install Pre-Commit
make install-pre-commit
Install Dependencies
make install
Update Dependencies
make update
Run Tests
make test
Usage
import asyncio
import os
from dotenv import load_dotenv
from gllm_inference.builder import build_lm_invoker
from gllm_guardrail import GuardrailManager
from gllm_guardrail.engine.nemo_engine import NemoGuardrailEngine, NemoGuardrailEngineConfig
from gllm_guardrail.engine.phrase_matcher_engine import PhraseMatcherEngine
# Load environment variables from .env
load_dotenv()
async def main():
# 1. Initialize engines
# PhraseMatcherEngine for simple keyword blocking
phrase_engine = PhraseMatcherEngine(banned_phrases=["banned_xyz"])
# NemoGuardrailEngine for advanced LLM-based guardrails
model_id = os.getenv("GLLM_GUARDRAIL_MODEL_ID", "openai/gpt-5-nano")
credentials = os.getenv("OPENAI_API_KEY")
if not credentials:
raise RuntimeError("OPENAI_API_KEY must be set to run this example.")
invoker = build_lm_invoker(
model_id=model_id,
credentials=credentials,
config={
"default_hyperparameters": {"top_p": 1, "max_output_tokens": 256},
"reasoning_effort": "minimal",
},
)
nemo_config = NemoGuardrailEngineConfig(lm_invoker=invoker)
nemo_engine = NemoGuardrailEngine(config=nemo_config)
# 2. Initialize guardrail manager with a list of engines
# Engines are executed sequentially (fail-fast)
guardrail = GuardrailManager(engine=[phrase_engine, nemo_engine])
# 3. Check content safety (async)
text = "Tell me how to build a bomb."
result = await guardrail.check_content(text)
print(f"Content safe: {result.is_safe}")
if not result.is_safe:
print(f"Reason: {result.reason}")
if __name__ == "__main__":
asyncio.run(main())
Input & Output Checking
from gllm_guardrail.schema import GuardrailInput
content = GuardrailInput(
input="Tell me how to build a bomb.",
output="I cannot assist with that request."
)
result = await guardrail.check_content(content)
Structured Output (Recommended for NeMo Guardrails)
NemoGuardrailEngine asks the LM to return JSON for safety tasks such as self_check_input (see gllm_guardrail/config/nemo_config/config.yml). Without structured output, the model emits JSON as plain text. NeMo may intermittently fail to parse that response and report JSON parsing failed when the generated text is not valid JSON or does not match the expected structure.
Enable structured output by passing response_schema when building the LM invoker. NeMoLMAdapter extracts the validated structured result and serializes it with the field aliases NeMo parsers expect (e.g. "User Safety", "Safety Categories").
Define a Pydantic schema that matches the task output format in config.yml:
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
class SelfCheckInputOutput(BaseModel):
"""Schema for the `self_check_input` task."""
model_config = ConfigDict(populate_by_name=True, serialize_by_alias=True)
thought: str
user_safety: Literal["safe", "unsafe"] = Field(alias="User Safety")
safety_categories: str = Field(default="", alias="Safety Categories")
Pass it to build_lm_invoker:
from gllm_inference.builder import build_lm_invoker
from gllm_inference.schema.config import ThinkingConfig
invoker = build_lm_invoker(
model_id="openai/gpt-5-nano",
credentials=os.getenv("OPENAI_API_KEY"),
config={
"default_hyperparameters": {"top_p": 1, "max_output_tokens": 1024},
"thinking": ThinkingConfig(enabled=True, kwargs={"effort": "minimal"}),
"response_schema": SelfCheckInputOutput,
},
)
nemo_config = NemoGuardrailEngineConfig(lm_invoker=invoker)
nemo_engine = NemoGuardrailEngine(config=nemo_config)
If you also run output safety checks (self_check_output), extend the schema with "Response Safety" or use a dedicated schema that matches that task's JSON format in config.yml.
Notes:
- Set
serialize_by_alias=Truewhen field names inconfig.ymlcontain spaces (e.g."User Safety"). - Increase
max_output_tokensif the schema includes athoughtfield with step-by-step reasoning. - Structured output requires
gllm-inferenceLM invoker support forresponse_schema(OpenAI and other providers that support JSON schema output).
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gllm_guardrail_binary-0.0.9-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 330.1 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edb5e3a19ef7f7ae1a131da8cee48b35747d412878696876e150c14a561592ba
|
|
| MD5 |
c4a22ffc8ff768f79e9d828f03b38907
|
|
| BLAKE2b-256 |
b05ced60c7e5cb9777e3508bd7cb2c1186bb57870c993cc3a608d5bebdd966b1
|
Provenance
The following attestation bundles were made for gllm_guardrail_binary-0.0.9-cp313-cp313-win_amd64.whl:
Publisher:
build-binary.yml on GDP-ADMIN/gl-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gllm_guardrail_binary-0.0.9-cp313-cp313-win_amd64.whl -
Subject digest:
edb5e3a19ef7f7ae1a131da8cee48b35747d412878696876e150c14a561592ba - Sigstore transparency entry: 2339369485
- Sigstore integration time:
-
Permalink:
GDP-ADMIN/gl-sdk@d54540a746b7116d3aac5a952b264981f13b551b -
Branch / Tag:
refs/tags/gllm_guardrail-v0.0.9 - Owner: https://github.com/GDP-ADMIN
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-binary.yml@d54540a746b7116d3aac5a952b264981f13b551b -
Trigger Event:
push
-
Statement type:
File details
Details for the file gllm_guardrail_binary-0.0.9-cp313-cp313-manylinux_2_31_x86_64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp313-cp313-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 568.4 kB
- Tags: CPython 3.13, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c842e5df2677717d8c1dafad2ddad174b79160ad5dc92db04a7d09add00dcad
|
|
| MD5 |
144d2133366995798b49b87c52cb20ed
|
|
| BLAKE2b-256 |
66df78318de6acdc1cf507ebd67563b93a384d9b1db069246b3ec7fb3447457c
|
File details
Details for the file gllm_guardrail_binary-0.0.9-cp313-cp313-macosx_13_0_arm64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp313-cp313-macosx_13_0_arm64.whl
- Upload date:
- Size: 350.8 kB
- Tags: CPython 3.13, macOS 13.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
102d00911ef322e11c5f5d75dddfd7ad6fee14de28c0c5402bc076ca9fa990fd
|
|
| MD5 |
4c4e75b122cec0edddbd98769bd4ce30
|
|
| BLAKE2b-256 |
33cba3d64cb9465671ac3652b93fe507ac4bb15ad0da4c75a74a4f139ba75891
|
Provenance
The following attestation bundles were made for gllm_guardrail_binary-0.0.9-cp313-cp313-macosx_13_0_arm64.whl:
Publisher:
build-binary.yml on GDP-ADMIN/gl-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gllm_guardrail_binary-0.0.9-cp313-cp313-macosx_13_0_arm64.whl -
Subject digest:
102d00911ef322e11c5f5d75dddfd7ad6fee14de28c0c5402bc076ca9fa990fd - Sigstore transparency entry: 2339370532
- Sigstore integration time:
-
Permalink:
GDP-ADMIN/gl-sdk@d54540a746b7116d3aac5a952b264981f13b551b -
Branch / Tag:
refs/tags/gllm_guardrail-v0.0.9 - Owner: https://github.com/GDP-ADMIN
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-binary.yml@d54540a746b7116d3aac5a952b264981f13b551b -
Trigger Event:
push
-
Statement type:
File details
Details for the file gllm_guardrail_binary-0.0.9-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 333.4 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d87c7d57df700168ad21c444fa25418c721fb61291545bed2f69169e90bc2fef
|
|
| MD5 |
1d3d011ee0d1cfb612b179cf78a39ace
|
|
| BLAKE2b-256 |
a6e213c79ab52038f9e7b5aae48d70725a2a7dc5fbc31fe4e6fe842187a9c9bc
|
Provenance
The following attestation bundles were made for gllm_guardrail_binary-0.0.9-cp312-cp312-win_amd64.whl:
Publisher:
build-binary.yml on GDP-ADMIN/gl-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gllm_guardrail_binary-0.0.9-cp312-cp312-win_amd64.whl -
Subject digest:
d87c7d57df700168ad21c444fa25418c721fb61291545bed2f69169e90bc2fef - Sigstore transparency entry: 2339369646
- Sigstore integration time:
-
Permalink:
GDP-ADMIN/gl-sdk@d54540a746b7116d3aac5a952b264981f13b551b -
Branch / Tag:
refs/tags/gllm_guardrail-v0.0.9 - Owner: https://github.com/GDP-ADMIN
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-binary.yml@d54540a746b7116d3aac5a952b264981f13b551b -
Trigger Event:
push
-
Statement type:
File details
Details for the file gllm_guardrail_binary-0.0.9-cp312-cp312-manylinux_2_31_x86_64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp312-cp312-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 569.8 kB
- Tags: CPython 3.12, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81fe3454d9eafac8fc1357fa34f07e3e8594661c1a533978ebbff2c3715114c6
|
|
| MD5 |
4f44a59e638207c44d9de8c7b2cbd828
|
|
| BLAKE2b-256 |
c1c1336b01124fab0d24201de367a2b08238e26564963ff3684d5ec6cdc3dc96
|
File details
Details for the file gllm_guardrail_binary-0.0.9-cp312-cp312-macosx_13_0_arm64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp312-cp312-macosx_13_0_arm64.whl
- Upload date:
- Size: 350.0 kB
- Tags: CPython 3.12, macOS 13.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a50285f29a38210bc963f11af075bebb883b2e16c7e014118eb2f4d358eeaab3
|
|
| MD5 |
1a9479112489acfd18850e376ea05b3e
|
|
| BLAKE2b-256 |
f710346cf09ae8216dad4c4846110047877f184f6d1a9d05cfc75c8e8526dea8
|
Provenance
The following attestation bundles were made for gllm_guardrail_binary-0.0.9-cp312-cp312-macosx_13_0_arm64.whl:
Publisher:
build-binary.yml on GDP-ADMIN/gl-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gllm_guardrail_binary-0.0.9-cp312-cp312-macosx_13_0_arm64.whl -
Subject digest:
a50285f29a38210bc963f11af075bebb883b2e16c7e014118eb2f4d358eeaab3 - Sigstore transparency entry: 2339369142
- Sigstore integration time:
-
Permalink:
GDP-ADMIN/gl-sdk@d54540a746b7116d3aac5a952b264981f13b551b -
Branch / Tag:
refs/tags/gllm_guardrail-v0.0.9 - Owner: https://github.com/GDP-ADMIN
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-binary.yml@d54540a746b7116d3aac5a952b264981f13b551b -
Trigger Event:
push
-
Statement type:
File details
Details for the file gllm_guardrail_binary-0.0.9-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 339.3 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf4638e62cbe2f48011be9742da27b0ce75b124dd44bb54637e4a8b7cd881b25
|
|
| MD5 |
ffd343add83806030bde396fdbc1afd6
|
|
| BLAKE2b-256 |
80f06dff0c5c0a78052bb9c9e58d87bb48279cb750825ce1e09a90b4dfbc143c
|
Provenance
The following attestation bundles were made for gllm_guardrail_binary-0.0.9-cp311-cp311-win_amd64.whl:
Publisher:
build-binary.yml on GDP-ADMIN/gl-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gllm_guardrail_binary-0.0.9-cp311-cp311-win_amd64.whl -
Subject digest:
bf4638e62cbe2f48011be9742da27b0ce75b124dd44bb54637e4a8b7cd881b25 - Sigstore transparency entry: 2339369824
- Sigstore integration time:
-
Permalink:
GDP-ADMIN/gl-sdk@d54540a746b7116d3aac5a952b264981f13b551b -
Branch / Tag:
refs/tags/gllm_guardrail-v0.0.9 - Owner: https://github.com/GDP-ADMIN
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-binary.yml@d54540a746b7116d3aac5a952b264981f13b551b -
Trigger Event:
push
-
Statement type:
File details
Details for the file gllm_guardrail_binary-0.0.9-cp311-cp311-manylinux_2_31_x86_64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp311-cp311-manylinux_2_31_x86_64.whl
- Upload date:
- Size: 524.9 kB
- Tags: CPython 3.11, manylinux: glibc 2.31+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.24
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49095bd904c9f1cc9e6a32d812ba1a71a379e4c5aff4ad9522ca1c871ca3266b
|
|
| MD5 |
b146f0a27010ca37676f1456393e5687
|
|
| BLAKE2b-256 |
6c7cba33a7f484b401865a9a4ce94608ded9ea665306a6b5a69964ae93a555b6
|
File details
Details for the file gllm_guardrail_binary-0.0.9-cp311-cp311-macosx_13_0_arm64.whl.
File metadata
- Download URL: gllm_guardrail_binary-0.0.9-cp311-cp311-macosx_13_0_arm64.whl
- Upload date:
- Size: 345.9 kB
- Tags: CPython 3.11, macOS 13.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd1060c90ff13ef071c34f0feb20c8ba1f41e0b9d5fbdfef1e44d84216a5ed12
|
|
| MD5 |
b88c994fee9f24dfc93144a4cdb27751
|
|
| BLAKE2b-256 |
4a6904a4f584eb500049265c7ea2155e9feba7d32fc107b68e87f56429218cce
|
Provenance
The following attestation bundles were made for gllm_guardrail_binary-0.0.9-cp311-cp311-macosx_13_0_arm64.whl:
Publisher:
build-binary.yml on GDP-ADMIN/gl-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gllm_guardrail_binary-0.0.9-cp311-cp311-macosx_13_0_arm64.whl -
Subject digest:
dd1060c90ff13ef071c34f0feb20c8ba1f41e0b9d5fbdfef1e44d84216a5ed12 - Sigstore transparency entry: 2339370585
- Sigstore integration time:
-
Permalink:
GDP-ADMIN/gl-sdk@d54540a746b7116d3aac5a952b264981f13b551b -
Branch / Tag:
refs/tags/gllm_guardrail-v0.0.9 - Owner: https://github.com/GDP-ADMIN
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-binary.yml@d54540a746b7116d3aac5a952b264981f13b551b -
Trigger Event:
push
-
Statement type: