This release is a pre-release and may not be stable for production use.
Swarmauri Middleware LlamaGuard
A FastAPI middleware that wraps Groq's llama-guard-3-8b model to provide end-to-end content inspection for both inbound requests and outbound responses. The middleware is designed to slot into any FastAPI application and enforce safety policies before your handlers are invoked.
Features
- Real-time scanning of incoming request bodies and outgoing responses (including streaming responses).
- Configurable language model injection ? provide your own :class:
~swarmauri_standard.llms.GroqModelor let the middleware create one for you. - Graceful degradation when no model is configured (traffic is allowed but logged).
Middleware behavior
LlamaGuardMiddleware inspects content by default with Groq's llama-guard-3-8b model. Provide an API key via the api_key argument or the GROQ_API_KEY environment variable to enable enforcement. When no model is available the middleware logs a warning and treats all content as safe so that applications can continue to function while you configure credentials.
Both JSON responses and streaming responses are inspected. Unsafe content results in an HTTP 400 response with a descriptive error payload.
Installation
Choose the workflow that matches your project tooling:
-
pip
pip install swarmauri_middleware_llamaguard
-
poetry
poetry add swarmauri_middleware_llamaguard
-
uv
uv add swarmauri_middleware_llamaguard
Quickstart
- Configure your Groq API key (either export
GROQ_API_KEYor passapi_keywhen constructing the middleware). - Attach the middleware to your FastAPI application:
from fastapi import FastAPI, Request
from swarmauri_middleware_llamaguard import LlamaGuardMiddleware
app = FastAPI()
middleware = LlamaGuardMiddleware() # Uses GROQ_API_KEY from the environment
@app.middleware("http")
async def llama_guard(request: Request, call_next):
return await middleware.dispatch(request, call_next)
@app.get("/health")
async def health_check():
return {"status": "ok"}
The middleware will block requests or responses when llama-guard-3-8b labels the payload as unsafe.
Example: Local safety checks without Groq
The middleware also accepts a custom language model implementation. The following self-contained example demonstrates how to supply a stub model for local development or tests while still benefiting from end-to-end request inspection.
# README Example: Basic request filtering
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from swarmauri_middleware_llamaguard import LlamaGuardMiddleware
from swarmauri_standard.messages.AgentMessage import AgentMessage
class StubGuardModel:
def predict(self, conversation, *args, **kwargs):
latest = str(conversation.get_last().content).lower()
verdict = "unsafe" if "malicious" in latest else "safe"
conversation.add_message(AgentMessage(content=verdict))
app = FastAPI()
middleware = LlamaGuardMiddleware(llm=StubGuardModel())
@app.middleware("http")
async def llama_guard(request: Request, call_next):
return await middleware.dispatch(request, call_next)
@app.post("/echo")
def echo(payload: dict) -> dict:
return payload
with TestClient(app) as client:
assert client.post("/echo", json={"message": "hello"}).status_code == 200
assert (
client.post("/echo", json={"message": "malicious content"}).status_code
== 400
)
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Metadata
Release files for swarmauri_middleware_llamaguard 0.11.0.dev1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| swarmauri_middleware_llamaguard-0.11.0.dev1.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarmauri_middleware_llamaguard-0.11.0.dev1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.3 kB
Release files / swarmauri_middleware_llamaguard-0.11.0.dev1.tar.gz
| Download URL | swarmauri_middleware_llamaguard-0.11.0.dev1.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4ea670de97ba299686c634d1fce04e3eb3178ddea1d8c14da972b8f54f9075e4
|
|
BLAKE2b-256 checksum How to use checksums |
35d50c5fbb16fea9aa22de873bd564159da619bd505620e6b8d4bffaf5bf8a99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / swarmauri_middleware_llamaguard-0.11.0.dev1-py3-none-any.whl
| Download URL | swarmauri_middleware_llamaguard-0.11.0.dev1-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae50144e3b6ee269677ea117e9c1d69bc161956c8b175531626ae6987289a29f
|
|
BLAKE2b-256 checksum How to use checksums |
36e16dce59acd3f25b3ee91ae988548370c64fb098b11aa1e651bd97f59a01c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|