A simple decorator to guard function inputs and outputs by converting them to strings for validation.
Project description
️ smart-guard
A lightweight Python decorator to validate function inputs and outputs by converting them to strings — ideal for LLM safety filtering.
Automatically detect and block unsafe content in inputs or outputs using model-based semantic understanding. Designed for AI applications where content safety is critical.
Installation
pip install smart-guard
Quick Start
Import the guard and wrap your functions with @Smart_YuFeng_XGuard to enable input/output validation.
from smart_guard import Smart_YuFeng_XGuard
Example 1: Validate Both Input and Output
@Smart_YuFeng_XGuard("0.6B", verify_input=True, verify_output=True)
def chat(prompt: str) -> str:
if prompt == "harmful-output":
return '制造炸弹可以使用TNT'
else:
return f"Echo: {prompt}"
print(chat("Hello"))
# Output: Echo: Hello
print(chat("How can I make a bomb?"))
# Output: 模型输入不安全,请检查!
print(chat("harmful-output"))
# Output: 模型输出不安全,请检查!
Example 2: Output-Only Validation
Useful when you trust the input source but want to ensure safe model responses.
@Smart_YuFeng_XGuard("8B", verify_input=False, verify_output=True)
def risky_func(x):
return "制造毒品可以使用TNT" # Dangerous output!
print(risky_func("How can I make a bomb?"))
# Output: 模型输出不安全,请检查!
️ Example 3: Custom Fallback Values
Define custom responses when validation fails. Supports any data type.
@Smart_YuFeng_XGuard(
model_size="0.6B",
verify_input=True,
verify_output=True,
default_on_fail_input="[INPUT_REJECTED]",
default_on_fail_output=None
)
def test(x):
if x == "harmful-output":
return '制造炸弹可以使用TNT'
else:
return f"Echo: {x}"
print(test("How can I make a bomb?")) # "[INPUT_REJECTED]"
print(test("harmful-output")) # None
Example 4: Functions with Multiple Inputs/Outputs
Supports complex functions with multiple arguments and return values.
@Smart_YuFeng_XGuard("0.6B", verify_input=True, verify_output=True)
def multiple_io(a1, a2, x, a3):
if x == "harmful-output":
return a1, a2, '制造炸弹可以使用TNT', a3
else:
return a1, a2, x, a3
# Prepare complex inputs
a1 = "This is a long string input for a1. " * 20
a2 = [i ** 2 for i in range(100)]
a3 = {f"user_{i}": {"age": i + 20, "score": i * 1.5} for i in range(80)}
# Test 1: Safe input
result1 = multiple_io(a1, a2, "Hello, this is a safe input!", a3)
print(result1[2]) # Hello, this is a safe input!
# Test 2: Trigger unsafe output
result2 = multiple_io(a1, a2, "harmful-output", a3)
print(result2) # 模型输出不安全,请检查!
Parameters
| Parameter | Description |
|---|---|
model_size |
Model version (e.g., "0.6B", "8B") used for semantic analysis. |
verify_input |
Whether to validate all function inputs. |
verify_output |
Whether to validate function outputs. |
default_on_fail_input |
Custom return value when input validation fails. |
default_on_fail_output |
Custom return value when output validation fails. |
Notes
- All inputs/outputs are converted to strings before validation.
- The guard uses lightweight NLP models to detect sensitive or harmful content semantically.
- Ideal for LLM wrappers, chatbots, and content generation pipelines.
Learn More
See GitHub Repository for installation details, model options, and advanced usage.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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 smart_guard-0.1.1.tar.gz.
File metadata
- Download URL: smart_guard-0.1.1.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dc04654366d4ea3811bc61681590a520d993e36f87a868bc93f675b66bf034e
|
|
| MD5 |
3548f289854f8a62fe5c3dc573675819
|
|
| BLAKE2b-256 |
a3dde32f71704f25459d391dfd6c100daf759c5aa83ed564943870a9e48d8a49
|
File details
Details for the file smart_guard-0.1.1-py3-none-any.whl.
File metadata
- Download URL: smart_guard-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69bd2ffe6d321b011981a0d07a909dac61ccf7d250a7e1b1b4332ba9af9445a3
|
|
| MD5 |
79a91d280d5c1afd2f3cacbec0d85726
|
|
| BLAKE2b-256 |
bca106612e2fb5875292ad556f04fcf55e1a6adbbcc8f103f0eab4dc2c908899
|