Nyraxis/ docs
Integrations
Developer documentation

Django

Gate an application-owned request and buffered model response in Django.

This is a server integration recipe. Complete the quickstart, configure a policy for docs-integration, and save nyraxis_gate.py in your application’s import path.

python -m pip install nyraxis-sdk==1.3.2 openai Django

Set NYRAXIS_API_KEY, NYRAXIS_BASE_URL and OPENAI_API_KEY on the server. Put this view in your Django application, map /chat to chat, and send requests with your application’s normal session and CSRF protections. Do not disable CSRF to make the example work. Configure upstream body limits as well as Django request limits.

import json
from django.http import JsonResponse
from django.views.decorators.http import require_POST
from openai import OpenAI
from nyraxis_gate import guarded_text_call

model = OpenAI()
def generate(prompt):
    result = model.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}])
    return result.choices[0].message.content or ""

@require_POST
def chat(request):
    if len(request.body) > 16 * 1024:
        return JsonResponse({"error": "Request too large"}, status=413)
    try:
        body = json.loads(request.body)
    except (ValueError, UnicodeDecodeError):
        return JsonResponse({"error": "Invalid JSON"}, status=400)
    text = body.get("input") if isinstance(body, dict) else None
    if not isinstance(text, str) or not text.strip() or len(text) > 4000:
        return JsonResponse({"error": "Provide 1–4000 characters"}, status=400)
    try:
        return JsonResponse({"output": guarded_text_call(text, generate)})
    except PermissionError:
        return JsonResponse({"error": "Policy blocked this request"}, status=403)
    except Exception:
        return JsonResponse({"error": "Required check or model unavailable"}, status=503)

Verify and deploy

The helper requires a complete applicable evaluation, checks input before the provider call, and checks buffered output before release. Test malformed requests, a safe request, synthetic Secrets detection input/output, and an unavailable Nyraxis API. Verify that blocked input makes no provider request.

Keep authentication, authorization, rate limits, upstream request limits and production timeouts in your application. Returning a public error must not log raw prompts or credentials. Review the framework documentation for deployment and data handling for processing/capture settings.

On this page