Llm governance middleware foundation - #65514
Conversation
Introduce RequestContext and BlockedResponse for Ray Serve LLM governance middleware (issue ray-project#65259). Signed-off-by: Divyam19 <divyamgupta19@gmail.com>
Introduce LLMMiddleware ABC, MiddlewareChain orchestrator, and pytest coverage for before/after/complete hook behavior (issue ray-project#65259). Signed-off-by: Divyam19 <divyamgupta19@gmail.com>
Integrate governance middleware hooks into OpenAiIngress via GovernanceIngress, with RequestContext builders, BlockedResponse HTTP mapping, and streaming completion handling (issue ray-project#65259). Signed-off-by: Divyam19 <divyamgupta19@gmail.com>
Signed-off-by: Divyam19 <divyamgupta19@gmail.com>
Make after_inference optional, close upstream generators on block and disconnect, lazy-import GovernanceIngress, and cover request context extraction plus reference middleware. Signed-off-by: Divyam19 <divyamgupta19@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Export GovernanceIngress and LLMMiddleware from ray.serve.llm.governance, document ingress_cls_config wiring, and exercise the path through build_openai_app and the OpenAI HTTP API. Signed-off-by: Divyam19 <divyamgupta19@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a new governance middleware framework for Ray Serve LLM, enabling users to inspect, modify, or block OpenAI-compatible traffic via custom LLMMiddleware hooks on GovernanceIngress. Key additions include request context tracking, blocked response handling, reference middlewares for PII detection and budget enforcement, and comprehensive tests. Feedback on the changes highlights opportunities to improve robustness, specifically by supporting Pydantic v1 models in usage_to_dict and the non-streaming response path to prevent potential crashes or silent tracking failures, and extending extract_request_text to support dictionary-based requests consistently.
Signed-off-by: Divyam19 <divyamgupta19@gmail.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 7f8c0d7. Configure here.
Local agent working notes must not ship in the Ray tree. Signed-off-by: Divyam19 <divyamgupta19@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>

Description
Adds an alpha governance middleware layer for Ray Serve LLM so users can inspect or block OpenAI-compatible chat, completions, and transcription traffic without changing vLLM or SGLang.
GovernanceIngresssubclassesOpenAiIngressand runs an orderedLLMMiddlewarechain around_process_llm_request:before_inference— required. Return the request to proceed, or aBlockedResponseto stop before the model is called.after_inference— optional. Sees the full non-streaming response, or the first stream chunk.on_inference_complete— optional. Runs after the response is returned, including after a stream ends, with the last usage dict.Users wire it through the existing
ingress_cls_config/ingress_extra_kwargsonbuild_openai_app. The public API isray.serve.llm.governance(GovernanceIngress,LLMMiddleware,RequestContext,BlockedResponse).BlockedResponsemaps to OpenAI-style JSON errors:PII_DETECTED→ 400,BUDGET_EXCEEDED→ 402,ACCESS_DENIED→ 403,THROTTLED→ 429 withRetry-After.This is the Ray-side hook interface for #65259. Concrete engines (TealTiger, AgentShield, custom policies) live in separate packages and subclass
LLMMiddleware.Out of scope for this PR: stream buffering / redaction, per-model middleware on
LLMConfig, embeddings/scoring hooks, and direct streaming (RAY_SERVE_LLM_ENABLE_DIRECT_STREAMINGalready rejects a custom ingress).Related issues
Related to #65259.
Additional information
Usage
Docs:
doc/source/serve/llm/user-guides/governance.md.Tests
Coverage: middleware chain, request-context extraction, reference PII/budget middleware, mocked-handle ingress (block before LLM, 429, stream completion, aclose on block/disconnect),
build_openai_appwiring, and HTTP e2e throughserve.run+httpx+MockVLLMEngine.User Guide
User guide:
doc/source/serve/llm/user-guides/governance.md(also linked from the Serve LLM user-guides index and the API reference).