The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat: add Qohash Nexus guardrail hook (#24927)
Mathieu St-Vincent49ec6aba80d3
diff --git a/litellm/proxy/guardrails/guardrail_hooks/qohash/__init__.py b/litellm/proxy/guardrails/guardrail_hooks/qohash/__init__.pynew file mode 100644index 00000000000..465f52db3d0--- /dev/null+++ b/litellm/proxy/guardrails/guardrail_hooks/qohash/__init__.py@@ -0,0 +1,35 @@+from typing import TYPE_CHECKING++from litellm.types.guardrails import SupportedGuardrailIntegrations++from .qohash import QostodianNexus++if TYPE_CHECKING:+ from litellm.types.guardrails import Guardrail, LitellmParams+++def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"):+ import litellm++ _instance = QostodianNexus(+ api_base=litellm_params.api_base,+ guardrail_name=guardrail.get("guardrail_name", ""),+ event_hook=litellm_params.mode,+ default_on=litellm_params.default_on,+ additional_provider_specific_params=litellm_params.additional_provider_specific_params,+ extra_headers=getattr(litellm_params, "extra_headers", None),+ )++ litellm.logging_callback_manager.add_litellm_callback(_instance)++ return _instance+++guardrail_initializer_registry = {+ SupportedGuardrailIntegrations.QOSTODIAN_NEXUS.value: initialize_guardrail,+}+++guardrail_class_registry = {+ SupportedGuardrailIntegrations.QOSTODIAN_NEXUS.value: QostodianNexus,+}diff --git a/litellm/proxy/guardrails/guardrail_hooks/qohash/qohash.py b/litellm/proxy/guardrails/guardrail_hooks/qohash/qohash.pynew file mode 100644index 00000000000..7c856b676b7--- /dev/null+++ b/litellm/proxy/guardrails/guardrail_hooks/qohash/qohash.py@@ -0,0 +1,74 @@+"""+Qostodian Nexus (by Qohash) — LiteLLM guardrail integration.+"""+import os+from typing import TYPE_CHECKING, Literal, Optional, Type++from litellm.proxy.guardrails.guardrail_hooks.generic_guardrail_api.generic_guardrail_api import (+ GenericGuardrailAPI,+)+from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+)+from litellm.types.utils import GenericGuardrailAPIInputs++if TYPE_CHECKING:+ from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj++GUARDRAIL_NAME = "qostodian_nexus"+++class QostodianNexus(GenericGuardrailAPI):+ def __init__(+ self,+ api_base: Optional[str] = None,+ **kwargs,+ ):+ api_base = api_base or os.environ.get("QOSTODIAN_NEXUS_API_BASE", "http://nexus:8800")++ kwargs["guardrail_name"] = kwargs.get("guardrail_name", GUARDRAIL_NAME)++ # Merge built-in Qostodian Nexus identifier headers with any caller-supplied extra_headers+ nexus_headers = [+ "x-qostodian-nexus-identifiers-trace",+ "x-qostodian-nexus-identifiers-source",+ "x-qostodian-nexus-identifiers-container",+ "x-qostodian-nexus-identifiers-identity",+ ]++ existing = kwargs.get("extra_headers") or []+ kwargs["extra_headers"] = nexus_headers + [h for h in existing if h not in nexus_headers]++ super().__init__(+ api_base=api_base,+ **kwargs,+ )++ async def apply_guardrail(+ self,+ inputs: GenericGuardrailAPIInputs,+ request_data: dict,+ input_type: Literal["request", "response"],+ logging_obj: Optional["LiteLLMLoggingObj"] = None,+ ) -> GenericGuardrailAPIInputs:+ """+ Apply Qostodian Nexus to the given inputs.++ NOTE: This override is intentionally a pass-through. It must be present+ directly in this class's __dict__ so that LiteLLM's unified guardrail+ routing check (`"apply_guardrail" in type(callback).__dict__` in+ litellm/proxy/utils.py) routes calls correctly. Do not remove.+ """+ return await super().apply_guardrail(+ inputs=inputs,+ request_data=request_data,+ input_type=input_type,+ logging_obj=logging_obj,+ )++ @classmethod+ def get_config_model(cls) -> Optional[Type[QostodianNexusConfigModel]]:+ """+ Returns the config model for Qostodian Nexus.+ """+ return QostodianNexusConfigModeldiff --git a/litellm/types/guardrails.py b/litellm/types/guardrails.pyindex a98f9d666ae..04347aebe3b 100644--- a/litellm/types/guardrails.py+++ b/litellm/types/guardrails.py@@ -38,6 +38,9 @@ from litellm.types.proxy.guardrails.guardrail_hooks.tool_permission import (from litellm.types.proxy.guardrails.guardrail_hooks.hiddenlayer import (HiddenlayerGuardrailConfigModel,)+from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+)"""Pydantic object defining how to set guardrails on litellm proxy@@ -96,6 +99,7 @@ class SupportedGuardrailIntegrations(Enum):AKTO = "akto"MCP_JWT_SIGNER = "mcp_jwt_signer"LLM_AS_A_JUDGE = "llm_as_a_judge"+ QOSTODIAN_NEXUS = "qostodian_nexus"class Role(Enum):@@ -773,6 +777,7 @@ class LitellmParams(QualifireGuardrailConfigModel,BlockCodeExecutionGuardrailConfigModel,HiddenlayerGuardrailConfigModel,+ QostodianNexusConfigModel,):guardrail: str = Field(description="The type of guardrail integration to use")mode: Union[str, List[str], Mode] = Field(diff --git a/litellm/types/proxy/guardrails/guardrail_hooks/qohash.py b/litellm/types/proxy/guardrails/guardrail_hooks/qohash.pynew file mode 100644index 00000000000..5abfa69148e--- /dev/null+++ b/litellm/types/proxy/guardrails/guardrail_hooks/qohash.py@@ -0,0 +1,16 @@+from typing import Optional++from pydantic import Field++from .base import GuardrailConfigModel+++class QostodianNexusConfigModel(GuardrailConfigModel):+ api_base: Optional[str] = Field(+ default=None,+ description="The API base URL for Qostodian Nexus. If not provided, the `QOSTODIAN_NEXUS_API_BASE` environment variable is checked. Defaults to http://nexus:8800.",+ )++ @staticmethod+ def ui_friendly_name() -> str:+ return "Qostodian Nexus"diff --git a/tests/test_litellm/proxy/guardrails/test_qostodian_nexus_guardrail.py b/tests/test_litellm/proxy/guardrails/test_qostodian_nexus_guardrail.pynew file mode 100644index 00000000000..6daa3e1430d--- /dev/null+++ b/tests/test_litellm/proxy/guardrails/test_qostodian_nexus_guardrail.py@@ -0,0 +1,252 @@+"""+Unit tests for Qostodian Nexus (by Qohash) integration.++Tests verify:+1. QostodianNexus can be instantiated with default and custom values+2. Qostodian Nexus is registered in SupportedGuardrailIntegrations+3. Guardrail initializer and class registries contain Qostodian Nexus+4. Configuration parameters are properly passed through+5. QostodianNexusConfigModel works correctly+"""++import os+import pytest+from unittest.mock import MagicMock+++def test_qostodian_nexus_initialization_with_defaults():+ """Test QostodianNexus initializes with default values."""+ import os+ from unittest.mock import patch+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus++ # Unset env var so the hardcoded default is used+ env = {k: v for k, v in os.environ.items() if k != "QOSTODIAN_NEXUS_API_BASE"}+ with patch.dict(os.environ, env, clear=True):+ guardrail = QostodianNexus()++ # Should use default api_base+ assert guardrail.api_base is not None+ assert "nexus:8800" in guardrail.api_base+++def test_qostodian_nexus_initialization_with_custom_api_base():+ """Test QostodianNexus initializes with custom api_base."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus++ custom_api_base = "http://custom-nexus:9000"+ guardrail = QostodianNexus(api_base=custom_api_base)++ assert custom_api_base in guardrail.api_base+++def test_qostodian_nexus_in_supported_guardrail_integrations():+ """Test that Qostodian Nexus is registered in SupportedGuardrailIntegrations enum."""+ from litellm.types.guardrails import SupportedGuardrailIntegrations++ # Check enum contains QOSTODIAN_NEXUS+ assert hasattr(SupportedGuardrailIntegrations, "QOSTODIAN_NEXUS")+ assert SupportedGuardrailIntegrations.QOSTODIAN_NEXUS.value == "qostodian_nexus"++ # Check it's in the list of all values+ all_values = [e.value for e in SupportedGuardrailIntegrations]+ assert "qostodian_nexus" in all_values+++def test_qostodian_nexus_in_guardrail_initializer_registry():+ """Test that Qostodian Nexus is registered in guardrail_initializer_registry."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import (+ guardrail_initializer_registry,+ )++ assert "qostodian_nexus" in guardrail_initializer_registry+ assert callable(guardrail_initializer_registry["qostodian_nexus"])+++def test_qostodian_nexus_in_guardrail_class_registry():+ """Test that Qostodian Nexus is registered in guardrail_class_registry."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import (+ guardrail_class_registry,+ QostodianNexus,+ )++ assert "qostodian_nexus" in guardrail_class_registry+ assert guardrail_class_registry["qostodian_nexus"] == QostodianNexus+++def test_qostodian_nexus_config_model_initialization():+ """Test QostodianNexusConfigModel can be instantiated."""+ from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+ )++ config = QostodianNexusConfigModel(+ api_base="http://test:8800",+ )++ assert config.api_base == "http://test:8800"+++def test_qostodian_nexus_config_model_defaults():+ """Test QostodianNexusConfigModel uses correct defaults."""+ from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+ )++ config = QostodianNexusConfigModel()++ assert config.api_base is None+++def test_qostodian_nexus_config_model_ui_friendly_name():+ """Test QostodianNexusConfigModel returns correct UI friendly name."""+ from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+ )++ ui_name = QostodianNexusConfigModel.ui_friendly_name()+ assert ui_name == "Qostodian Nexus"+++def test_qostodian_nexus_initializer_function():+ """Test the initialize_guardrail function."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import initialize_guardrail+ from litellm.types.guardrails import LitellmParams, Guardrail+ from unittest.mock import patch++ # Mock litellm.logging_callback_manager+ with patch("litellm.logging_callback_manager") as mock_manager:+ mock_manager.add_litellm_callback = MagicMock()++ # Create test params+ litellm_params = LitellmParams(+ guardrail="qostodian_nexus",+ mode="pre_call",+ api_base="http://test:8800",+ default_on=True,+ )++ guardrail_config: Guardrail = {"guardrail_name": "test-qostodian-nexus"}++ # Call initializer+ result = initialize_guardrail(litellm_params, guardrail_config)++ # Verify callback was added+ mock_manager.add_litellm_callback.assert_called_once()++ # Verify returned instance has correct properties+ assert result is not None+ assert "test:8800" in result.api_base+++def test_qostodian_nexus_inherits_from_generic_guardrail_api():+ """Test that QostodianNexus inherits from GenericGuardrailAPI."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus+ from litellm.proxy.guardrails.guardrail_hooks.generic_guardrail_api.generic_guardrail_api import (+ GenericGuardrailAPI,+ )++ assert issubclass(QostodianNexus, GenericGuardrailAPI)+++def test_qostodian_nexus_guardrail_name_constant():+ """Test that GUARDRAIL_NAME constant is defined correctly."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash.qohash import GUARDRAIL_NAME++ assert GUARDRAIL_NAME == "qostodian_nexus"+++def test_qostodian_nexus_get_config_model():+ """Test that QostodianNexus returns the correct config model."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus+ from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+ )++ config_model = QostodianNexus.get_config_model()++ assert config_model is not None+ assert config_model == QostodianNexusConfigModel+++def test_qostodian_nexus_env_vars():+ """Test that QOSTODIAN_NEXUS_API_BASE env var is picked up correctly."""+ import os+ from unittest.mock import patch+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus++ with patch.dict(os.environ, {"QOSTODIAN_NEXUS_API_BASE": "http://new-api:8800"}):+ guardrail = QostodianNexus()+ assert "new-api:8800" in guardrail.api_base+++def test_qostodian_nexus_config_model_field_descriptions():+ """Test that QostodianNexusConfigModel has correct field descriptions."""+ from litellm.types.proxy.guardrails.guardrail_hooks.qohash import (+ QostodianNexusConfigModel,+ )++ # Check that field descriptions mention the correct env vars+ api_base_field = QostodianNexusConfigModel.model_fields["api_base"]+ assert "QOSTODIAN_NEXUS_API_BASE" in api_base_field.description+++def test_qostodian_nexus_unified_detection():+ """+ Test that QostodianNexus is properly detected by LiteLLM's unified guardrail system.++ This verifies the fix for the detection bug where QostodianNexus wasn't being+ recognized because apply_guardrail was only inherited, not in the class's own __dict__.+ """+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus++ # Create an instance (this is how LiteLLM uses it)+ instance = QostodianNexus(api_base="http://test:8800")++ # Test the exact detection logic used in litellm/proxy/utils.py:868+ # use_unified = "apply_guardrail" in type(callback).__dict__+ use_unified = "apply_guardrail" in type(instance).__dict__++ # Should be detected as using unified guardrail system+ assert use_unified is True, (+ "QostodianNexus should be detected by unified guardrail system. "+ "The apply_guardrail method must be present in QostodianNexus.__dict__"+ )++ # Also verify the method is callable+ assert hasattr(instance, "apply_guardrail")+ assert callable(instance.apply_guardrail)+++def test_qostodian_nexus_builtin_extra_headers():+ """Test that QostodianNexus includes built-in x-qostodian-nexus-identifiers-* headers."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus++ instance = QostodianNexus()++ expected_headers = [+ "x-qostodian-nexus-identifiers-trace",+ "x-qostodian-nexus-identifiers-source",+ "x-qostodian-nexus-identifiers-container",+ "x-qostodian-nexus-identifiers-identity",+ ]++ for header in expected_headers:+ assert header in instance.extra_headers, (+ f"Expected built-in header '{header}' to be in extra_headers"+ )+++def test_qostodian_nexus_extra_headers_merged():+ """Test that caller-supplied extra_headers are merged with built-in headers."""+ from litellm.proxy.guardrails.guardrail_hooks.qohash import QostodianNexus++ custom_header = "x-custom-correlation-id"+ instance = QostodianNexus(extra_headers=[custom_header])++ # Built-in headers should be present+ assert "x-qostodian-nexus-identifiers-trace" in instance.extra_headers+ # Custom header should also be present+ assert custom_header in instance.extra_headers+ # No duplicates+ assert len(instance.extra_headers) == len(set(instance.extra_headers))diff --git a/ui/litellm-dashboard/public/assets/logos/qohash.jpg b/ui/litellm-dashboard/public/assets/logos/qohash.jpgnew file mode 100644index 00000000000..50227ab3910Binary files /dev/null and b/ui/litellm-dashboard/public/assets/logos/qohash.jpg differdiff --git a/ui/litellm-dashboard/src/components/guardrails/guardrail_info_helpers.tsx b/ui/litellm-dashboard/src/components/guardrails/guardrail_info_helpers.tsxindex 2286eba7768..ac4b787e96a 100644--- a/ui/litellm-dashboard/src/components/guardrails/guardrail_info_helpers.tsx+++ b/ui/litellm-dashboard/src/components/guardrails/guardrail_info_helpers.tsx@@ -52,6 +52,7 @@ export const guardrail_provider_map: Record<string, string> = {Promptguard: "promptguard",LlmAsAJudge: "llm_as_a_judge",Xecguard: "xecguard",+ QostodianNexus: "qostodian_nexus",};// Function to populate provider map from API response - updates the original map@@ -138,6 +139,7 @@ export const guardrailLogoMap: Record<string, string> = {"LiteLLM Content Filter": `${asset_logos_folder}litellm_logo.jpg`,"LiteLLM LLM as a Judge": `${asset_logos_folder}litellm_logo.jpg`,"Akto": `${asset_logos_folder}akto.svg`,+ "Qostodian Nexus": `${asset_logos_folder}qohash.jpg`,};export const getGuardrailLogoAndName = (guardrailValue: string): { logo: string; displayName: string } => {