Nyraxis AI

Django

Evaluate incoming LLM requests with a Django middleware class powered by Nyraxis.

Setup

pip install django nyraxis
export NYRAXIS_API_KEY="your-api-key"
export NYRAXIS_PROJECT_ID="your-project-id"

Add the middleware to your settings.py:

MIDDLEWARE = [
    # ... other middleware
    "myapp.middleware.NyraxisMiddleware",
]

Code Example

# myapp/middleware.py
import json
from django.http import JsonResponse
from nyraxis import Nyraxis

class NyraxisMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        self.nx = Nyraxis()

    def __call__(self, request):
        if request.method == "POST" and request.content_type == "application/json":
            try:
                body = json.loads(request.body)
                message = body.get("message", "")
                if message:
                    result = self.nx.evaluate(message)
                    if result.blocked:
                        return JsonResponse(
                            {"error": "Blocked by policy"}, status=403
                        )
            except json.JSONDecodeError:
                pass

        return self.get_response(request)

What Gets Protected

  • All POST requests with JSON bodies containing a message field are evaluated
  • Blocked requests return a 403 response before reaching any view
  • The middleware integrates naturally into Django's request/response cycle
  • Works alongside Django REST Framework and other middleware

On this page