diff --git a/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py b/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py index 508736fb78e..ff261efa5b8 100644 --- a/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py +++ b/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py @@ -997,6 +997,33 @@ async def test_create_guardrail_endpoint( assert "Failed to initialize guardrail" in str(mock_logger.warning.call_args) +@pytest.mark.asyncio +async def test_create_guardrail_config_error_returns_400_not_500( + mocker, mock_guardrail_registry, mock_in_memory_handler +): + mock_prisma_client = mocker.Mock() + mock_in_memory_handler.initialize_guardrail.side_effect = ValueError("invalid config") + + mocker.patch("litellm.proxy.proxy_server.prisma_client", mock_prisma_client) + mocker.patch( + "litellm.proxy.guardrails.guardrail_endpoints.GUARDRAIL_REGISTRY", + mock_guardrail_registry, + ) + mocker.patch( + "litellm.proxy.guardrails.guardrail_registry.IN_MEMORY_GUARDRAIL_HANDLER", + mock_in_memory_handler, + ) + + with pytest.raises(HTTPException) as exc_info: + await create_guardrail(MOCK_CREATE_REQUEST, user_api_key_dict=MOCK_ADMIN_USER) + + assert exc_info.value.status_code == 400 + assert "invalid config" in str(exc_info.value.detail) + mock_guardrail_registry.add_guardrail_to_db.assert_called_once_with( + guardrail=MOCK_CREATE_REQUEST.guardrail, prisma_client=mocker.ANY + ) + + @pytest.mark.parametrize( "scenario,expected_result,expected_exception", [