Flask
Gate an application-owned request and buffered model response in Flask.
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 FlaskSet NYRAXIS_API_KEY, NYRAXIS_BASE_URL and OPENAI_API_KEY on the server. Save as app.py and run flask --app app run --host 127.0.0.1 --port 8080 for local verification. Use a production WSGI server for deployment.
from flask import Flask, jsonify, request
from openai import OpenAI
from nyraxis_gate import guarded_text_call
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024
model = OpenAI()
def generate(prompt):
answer = model.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}])
return answer.choices[0].message.content or ""
@app.post("/chat")
def chat():
body = request.get_json(silent=True)
text = body.get("input") if isinstance(body, dict) else None
if not isinstance(text, str) or not text.strip() or len(text) > 4000:
return jsonify(error="Provide 1–4000 characters"), 400
try:
return jsonify(output=guarded_text_call(text, generate))
except PermissionError:
return jsonify(error="Policy blocked this request"), 403
except Exception:
return jsonify(error="Required check or model unavailable"), 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.