api.py 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  1. """Use the Public API to export or update data that you have saved to W&B.
  2. Before using this API, you'll want to log data from your script — check the
  3. [Quickstart](https://docs.wandb.ai/models/quickstart) for more details.
  4. You might use the Public API to
  5. - update metadata or metrics for an experiment after it has been completed,
  6. - pull down your results as a dataframe for post-hoc analysis in a Jupyter notebook, or
  7. - check your saved model artifacts for those tagged as `ready-to-deploy`.
  8. For more on using the Public API, check out [our guide](https://docs.wandb.ai/models/track/public-api-guide).
  9. """
  10. from __future__ import annotations
  11. import json
  12. import logging
  13. import os
  14. import urllib
  15. from collections.abc import Iterator
  16. from http import HTTPStatus
  17. from typing import TYPE_CHECKING, Any, Callable, Literal
  18. from pydantic import ValidationError
  19. from typing_extensions import Unpack, overload
  20. from wandb_gql import Client, gql
  21. from wandb_gql.client import RetryError
  22. import wandb
  23. from wandb import env, util
  24. from wandb._analytics import tracked
  25. from wandb._iterutils import one
  26. from wandb._strutils import nameof
  27. from wandb.apis import public
  28. from wandb.apis.normalize import normalize_exceptions
  29. from wandb.apis.public.const import RETRY_TIMEDELTA
  30. from wandb.apis.public.registries import Registries, Registry
  31. from wandb.apis.public.registries._utils import fetch_org_entity_from_organization
  32. from wandb.apis.public.service_api import ServiceApi
  33. from wandb.apis.public.utils import (
  34. PathType,
  35. fetch_org_from_settings_or_entity,
  36. gql_compat,
  37. parse_org_from_registry_path,
  38. )
  39. from wandb.errors import UsageError
  40. from wandb.proto import wandb_internal_pb2 as pb
  41. from wandb.proto.wandb_telemetry_pb2 import Deprecated
  42. from wandb.sdk import wandb_login, wandb_setup
  43. from wandb.sdk.artifacts._gqlutils import resolve_org_entity_name
  44. from wandb.sdk.internal.internal_api import Api as InternalApi
  45. from wandb.sdk.launch.utils import LAUNCH_DEFAULT_PROJECT
  46. from wandb.sdk.lib import retry, runid, wbauth
  47. from wandb.sdk.lib.deprecation import warn_and_record_deprecation
  48. from wandb.sdk.lib.gql_request import GraphQLSession
  49. if TYPE_CHECKING:
  50. from wandb.automations import (
  51. ActionType,
  52. Automation,
  53. EventType,
  54. Integration,
  55. NewAutomation,
  56. SlackIntegration,
  57. WebhookIntegration,
  58. )
  59. from wandb.automations._utils import WriteAutomationsKwargs
  60. from wandb.sdk.artifacts.artifact import Artifact
  61. from .artifacts import (
  62. ArtifactCollection,
  63. ArtifactCollections,
  64. Artifacts,
  65. ArtifactType,
  66. ArtifactTypes,
  67. )
  68. from .teams import Team
  69. from .users import User
  70. logger = logging.getLogger(__name__)
  71. class RetryingClient:
  72. """A GraphQL client that retries requests on failure.
  73. <!-- lazydoc-ignore-class: internal -->
  74. """
  75. INFO_QUERY = gql(
  76. """
  77. query ServerInfo{
  78. serverInfo {
  79. cliVersionInfo
  80. latestLocalVersionInfo {
  81. outOfDate
  82. latestVersionString
  83. versionOnThisInstanceString
  84. }
  85. }
  86. }
  87. """
  88. )
  89. def __init__(self, client: Client):
  90. self._server_info = None
  91. self._client = client
  92. self._execute_decorated: Callable[..., Any] | None = None
  93. def execute(self, *args, **kwargs):
  94. if self._execute_decorated is None:
  95. self._execute_decorated = self._build_execute_wrapper()
  96. return self._execute_decorated(*args, **kwargs)
  97. def _build_execute_wrapper(self) -> Callable[..., Any]:
  98. import requests
  99. @retry.retriable(
  100. retry_timedelta=RETRY_TIMEDELTA,
  101. check_retry_fn=util.no_retry_auth,
  102. retryable_exceptions=(RetryError, requests.RequestException),
  103. )
  104. def _wrapped(*args, **kwargs):
  105. try:
  106. return self._client.execute(*args, **kwargs)
  107. except requests.exceptions.ReadTimeout:
  108. if "timeout" not in kwargs:
  109. timeout = self._client.transport.default_timeout
  110. wandb.termwarn(
  111. f"A graphql request initiated by the public wandb API timed out (timeout={timeout} sec). "
  112. f"Create a new API with an integer timeout larger than {timeout}, e.g., "
  113. f"`api = wandb.Api(timeout={timeout + 10})` to increase the graphql timeout."
  114. )
  115. raise
  116. return _wrapped
  117. @property
  118. def app_url(self):
  119. return util.app_url(self._client.transport.url.replace("/graphql", "")) + "/"
  120. @property
  121. def server_info(self):
  122. if self._server_info is None:
  123. self._server_info = self.execute(self.INFO_QUERY).get("serverInfo")
  124. return self._server_info
  125. def version_supported(
  126. self, min_version: str
  127. ) -> bool: # User not encouraged to use this class directly
  128. from packaging.version import parse
  129. return parse(min_version) <= parse(
  130. self.server_info["cliVersionInfo"]["max_cli_version"]
  131. )
  132. class Api:
  133. """Used for querying the W&B server.
  134. Examples:
  135. ```python
  136. import wandb
  137. wandb.Api()
  138. ```
  139. """
  140. _HTTP_TIMEOUT = env.get_http_timeout(19)
  141. def __init__(
  142. self,
  143. overrides: dict[str, Any] | None = None,
  144. timeout: int | None = None,
  145. api_key: str | None = None,
  146. ) -> None:
  147. """Initialize the API.
  148. Args:
  149. overrides: You can set `base_url` if you are
  150. using a W&B server other than `https://api.wandb.ai`. You can also
  151. set defaults for `entity`, `project`, and `run`.
  152. timeout: HTTP timeout in seconds for API requests. If not
  153. specified, the default timeout will be used.
  154. api_key: API key to use for authentication. If not provided,
  155. the API key from the current environment or configuration will be used.
  156. Prompts for an API key if none is provided
  157. or configured in the environment.
  158. """
  159. self.settings = InternalApi().settings()
  160. self.settings.update(overrides or {})
  161. self.settings["base_url"] = self.settings["base_url"].rstrip("/")
  162. if api_key:
  163. self._auth = wbauth.AuthApiKey(
  164. host=self.settings["base_url"],
  165. api_key=api_key,
  166. )
  167. else:
  168. self._auth = self._load_auth(base_url=self.settings["base_url"])
  169. base_url = self._auth.host.url
  170. if isinstance(self._auth, wbauth.AuthApiKey):
  171. self.api_key = self._auth.api_key
  172. wandb_login._verify_login(
  173. key=self.api_key,
  174. base_url=base_url,
  175. )
  176. else:
  177. self.api_key = None
  178. session_auth = self._auth.as_requests_auth()
  179. self._viewer = None
  180. self._projects = {}
  181. self._runs = {}
  182. self._sweeps = {}
  183. self._reports = {}
  184. self._default_entity = None
  185. self._timeout = timeout if timeout is not None else self._HTTP_TIMEOUT
  186. proxies = self.settings.get("_proxies") or json.loads(
  187. os.environ.get("WANDB__PROXIES", "{}")
  188. )
  189. self._base_client = Client(
  190. transport=GraphQLSession(
  191. headers={
  192. "User-Agent": self.user_agent,
  193. "Use-Admin-Privileges": "true",
  194. },
  195. use_json=True,
  196. # this timeout won't apply when the DNS lookup fails. in that case, it will be 60s
  197. # https://bugs.python.org/issue22889
  198. timeout=self._timeout,
  199. auth=session_auth,
  200. url="{}/graphql".format(base_url),
  201. proxies=proxies,
  202. )
  203. )
  204. self._client = RetryingClient(self._base_client)
  205. self._sentry = wandb.analytics.sentry.Sentry(pid=os.getpid())
  206. self._configure_sentry()
  207. settings = wandb_setup.singleton().settings.model_copy()
  208. settings.base_url = base_url
  209. settings.api_key = self.api_key or ""
  210. self._service_api = ServiceApi(settings=settings)
  211. def _load_auth(self, base_url: str) -> wbauth.Auth:
  212. """Load or prompt for authentication credentials."""
  213. auth = wbauth.authenticate_session(
  214. host=base_url,
  215. source="wandb.Api()",
  216. no_offline=True,
  217. input_timeout=wandb_setup.singleton().settings.login_timeout,
  218. )
  219. if not auth:
  220. raise UsageError(
  221. "No authentication configured. Use `wandb login` to log in."
  222. )
  223. return auth
  224. def _configure_sentry(self) -> None:
  225. if not env.error_reporting_enabled():
  226. return
  227. import requests
  228. try:
  229. viewer = self.viewer
  230. except (ValueError, requests.RequestException):
  231. # we need the viewer to configure the entity, and user email
  232. return
  233. email = viewer.email if viewer else None
  234. entity = self.default_entity
  235. self._sentry.configure_scope(
  236. tags={
  237. "entity": entity,
  238. "email": email,
  239. },
  240. )
  241. def create_project(self, name: str, entity: str) -> None:
  242. """Create a new project.
  243. Args:
  244. name: The name of the new project.
  245. entity: The entity of the new project.
  246. """
  247. from wandb.apis._generated import CREATE_PROJECT_GQL, UpsertModelInput
  248. gql_input = UpsertModelInput(name=name, entity_name=entity)
  249. self.client.execute(gql(CREATE_PROJECT_GQL), {"input": gql_input.model_dump()})
  250. def create_run(
  251. self,
  252. *,
  253. run_id: str | None = None,
  254. project: str | None = None,
  255. entity: str | None = None,
  256. ) -> public.Run:
  257. """Create a new run.
  258. Args:
  259. run_id: The ID to assign to the run. If not specified, W&B
  260. creates a random ID.
  261. project: The project where to log the run to. If no project is specified,
  262. log the run to a project called "Uncategorized".
  263. entity: The entity that owns the project. If no entity is
  264. specified, log the run to the default entity.
  265. Returns:
  266. The newly created `Run`.
  267. """
  268. if entity is None:
  269. entity = self.default_entity
  270. return public.Run.create(self, run_id=run_id, project=project, entity=entity)
  271. def create_run_queue(
  272. self,
  273. name: str,
  274. type: public.RunQueueResourceType,
  275. entity: str | None = None,
  276. prioritization_mode: public.RunQueuePrioritizationMode | None = None,
  277. config: dict | None = None,
  278. template_variables: dict | None = None,
  279. ) -> public.RunQueue:
  280. """Create a new run queue in W&B Launch.
  281. Args:
  282. name: Name of the queue to create
  283. type: Type of resource to be used for the queue. One of
  284. "local-container", "local-process", "kubernetes","sagemaker",
  285. or "gcp-vertex".
  286. entity: Name of the entity to create the queue. If `None`, use
  287. the configured or default entity.
  288. prioritization_mode: Version of prioritization to use.
  289. Either "V0" or `None`.
  290. config: Default resource configuration to be used for the queue.
  291. Use handlebars (eg. `{{var}}`) to specify template variables.
  292. template_variables: A dictionary of template variable schemas to
  293. use with the config.
  294. Returns:
  295. The newly created `RunQueue`.
  296. Raises:
  297. `ValueError` if any of the parameters are invalid
  298. `wandb.Error` on wandb API errors
  299. """
  300. # TODO(np): Need to check server capabilities for this feature
  301. # 0. assert params are valid/normalized
  302. if entity is None:
  303. entity = self.settings["entity"] or self.default_entity
  304. if entity is None:
  305. raise ValueError(
  306. "entity must be passed as a parameter, or set in settings"
  307. )
  308. if len(name) == 0:
  309. raise ValueError("name must be non-empty")
  310. if len(name) > 64:
  311. raise ValueError("name must be less than 64 characters")
  312. if type not in [
  313. "local-container",
  314. "local-process",
  315. "kubernetes",
  316. "sagemaker",
  317. "gcp-vertex",
  318. ]:
  319. raise ValueError(
  320. "resource_type must be one of 'local-container', 'local-process', 'kubernetes', 'sagemaker', or 'gcp-vertex'"
  321. )
  322. if prioritization_mode:
  323. prioritization_mode = prioritization_mode.upper()
  324. if prioritization_mode not in ["V0"]:
  325. raise ValueError("prioritization_mode must be 'V0' if present")
  326. if config is None:
  327. config = {}
  328. # 1. create required default launch project in the entity
  329. self.create_project(LAUNCH_DEFAULT_PROJECT, entity)
  330. api = InternalApi(
  331. default_settings={
  332. "entity": entity,
  333. "project": self.project(LAUNCH_DEFAULT_PROJECT),
  334. },
  335. retry_timedelta=RETRY_TIMEDELTA,
  336. )
  337. # 2. create default resource config, receive config id
  338. config_json = json.dumps({"resource_args": {type: config}})
  339. create_config_result = api.create_default_resource_config(
  340. entity, type, config_json, template_variables
  341. )
  342. if not create_config_result["success"]:
  343. raise wandb.Error("failed to create default resource config")
  344. config_id = create_config_result["defaultResourceConfigID"]
  345. # 3. create run queue
  346. create_queue_result = api.create_run_queue(
  347. entity,
  348. LAUNCH_DEFAULT_PROJECT,
  349. name,
  350. "PROJECT",
  351. prioritization_mode,
  352. config_id,
  353. )
  354. if not create_queue_result["success"]:
  355. raise wandb.Error("failed to create run queue")
  356. return public.RunQueue(
  357. client=self.client,
  358. name=name,
  359. entity=entity,
  360. prioritization_mode=prioritization_mode,
  361. _access="PROJECT",
  362. _default_resource_config_id=config_id,
  363. _default_resource_config=config,
  364. )
  365. def create_custom_chart(
  366. self,
  367. entity: str,
  368. name: str,
  369. display_name: str,
  370. spec_type: Literal["vega2"],
  371. access: Literal["private", "public"],
  372. spec: str | dict,
  373. ) -> str:
  374. """Create a custom chart preset and return its id.
  375. Args:
  376. entity: The entity (user or team) that owns the chart
  377. name: Unique identifier for the chart preset
  378. display_name: Human-readable name shown in the UI
  379. spec_type: Type of specification. Must be "vega2" for Vega-Lite v2 specifications.
  380. access: Access level for the chart:
  381. - "private": Chart is only accessible to the entity that created it
  382. - "public": Chart is publicly accessible
  383. spec: The Vega/Vega-Lite specification as a dictionary or JSON string
  384. Returns:
  385. The ID of the created chart preset in the format "entity/name"
  386. Raises:
  387. wandb.Error: If chart creation fails
  388. UnsupportedError: If the server doesn't support custom charts
  389. Example:
  390. ```python
  391. import wandb
  392. api = wandb.Api()
  393. # Define a simple bar chart specification
  394. vega_spec = {
  395. "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  396. "mark": "bar",
  397. "data": {"name": "wandb"},
  398. "encoding": {
  399. "x": {"field": "${field:x}", "type": "ordinal"},
  400. "y": {"field": "${field:y}", "type": "quantitative"},
  401. },
  402. }
  403. # Create the custom chart
  404. chart_id = api.create_custom_chart(
  405. entity="my-team",
  406. name="my-bar-chart",
  407. display_name="My Custom Bar Chart",
  408. spec_type="vega2",
  409. access="private",
  410. spec=vega_spec,
  411. )
  412. # Use with wandb.plot_table()
  413. chart = wandb.plot_table(
  414. vega_spec_name=chart_id,
  415. data_table=my_table,
  416. fields={"x": "category", "y": "value"},
  417. )
  418. ```
  419. """
  420. # Convert user-facing lowercase access to backend uppercase
  421. backend_access = access.upper()
  422. api = InternalApi(retry_timedelta=RETRY_TIMEDELTA)
  423. result = api.create_custom_chart(
  424. entity=entity,
  425. name=name,
  426. display_name=display_name,
  427. spec_type=spec_type,
  428. access=backend_access,
  429. spec=spec,
  430. )
  431. if result is None or result.get("chart") is None:
  432. raise wandb.Error("failed to create custom chart")
  433. return result["chart"]["id"]
  434. def upsert_run_queue(
  435. self,
  436. name: str,
  437. resource_config: dict,
  438. resource_type: public.RunQueueResourceType,
  439. entity: str | None = None,
  440. template_variables: dict | None = None,
  441. external_links: dict | None = None,
  442. prioritization_mode: public.RunQueuePrioritizationMode | None = None,
  443. ):
  444. """Upsert a run queue in W&B Launch.
  445. Args:
  446. name: Name of the queue to create
  447. entity: Optional name of the entity to create the queue. If `None`,
  448. use the configured or default entity.
  449. resource_config: Optional default resource configuration to be used
  450. for the queue. Use handlebars (eg. `{{var}}`) to specify
  451. template variables.
  452. resource_type: Type of resource to be used for the queue. One of
  453. "local-container", "local-process", "kubernetes", "sagemaker",
  454. or "gcp-vertex".
  455. template_variables: A dictionary of template variable schemas to
  456. be used with the config.
  457. external_links: Optional dictionary of external links to be used
  458. with the queue.
  459. prioritization_mode: Optional version of prioritization to use.
  460. Either "V0" or None
  461. Returns:
  462. The upserted `RunQueue`.
  463. Raises:
  464. ValueError if any of the parameters are invalid
  465. wandb.Error on wandb API errors
  466. """
  467. if entity is None:
  468. entity = self.settings["entity"] or self.default_entity
  469. if entity is None:
  470. raise ValueError(
  471. "entity must be passed as a parameter, or set in settings"
  472. )
  473. if len(name) == 0:
  474. raise ValueError("name must be non-empty")
  475. if len(name) > 64:
  476. raise ValueError("name must be less than 64 characters")
  477. prioritization_mode = prioritization_mode or "DISABLED"
  478. prioritization_mode = prioritization_mode.upper()
  479. if prioritization_mode not in ["V0", "DISABLED"]:
  480. raise ValueError(
  481. "prioritization_mode must be 'V0' or 'DISABLED' if present"
  482. )
  483. if resource_type not in [
  484. "local-container",
  485. "local-process",
  486. "kubernetes",
  487. "sagemaker",
  488. "gcp-vertex",
  489. ]:
  490. raise ValueError(
  491. "resource_type must be one of 'local-container', 'local-process', 'kubernetes', 'sagemaker', or 'gcp-vertex'"
  492. )
  493. self.create_project(LAUNCH_DEFAULT_PROJECT, entity)
  494. api = InternalApi(
  495. default_settings={
  496. "entity": entity,
  497. "project": self.project(LAUNCH_DEFAULT_PROJECT),
  498. },
  499. retry_timedelta=RETRY_TIMEDELTA,
  500. )
  501. # User provides external_links as a dict with name: url format
  502. # but backend stores it as a list of dicts with url and label keys.
  503. external_links = external_links or {}
  504. external_links = {
  505. "links": [
  506. {
  507. "label": key,
  508. "url": value,
  509. }
  510. for key, value in external_links.items()
  511. ]
  512. }
  513. upsert_run_queue_result = api.upsert_run_queue(
  514. name,
  515. entity,
  516. resource_type,
  517. {"resource_args": {resource_type: resource_config}},
  518. template_variables=template_variables,
  519. external_links=external_links,
  520. prioritization_mode=prioritization_mode,
  521. )
  522. if not upsert_run_queue_result["success"]:
  523. raise wandb.Error("failed to create run queue")
  524. schema_errors = (
  525. upsert_run_queue_result.get("configSchemaValidationErrors") or []
  526. )
  527. for error in schema_errors:
  528. wandb.termwarn(f"resource config validation: {error}")
  529. return public.RunQueue(
  530. client=self.client,
  531. name=name,
  532. entity=entity,
  533. )
  534. def create_user(self, email: str, admin: bool | None = False) -> User:
  535. """Create a new user.
  536. Args:
  537. email: The email address of the user.
  538. admin: Set user as a global instance administrator.
  539. Returns:
  540. A `User` object.
  541. """
  542. from .users import User
  543. return User.create(self, email, admin)
  544. def sync_tensorboard(self, root_dir, run_id=None, project=None, entity=None):
  545. """Sync a local directory containing tfevent files to wandb."""
  546. from wandb.sync import SyncManager # TODO: circular import madness
  547. run_id = run_id or runid.generate_id()
  548. project = project or self.settings.get("project") or "uncategorized"
  549. entity = entity or self.default_entity
  550. # TODO: pipe through log_path to inform the user how to debug
  551. sm = SyncManager(
  552. project=project,
  553. entity=entity,
  554. run_id=run_id,
  555. mark_synced=False,
  556. app_url=self.client.app_url,
  557. view=False,
  558. verbose=False,
  559. sync_tensorboard=True,
  560. )
  561. sm.add(root_dir)
  562. sm.start()
  563. while not sm.is_done():
  564. _ = sm.poll()
  565. return self.run("/".join([entity, project, run_id]))
  566. @property
  567. def client(self) -> RetryingClient:
  568. """Returns the client object."""
  569. return self._client
  570. @property
  571. def user_agent(self) -> str:
  572. """Returns W&B public user agent."""
  573. return "W&B Public Client {}".format(wandb.__version__)
  574. @property
  575. def default_entity(self) -> str | None:
  576. """Returns the default W&B entity."""
  577. from wandb.apis._generated import GET_DEFAULT_ENTITY_GQL, GetDefaultEntity
  578. if self._default_entity is None:
  579. data = self._client.execute(gql(GET_DEFAULT_ENTITY_GQL))
  580. result = GetDefaultEntity.model_validate(data)
  581. if (viewer := result.viewer) and (entity := viewer.entity):
  582. self._default_entity = entity
  583. return self._default_entity
  584. @property
  585. def viewer(self) -> User:
  586. """Returns the viewer object.
  587. Raises:
  588. ValueError: If viewer data is not able to be fetched from W&B.
  589. requests.RequestException: If an error occurs while making the graphql request.
  590. """
  591. from wandb.apis._generated import GET_VIEWER_GQL, GetViewer
  592. from .users import User
  593. if self._viewer is None:
  594. data = self._client.execute(gql(GET_VIEWER_GQL))
  595. result = GetViewer.model_validate(data)
  596. if (viewer := result.viewer) is None:
  597. msg = "Unable to fetch user data from W&B, please verify your API key is valid."
  598. raise ValueError(msg)
  599. self._viewer = User(self._client, viewer.model_dump(), api_key=self.api_key)
  600. self._default_entity = self._viewer.entity
  601. return self._viewer
  602. def flush(self):
  603. """Flush the local cache.
  604. The api object keeps a local cache of runs, so if the state of the run
  605. may change while executing your script you must clear the local cache
  606. with `api.flush()` to get the latest values associated with the run.
  607. """
  608. self._runs = {}
  609. def from_path(self, path: str):
  610. """Return a run, sweep, project or report from a path.
  611. Args:
  612. path: The path to the project, run, sweep or report
  613. Returns:
  614. A `Project`, `Run`, `Sweep`, or `BetaReport` instance.
  615. Raises:
  616. `wandb.Error` if path is invalid or the object doesn't exist.
  617. Examples:
  618. In the proceeding code snippets "project", "team", "run_id", "sweep_id",
  619. and "report_name" are placeholders for the project, team, run ID,
  620. sweep ID, and the name of a specific report, respectively.
  621. ```python
  622. import wandb
  623. api = wandb.Api()
  624. project = api.from_path("project")
  625. team_project = api.from_path("team/project")
  626. run = api.from_path("team/project/runs/run_id")
  627. sweep = api.from_path("team/project/sweeps/sweep_id")
  628. report = api.from_path("team/project/reports/report_name")
  629. ```
  630. """
  631. parts = path.strip("/ ").split("/")
  632. if len(parts) == 1:
  633. return self.project(path)
  634. elif len(parts) == 2:
  635. return self.project(parts[1], parts[0])
  636. elif len(parts) == 3:
  637. return self.run(path)
  638. elif len(parts) == 4:
  639. if parts[2].startswith("run"):
  640. return self.run(path)
  641. elif parts[2].startswith("sweep"):
  642. return self.sweep(path)
  643. elif parts[2].startswith("report"):
  644. if "--" not in parts[-1]:
  645. if "-" in parts[-1]:
  646. raise wandb.Error(
  647. "Invalid report path, should be team/project/reports/Name--XXXX"
  648. )
  649. else:
  650. parts[-1] = "--" + parts[-1]
  651. name, id = parts[-1].split("--")
  652. return public.BetaReport(
  653. self.client,
  654. {
  655. "displayName": urllib.parse.unquote(name.replace("-", " ")),
  656. "id": id,
  657. "spec": "{}",
  658. },
  659. parts[0],
  660. parts[1],
  661. )
  662. raise wandb.Error(
  663. "Invalid path, should be TEAM/PROJECT/TYPE/ID where TYPE is runs, sweeps, or reports"
  664. )
  665. def _parse_project_path(self, path):
  666. """Return project and entity for project specified by path."""
  667. project = self.settings["project"] or "uncategorized"
  668. entity = self.settings["entity"] or self.default_entity
  669. if path is None:
  670. return entity, project
  671. parts = path.split("/", 1)
  672. if len(parts) == 1:
  673. return entity, path
  674. return parts
  675. def _parse_path(self, path: str) -> tuple[str, str, str]:
  676. """Parse url, filepath, or docker paths.
  677. Allows paths in the following formats:
  678. - entity/project/runs/id (URL)
  679. - entity/project/id
  680. - entity/project:id (Docker style)
  681. - project/id
  682. - project:id
  683. - id (cannot contain colons)
  684. The path may also start with /runs/ or /sweeps/.
  685. Returns:
  686. A tuple with the extracted (entity, project, id).
  687. Raises:
  688. ValueError: If the path is in an invalid format or is missing
  689. an entity that is not otherwise provided.
  690. """
  691. # NOTE: This may result in a GQL call. Ideally we'd only access
  692. # `self.default_entity` lazily, but changing this requires care as it
  693. # changes when `self._default_entity` is cached and could impact other
  694. # code.
  695. entity: str | None = self.settings["entity"] or self.default_entity
  696. project: str | None = self.settings["project"]
  697. id: str | None = None
  698. input_path = path
  699. path = path.replace("/runs/", "/")
  700. path = path.replace("/sweeps/", "/")
  701. path = path.strip("/ ") # slashes and spaces
  702. parts = path.split("/")
  703. # "entity/project/runs/id"
  704. if len(parts) == 4 and parts[2] == "runs":
  705. entity, project, id = parts[0], parts[1], parts[3]
  706. # "entity/project/id"
  707. elif len(parts) == 3:
  708. entity, project, id = parts[0], parts[1], parts[2]
  709. elif len(parts) == 2:
  710. # "entity/project:id"
  711. if ":" in parts[1]:
  712. entity = parts[0]
  713. project, id = parts[1].split(":", maxsplit=1)
  714. # "project/id"
  715. else:
  716. project, id = parts[0], parts[1]
  717. elif len(parts) == 1 and parts[0]: # Don't match the empty string.
  718. # "project:id"
  719. if ":" in parts[0]:
  720. project, id = parts[0].split(":", maxsplit=1)
  721. # "id"
  722. else:
  723. id = parts[0]
  724. # Ignore whitespace.
  725. entity = entity and entity.strip()
  726. project = project and project.strip()
  727. id = id and id.strip()
  728. if not entity:
  729. raise ValueError(f"Invalid path: {input_path!r} (missing entity)")
  730. if not project:
  731. raise ValueError(f"Invalid path: {input_path!r} (missing project)")
  732. if not id:
  733. raise ValueError(f"Invalid path: {input_path!r}")
  734. return entity, project, id
  735. @overload
  736. def _parse_artifact_path(self, path: None) -> tuple[str | None, str]: ...
  737. @overload
  738. def _parse_artifact_path(self, path: str) -> tuple[str | None, str, str]: ...
  739. def _parse_artifact_path(self, path: str | None) -> tuple[str | None, ...]:
  740. """Return project, entity and artifact name for project specified by path."""
  741. from wandb.sdk.artifacts._validators import ArtifactPath
  742. project = self.settings["project"] or "uncategorized"
  743. entity = self.settings["entity"] or self.default_entity
  744. if path is None:
  745. return entity, project
  746. parsed = ArtifactPath.from_str(path)
  747. parsed = parsed.with_defaults(prefix=entity, project=project)
  748. return parsed.prefix, parsed.project, parsed.name
  749. def projects(
  750. self, entity: str | None = None, per_page: int = 200
  751. ) -> public.Projects:
  752. """Get projects for a given entity.
  753. Args:
  754. entity: Name of the entity requested. If None, will fall back to
  755. the default entity passed to `Api`. If no default entity,
  756. will raise a `ValueError`.
  757. per_page: Sets the page size for query pagination.
  758. Usually there is no reason to change this.
  759. Returns:
  760. A `Projects` object which is an iterable collection of `Project`objects.
  761. """
  762. if entity is None:
  763. entity = self.settings["entity"] or self.default_entity
  764. if entity is None:
  765. raise ValueError(
  766. "entity must be passed as a parameter, or set in settings"
  767. )
  768. if entity not in self._projects:
  769. self._projects[entity] = public.Projects(
  770. self.client, entity, per_page=per_page
  771. )
  772. return self._projects[entity]
  773. def project(self, name: str, entity: str | None = None) -> public.Project:
  774. """Return the `Project` with the given name (and entity, if given).
  775. Args:
  776. name: The project name.
  777. entity: Name of the entity requested. If None, will fall back to the
  778. default entity passed to `Api`. If no default entity, will
  779. raise a `ValueError`.
  780. Returns:
  781. A `Project` object.
  782. """
  783. from wandb.sdk.artifacts._validators import is_artifact_registry_project
  784. # For registry artifacts, capture potential org user inputted before resolving entity
  785. org = entity if is_artifact_registry_project(name) else ""
  786. if entity is None:
  787. entity = self.settings["entity"] or self.default_entity
  788. # For registry artifacts, resolve org-based entity
  789. if is_artifact_registry_project(name):
  790. settings_entity = self.settings["entity"] or self.default_entity
  791. entity = resolve_org_entity_name(
  792. self.client, non_org_entity=settings_entity, org_or_entity=org
  793. )
  794. return public.Project(self.client, entity, name, {})
  795. def reports(
  796. self, path: str = "", name: str | None = None, per_page: int = 50
  797. ) -> public.Reports:
  798. """Get reports for a given project path.
  799. Note: `wandb.Api.reports()` API is in beta and will likely change in
  800. future releases.
  801. Args:
  802. path: The path to the project the report resides in. Specify the
  803. entity that created the project as a prefix followed by a
  804. forward slash.
  805. name: Name of the report requested.
  806. per_page: Sets the page size for query pagination.
  807. Usually there is no reason to change this.
  808. Returns:
  809. A `Reports` object which is an iterable collection of
  810. `BetaReport` objects.
  811. Examples:
  812. ```python
  813. import wandb
  814. wandb.Api.reports("entity/project")
  815. ```
  816. """
  817. entity, project, _ = self._parse_path(path + "/fake_run")
  818. if name:
  819. name = urllib.parse.unquote(name)
  820. key = "/".join([entity, project, str(name)])
  821. else:
  822. key = "/".join([entity, project])
  823. if key not in self._reports:
  824. self._reports[key] = public.Reports(
  825. self.client,
  826. public.Project(self.client, entity, project, {}),
  827. name=name,
  828. per_page=per_page,
  829. )
  830. return self._reports[key]
  831. def create_team(self, team: str, admin_username: str | None = None) -> Team:
  832. """Create a new team.
  833. Args:
  834. team: The name of the team
  835. admin_username: Username of the admin user of the team.
  836. Defaults to the current user.
  837. Returns:
  838. A `Team` object.
  839. """
  840. from .teams import Team
  841. return Team.create(self, team, admin_username)
  842. def team(self, team: str) -> Team:
  843. """Return the matching `Team` with the given name.
  844. Args:
  845. team: The name of the team.
  846. Returns:
  847. A `Team` object.
  848. """
  849. from .teams import Team
  850. return Team(self.client, team)
  851. def user(self, username_or_email: str) -> User | None:
  852. """Return a user from a username or email address.
  853. This function only works for local administrators. Use `api.viewer`
  854. to get your own user object.
  855. Args:
  856. username_or_email: The username or email address of the user.
  857. Returns:
  858. A `User` object or None if a user is not found.
  859. """
  860. from wandb.apis._generated import SEARCH_USERS_GQL, SearchUsers
  861. from .users import User
  862. data = self._client.execute(gql(SEARCH_USERS_GQL), {"query": username_or_email})
  863. result = SearchUsers.model_validate(data)
  864. if not (conn := result.users) or not (edges := conn.edges):
  865. return None
  866. if len(edges) > 1:
  867. msg = f"Found multiple users, returning the first user matching {username_or_email!r}"
  868. wandb.termwarn(msg)
  869. return User(self._client, edges[0].node.model_dump(), api_key=self.api_key)
  870. def users(self, username_or_email: str) -> list[User]:
  871. """Return all users from a partial username or email address query.
  872. This function only works for local administrators. Use `api.viewer`
  873. to get your own user object.
  874. Args:
  875. username_or_email: The prefix or suffix of the user you want to find.
  876. Returns:
  877. An array of `User` objects.
  878. """
  879. from wandb.apis._generated import SEARCH_USERS_GQL, SearchUsers
  880. from .users import User
  881. data = self._client.execute(gql(SEARCH_USERS_GQL), {"query": username_or_email})
  882. result = SearchUsers.model_validate(data)
  883. if not ((conn := result.users) and (edges := conn.edges)):
  884. return []
  885. return [
  886. User(self._client, edge.node.model_dump(), api_key=self.api_key)
  887. for edge in edges
  888. ]
  889. def runs(
  890. self,
  891. path: str | None = None,
  892. filters: dict[str, Any] | None = None,
  893. order: str = "+created_at",
  894. per_page: int = 50,
  895. include_sweeps: bool = True,
  896. lazy: bool = True,
  897. ):
  898. """Returns a `Runs` object, which lazily iterates over `Run` objects.
  899. Fields you can filter by include:
  900. - `createdAt`: The timestamp when the run was created. (in ISO 8601 format, e.g. "2023-01-01T12:00:00Z")
  901. - `displayName`: The human-readable display name of the run. (e.g. "eager-fox-1")
  902. - `duration`: The total runtime of the run in seconds.
  903. - `group`: The group name used to organize related runs together.
  904. - `host`: The hostname where the run was executed.
  905. - `jobType`: The type of job or purpose of the run.
  906. - `name`: The unique identifier of the run. (e.g. "a1b2cdef")
  907. - `state`: The current state of the run.
  908. - `tags`: The tags associated with the run.
  909. - `username`: The username of the user who initiated the run
  910. Additionally, you can filter by items in the run config or summary metrics.
  911. Such as `config.experiment_name`, `summary_metrics.loss`, etc.
  912. For more complex filtering, you can use MongoDB query operators.
  913. For details, see: https://docs.mongodb.com/manual/reference/operator/query
  914. The following operations are supported:
  915. - `$and`
  916. - `$or`
  917. - `$nor`
  918. - `$eq`
  919. - `$ne`
  920. - `$gt`
  921. - `$gte`
  922. - `$lt`
  923. - `$lte`
  924. - `$in`
  925. - `$nin`
  926. - `$exists`
  927. - `$regex`
  928. Args:
  929. path: (str) path to project, should be in the form: "entity/project"
  930. filters: (dict) queries for specific runs using the MongoDB query language.
  931. You can filter by run properties such as config.key, summary_metrics.key, state, entity, createdAt, etc.
  932. For example: `{"config.experiment_name": "foo"}` would find runs with a config entry
  933. of experiment name set to "foo"
  934. order: (str) Order can be `created_at`, `heartbeat_at`, `config.*.value`, or `summary_metrics.*`.
  935. If you prepend order with a + order is ascending (default).
  936. If you prepend order with a - order is descending.
  937. The default order is run.created_at from oldest to newest.
  938. per_page: (int) Sets the page size for query pagination.
  939. include_sweeps: (bool) Whether to include the sweep runs in the results.
  940. lazy: (bool) Whether to use lazy loading for faster performance.
  941. When True (default), only essential run metadata is loaded initially.
  942. Heavy fields like config, summaryMetrics, and systemMetrics are loaded
  943. on-demand when accessed. Set to False for full data upfront.
  944. Returns:
  945. A `Runs` object, which is an iterable collection of `Run` objects.
  946. Examples:
  947. ```python
  948. import wandb
  949. from wandb.apis.public import Api
  950. # Find runs in project where config.experiment_name has been set to "foo"
  951. Api.runs(path="my_entity/project", filters={"config.experiment_name": "foo"})
  952. ```
  953. ```python
  954. # Find runs in project where config.experiment_name has been set to "foo" or "bar"
  955. Api.runs(
  956. path="my_entity/project",
  957. filters={
  958. "$or": [
  959. {"config.experiment_name": "foo"},
  960. {"config.experiment_name": "bar"},
  961. ]
  962. },
  963. )
  964. ```
  965. ```python
  966. # Find runs in project where config.experiment_name matches a regex
  967. # (anchors are not supported)
  968. Api.runs(
  969. path="my_entity/project",
  970. filters={"config.experiment_name": {"$regex": "b.*"}},
  971. )
  972. ```
  973. ```python
  974. # Find runs in project where the run name matches a regex
  975. # (anchors are not supported)
  976. Api.runs(
  977. path="my_entity/project", filters={"display_name": {"$regex": "^foo.*"}}
  978. )
  979. ```
  980. ```python
  981. # Find runs in project sorted by ascending loss
  982. Api.runs(path="my_entity/project", order="+summary_metrics.loss")
  983. ```
  984. """
  985. entity, project = self._parse_project_path(path)
  986. filters = filters or {}
  987. key = (path or "") + str(filters) + str(order)
  988. # Check if we have cached results
  989. if self._runs.get(key):
  990. cached_runs = self._runs[key]
  991. # If requesting full data but cached data is lazy, upgrade it
  992. if not lazy and cached_runs._lazy:
  993. cached_runs.upgrade_to_full()
  994. return cached_runs
  995. # Create new Runs object
  996. self._runs[key] = public.Runs(
  997. self.client,
  998. entity,
  999. project,
  1000. service_api=self._service_api,
  1001. filters=filters,
  1002. order=order,
  1003. per_page=per_page,
  1004. include_sweeps=include_sweeps,
  1005. lazy=lazy,
  1006. )
  1007. return self._runs[key]
  1008. @normalize_exceptions
  1009. def run(self, path=""):
  1010. """Return a single run by parsing path in the form `entity/project/run_id`.
  1011. Args:
  1012. path: Path to run in the form `entity/project/run_id`.
  1013. If `api.entity` is set, this can be in the form `project/run_id`
  1014. and if `api.project` is set this can just be the run_id.
  1015. Returns:
  1016. A `Run` object.
  1017. """
  1018. entity, project, run_id = self._parse_path(path)
  1019. if not self._runs.get(path):
  1020. # Individual runs should load full data by default
  1021. self._runs[path] = public.Run(
  1022. self.client,
  1023. entity,
  1024. project,
  1025. run_id,
  1026. service_api=self._service_api,
  1027. lazy=False,
  1028. )
  1029. return self._runs[path]
  1030. def queued_run(
  1031. self,
  1032. entity: str,
  1033. project: str,
  1034. queue_name: str,
  1035. run_queue_item_id: str,
  1036. project_queue=None,
  1037. priority=None,
  1038. ):
  1039. """Return a single queued run based on the path.
  1040. Parses paths of the form `entity/project/queue_id/run_queue_item_id`.
  1041. """
  1042. return public.QueuedRun(
  1043. self.client,
  1044. entity,
  1045. project,
  1046. queue_name,
  1047. run_queue_item_id,
  1048. project_queue=project_queue,
  1049. priority=priority,
  1050. )
  1051. def run_queue(
  1052. self,
  1053. entity: str,
  1054. name: str,
  1055. ):
  1056. """Return the named `RunQueue` for entity.
  1057. See `Api.create_run_queue` for more information on how to create a run queue.
  1058. """
  1059. return public.RunQueue(
  1060. self.client,
  1061. name,
  1062. entity,
  1063. )
  1064. @normalize_exceptions
  1065. def sweep(self, path=""):
  1066. """Return a sweep by parsing path in the form `entity/project/sweep_id`.
  1067. Args:
  1068. path: Path to sweep in the form entity/project/sweep_id.
  1069. If `api.entity` is set, this can be in the form
  1070. project/sweep_id and if `api.project` is set
  1071. this can just be the sweep_id.
  1072. Returns:
  1073. A `Sweep` object.
  1074. """
  1075. entity, project, sweep_id = self._parse_path(path)
  1076. if not self._sweeps.get(path):
  1077. self._sweeps[path] = public.Sweep(self.client, entity, project, sweep_id)
  1078. return self._sweeps[path]
  1079. @normalize_exceptions
  1080. def artifact_types(self, project: str | None = None) -> ArtifactTypes:
  1081. """Returns a collection of matching artifact types.
  1082. Args:
  1083. project: The project name or path to filter on.
  1084. Returns:
  1085. An iterable `ArtifactTypes` object.
  1086. """
  1087. from wandb.sdk.artifacts._validators import is_artifact_registry_project
  1088. from .artifacts import ArtifactTypes
  1089. project_path = project
  1090. entity, project = self._parse_project_path(project_path)
  1091. # If its a Registry project, the entity is considered to be an org instead
  1092. if is_artifact_registry_project(project):
  1093. settings_entity = self.settings["entity"] or self.default_entity
  1094. org = parse_org_from_registry_path(project_path, PathType.PROJECT)
  1095. entity = resolve_org_entity_name(
  1096. self.client, non_org_entity=settings_entity, org_or_entity=org
  1097. )
  1098. return ArtifactTypes(self.client, entity, project)
  1099. @normalize_exceptions
  1100. def artifact_type(self, type_name: str, project: str | None = None) -> ArtifactType:
  1101. """Returns the matching `ArtifactType`.
  1102. Args:
  1103. type_name: The name of the artifact type to retrieve.
  1104. project: If given, a project name or path to filter on.
  1105. Returns:
  1106. An `ArtifactType` object.
  1107. """
  1108. from wandb.sdk.artifacts._validators import is_artifact_registry_project
  1109. from .artifacts import ArtifactType
  1110. project_path = project
  1111. entity, project = self._parse_project_path(project_path)
  1112. # If its an Registry artifact, the entity is an org instead
  1113. if is_artifact_registry_project(project):
  1114. org = parse_org_from_registry_path(project_path, PathType.PROJECT)
  1115. settings_entity = self.settings["entity"] or self.default_entity
  1116. entity = resolve_org_entity_name(
  1117. self.client, non_org_entity=settings_entity, org_or_entity=org
  1118. )
  1119. return ArtifactType(self.client, entity, project, type_name)
  1120. @normalize_exceptions
  1121. def artifact_collections(
  1122. self, project_name: str, type_name: str, per_page: int = 50
  1123. ) -> ArtifactCollections:
  1124. """Returns a collection of matching artifact collections.
  1125. Args:
  1126. project_name: The name of the project to filter on.
  1127. type_name: The name of the artifact type to filter on.
  1128. per_page: Sets the page size for query pagination.
  1129. Usually there is no reason to change this.
  1130. Returns:
  1131. An iterable `ArtifactCollections` object.
  1132. """
  1133. from wandb.sdk.artifacts._validators import is_artifact_registry_project
  1134. from .artifacts import ArtifactCollections
  1135. entity, project = self._parse_project_path(project_name)
  1136. # If iterating through Registry project, the entity is considered to be an org instead
  1137. if is_artifact_registry_project(project):
  1138. org = parse_org_from_registry_path(project_name, PathType.PROJECT)
  1139. settings_entity = self.settings["entity"] or self.default_entity
  1140. entity = resolve_org_entity_name(
  1141. self.client, non_org_entity=settings_entity, org_or_entity=org
  1142. )
  1143. return ArtifactCollections(
  1144. self.client, entity, project, type_name, per_page=per_page
  1145. )
  1146. @normalize_exceptions
  1147. def artifact_collection(self, type_name: str, name: str) -> ArtifactCollection:
  1148. """Returns a single artifact collection by type.
  1149. You can use the returned `ArtifactCollection` object to retrieve
  1150. information about specific artifacts in that collection, and more.
  1151. Args:
  1152. type_name: The type of artifact collection to fetch.
  1153. name: An artifact collection name. Optionally append the entity
  1154. that logged the artifact as a prefix followed by a forward
  1155. slash.
  1156. Returns:
  1157. An `ArtifactCollection` object.
  1158. Examples:
  1159. In the proceeding code snippet "type", "entity", "project", and
  1160. "artifact_name" are placeholders for the collection type, your W&B
  1161. entity, name of the project the artifact is in, and the name of
  1162. the artifact, respectively.
  1163. ```python
  1164. import wandb
  1165. collections = wandb.Api().artifact_collection(
  1166. type_name="type", name="entity/project/artifact_name"
  1167. )
  1168. # Get the first artifact in the collection
  1169. artifact_example = collections.artifacts()[0]
  1170. # Download the contents of the artifact to the specified root directory.
  1171. artifact_example.download()
  1172. ```
  1173. """
  1174. from wandb.sdk.artifacts._validators import is_artifact_registry_project
  1175. from .artifacts import ArtifactCollection
  1176. entity, project, collection_name = self._parse_artifact_path(name)
  1177. # If its an Registry artifact, the entity is considered to be an org instead
  1178. if is_artifact_registry_project(project):
  1179. org = parse_org_from_registry_path(name, PathType.ARTIFACT)
  1180. settings_entity = self.settings["entity"] or self.default_entity
  1181. entity = resolve_org_entity_name(
  1182. self.client, non_org_entity=settings_entity, org_or_entity=org
  1183. )
  1184. if entity is None:
  1185. raise ValueError(
  1186. "Could not determine entity. Please include the entity as part of the collection name path."
  1187. )
  1188. return ArtifactCollection(
  1189. self.client, entity, project, collection_name, type_name
  1190. )
  1191. @normalize_exceptions
  1192. def artifact_versions(self, type_name, name, per_page=50):
  1193. """Deprecated. Use `Api.artifacts(type_name, name)` method instead."""
  1194. warn_and_record_deprecation(
  1195. feature=Deprecated(api__artifact_versions=True),
  1196. message=(
  1197. "Api.artifact_versions(type_name, name) is deprecated, "
  1198. "use Api.artifacts(type_name, name) instead."
  1199. ),
  1200. )
  1201. return self.artifacts(type_name, name, per_page=per_page)
  1202. @normalize_exceptions
  1203. def artifacts(
  1204. self,
  1205. type_name: str,
  1206. name: str,
  1207. per_page: int = 50,
  1208. tags: list[str] | None = None,
  1209. ) -> Artifacts:
  1210. """Return an `Artifacts` collection.
  1211. Args:
  1212. type_name: The type of artifacts to fetch.
  1213. name: The artifact's collection name. Optionally append the
  1214. entity that logged the artifact as a prefix followed by
  1215. a forward slash.
  1216. per_page: Sets the page size for query pagination. Usually
  1217. there is no reason to change this.
  1218. tags: Only return artifacts with all of these tags.
  1219. Returns:
  1220. An iterable `Artifacts` object.
  1221. Examples:
  1222. In the proceeding code snippet, "type", "entity", "project", and
  1223. "artifact_name" are placeholders for the artifact type, W&B entity,
  1224. name of the project the artifact was logged to,
  1225. and the name of the artifact, respectively.
  1226. ```python
  1227. import wandb
  1228. wandb.Api().artifacts(type_name="type", name="entity/project/artifact_name")
  1229. ```
  1230. """
  1231. from wandb.sdk.artifacts._validators import is_artifact_registry_project
  1232. from .artifacts import Artifacts
  1233. entity, project, collection_name = self._parse_artifact_path(name)
  1234. # If its an Registry project, the entity is considered to be an org instead
  1235. if is_artifact_registry_project(project):
  1236. org = parse_org_from_registry_path(name, PathType.ARTIFACT)
  1237. settings_entity = self.settings["entity"] or self.default_entity
  1238. entity = resolve_org_entity_name(
  1239. self.client, non_org_entity=settings_entity, org_or_entity=org
  1240. )
  1241. return Artifacts(
  1242. self.client,
  1243. entity,
  1244. project,
  1245. collection_name,
  1246. type_name,
  1247. per_page=per_page,
  1248. tags=tags,
  1249. )
  1250. @normalize_exceptions
  1251. def _artifact(
  1252. self, name: str, type: str | None = None, enable_tracking: bool = False
  1253. ) -> Artifact:
  1254. from wandb.sdk.artifacts._validators import (
  1255. FullArtifactPath,
  1256. is_artifact_registry_project,
  1257. )
  1258. from wandb.sdk.artifacts.artifact import Artifact
  1259. if name is None:
  1260. raise ValueError("You must specify name= to fetch an artifact.")
  1261. entity, project, artifact_name = self._parse_artifact_path(name)
  1262. # If its an Registry artifact, the entity is an org instead
  1263. if is_artifact_registry_project(project):
  1264. organization = (
  1265. name.split("/")[0]
  1266. if name.count("/") == 2
  1267. else self.settings["organization"]
  1268. )
  1269. # set entity to match the settings since in above code it was potentially set to an org
  1270. settings_entity = self.settings["entity"] or self.default_entity
  1271. # Registry artifacts are under the org entity. Because we offer a shorthand and alias for this path,
  1272. # we need to fetch the org entity to for the user behind the scenes.
  1273. entity = resolve_org_entity_name(
  1274. self.client, non_org_entity=settings_entity, org_or_entity=organization
  1275. )
  1276. if entity is None:
  1277. raise ValueError(
  1278. "Could not determine entity. Please include the entity as part of the artifact name path."
  1279. )
  1280. path = FullArtifactPath(prefix=entity, project=project, name=artifact_name)
  1281. artifact = Artifact._from_name(
  1282. path=path,
  1283. client=self.client,
  1284. enable_tracking=enable_tracking,
  1285. )
  1286. if type is not None and artifact.type != type:
  1287. raise ValueError(
  1288. f"type {type} specified but this artifact is of type {artifact.type}"
  1289. )
  1290. return artifact
  1291. @normalize_exceptions
  1292. def artifact(self, name: str, type: str | None = None):
  1293. """Returns a single artifact.
  1294. Args:
  1295. name: The artifact's name. The name of an artifact resembles a
  1296. filepath that consists, at a minimum, the name of the project
  1297. the artifact was logged to, the name of the artifact, and the
  1298. artifact's version or alias. Optionally append the entity that
  1299. logged the artifact as a prefix followed by a forward slash.
  1300. If no entity is specified in the name, the Run or API
  1301. setting's entity is used.
  1302. type: The type of artifact to fetch.
  1303. Returns:
  1304. An `Artifact` object.
  1305. Raises:
  1306. ValueError: If the artifact name is not specified.
  1307. ValueError: If the artifact type is specified but does not
  1308. match the type of the fetched artifact.
  1309. Examples:
  1310. In the following code snippets "entity", "project", "artifact",
  1311. "version", and "alias" are placeholders for your W&B entity, name
  1312. of the project the artifact is in, the name of the artifact,
  1313. and artifact's version, respectively.
  1314. ```python
  1315. import wandb
  1316. # Specify the project, artifact's name, and the artifact's alias
  1317. wandb.Api().artifact(name="project/artifact:alias")
  1318. # Specify the project, artifact's name, and a specific artifact version
  1319. wandb.Api().artifact(name="project/artifact:version")
  1320. # Specify the entity, project, artifact's name, and the artifact's alias
  1321. wandb.Api().artifact(name="entity/project/artifact:alias")
  1322. # Specify the entity, project, artifact's name, and a specific artifact version
  1323. wandb.Api().artifact(name="entity/project/artifact:version")
  1324. ```
  1325. Note:
  1326. This method is intended for external use only. Do not call `api.artifact()` within the wandb repository code.
  1327. """
  1328. return self._artifact(name=name, type=type, enable_tracking=True)
  1329. @normalize_exceptions
  1330. def job(self, name: str | None, path: str | None = None) -> public.Job:
  1331. """Return a `Job` object.
  1332. Args:
  1333. name: The name of the job.
  1334. path: The root path to download the job artifact.
  1335. Returns:
  1336. A `Job` object.
  1337. """
  1338. if name is None:
  1339. raise ValueError("You must specify name= to fetch a job.")
  1340. elif name.count("/") != 2 or ":" not in name:
  1341. raise ValueError(
  1342. "Invalid job specification. A job must be of the form: <entity>/<project>/<job-name>:<alias-or-version>"
  1343. )
  1344. return public.Job(self, name, path)
  1345. @normalize_exceptions
  1346. def list_jobs(self, entity: str, project: str) -> list[dict[str, Any]]:
  1347. """Return a list of jobs, if any, for the given entity and project.
  1348. Args:
  1349. entity: The entity for the listed jobs.
  1350. project: The project for the listed jobs.
  1351. Returns:
  1352. A list of matching jobs.
  1353. """
  1354. import requests
  1355. if entity is None:
  1356. raise ValueError("Specify an entity when listing jobs")
  1357. if project is None:
  1358. raise ValueError("Specify a project when listing jobs")
  1359. query = gql(
  1360. """
  1361. query ArtifactOfType(
  1362. $entityName: String!,
  1363. $projectName: String!,
  1364. $artifactTypeName: String!,
  1365. ) {
  1366. project(name: $projectName, entityName: $entityName) {
  1367. artifactType(name: $artifactTypeName) {
  1368. artifactCollections {
  1369. edges {
  1370. node {
  1371. artifacts {
  1372. edges {
  1373. node {
  1374. id
  1375. state
  1376. aliases {
  1377. alias
  1378. }
  1379. artifactSequence {
  1380. name
  1381. }
  1382. }
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. }
  1389. }
  1390. }
  1391. """
  1392. )
  1393. try:
  1394. artifact_query = self._client.execute(
  1395. query,
  1396. {
  1397. "projectName": project,
  1398. "entityName": entity,
  1399. "artifactTypeName": "job",
  1400. },
  1401. )
  1402. if not artifact_query or not artifact_query["project"]:
  1403. wandb.termerror(
  1404. f"Project: '{project}' not found in entity: '{entity}' or access denied."
  1405. )
  1406. return []
  1407. if artifact_query["project"]["artifactType"] is None:
  1408. return []
  1409. artifacts = artifact_query["project"]["artifactType"][
  1410. "artifactCollections"
  1411. ]["edges"]
  1412. return [x["node"]["artifacts"] for x in artifacts]
  1413. except requests.exceptions.HTTPError:
  1414. return False
  1415. @normalize_exceptions
  1416. def artifact_exists(self, name: str, type: str | None = None) -> bool:
  1417. """Whether an artifact version exists within the specified project and entity.
  1418. Args:
  1419. name: The name of artifact. Add the artifact's entity and project
  1420. as a prefix. Append the version or the alias of the artifact
  1421. with a colon. If the entity or project is not specified,
  1422. W&B uses override parameters if populated. Otherwise, the
  1423. entity is pulled from the user settings and the project is
  1424. set to "Uncategorized".
  1425. type: The type of artifact.
  1426. Returns:
  1427. True if the artifact version exists, False otherwise.
  1428. Examples:
  1429. In the proceeding code snippets "entity", "project", "artifact",
  1430. "version", and "alias" are placeholders for your W&B entity, name of
  1431. the project the artifact is in, the name of the artifact, and
  1432. artifact's version, respectively.
  1433. ```python
  1434. import wandb
  1435. wandb.Api().artifact_exists("entity/project/artifact:version")
  1436. wandb.Api().artifact_exists("entity/project/artifact:alias")
  1437. ```
  1438. """
  1439. import requests
  1440. try:
  1441. self._artifact(name, type)
  1442. except wandb.errors.CommError as e:
  1443. if isinstance(e.exc, requests.Timeout):
  1444. raise
  1445. return False
  1446. return True
  1447. @normalize_exceptions
  1448. def artifact_collection_exists(self, name: str, type: str) -> bool:
  1449. """Whether an artifact collection exists within a specified project and entity.
  1450. Args:
  1451. name: An artifact collection name. Optionally append the
  1452. entity that logged the artifact as a prefix followed by
  1453. a forward slash. If entity or project is not specified,
  1454. infer the collection from the override params if they exist.
  1455. Otherwise, entity is pulled from the user settings and project
  1456. will default to "uncategorized".
  1457. type: The type of artifact collection.
  1458. Returns:
  1459. True if the artifact collection exists, False otherwise.
  1460. Examples:
  1461. In the proceeding code snippet "type", and "collection_name" refer to the type
  1462. of the artifact collection and the name of the collection, respectively.
  1463. ```python
  1464. import wandb
  1465. wandb.Api.artifact_collection_exists(type="type", name="collection_name")
  1466. ```
  1467. """
  1468. import requests
  1469. try:
  1470. self.artifact_collection(type, name)
  1471. except wandb.errors.CommError as e:
  1472. if isinstance(e.exc, requests.Timeout):
  1473. raise
  1474. return False
  1475. return True
  1476. @tracked
  1477. def registries(
  1478. self,
  1479. organization: str | None = None,
  1480. filter: dict[str, Any] | None = None,
  1481. per_page: int = 100,
  1482. ) -> Registries:
  1483. """Returns a lazy iterator of `Registry` objects.
  1484. Use the iterator to search and filter registries, collections,
  1485. or artifact versions across your organization's registry.
  1486. Args:
  1487. organization: (str, optional) The organization of the registry to fetch.
  1488. If not specified, use the organization specified in the user's settings.
  1489. filter: (dict, optional) MongoDB-style filter to apply to each object in the lazy registry iterator.
  1490. Fields available to filter for registries are
  1491. `name`, `description`, `created_at`, `updated_at`.
  1492. Fields available to filter for collections are
  1493. `name`, `tag`, `description`, `created_at`, `updated_at`
  1494. Fields available to filter for versions are
  1495. `tag`, `alias`, `created_at`, `updated_at`, `metadata`
  1496. per_page: Sets the page size for query pagination.
  1497. Returns:
  1498. A lazy iterator of `Registry` objects.
  1499. Examples:
  1500. Find all registries with the names that contain "model"
  1501. ```python
  1502. import wandb
  1503. api = wandb.Api() # specify an org if your entity belongs to multiple orgs
  1504. api.registries(filter={"name": {"$regex": "model"}})
  1505. ```
  1506. Find all collections in the registries with the name "my_collection" and the tag "my_tag"
  1507. ```python
  1508. api.registries().collections(filter={"name": "my_collection", "tag": "my_tag"})
  1509. ```
  1510. Find all artifact versions in the registries with a collection name that contains "my_collection" and a version that has the alias "best"
  1511. ```python
  1512. api.registries().collections(
  1513. filter={"name": {"$regex": "my_collection"}}
  1514. ).versions(filter={"alias": "best"})
  1515. ```
  1516. Find all artifact versions in the registries that contain "model" and have the tag "prod" or alias "best"
  1517. ```python
  1518. api.registries(filter={"name": {"$regex": "model"}}).versions(
  1519. filter={"$or": [{"tag": "prod"}, {"alias": "best"}]}
  1520. )
  1521. ```
  1522. """
  1523. if not self._service_api.feature_enabled(pb.ARTIFACT_REGISTRY_SEARCH):
  1524. raise RuntimeError(
  1525. "Registry search API is not enabled on this wandb server version."
  1526. + " Please upgrade your server version or contact support"
  1527. + " at support@wandb.com."
  1528. )
  1529. organization = organization or fetch_org_from_settings_or_entity(
  1530. self.settings, self.default_entity
  1531. )
  1532. return Registries(
  1533. self.client, organization=organization, filter=filter, per_page=per_page
  1534. )
  1535. @tracked
  1536. def registry(self, name: str, organization: str | None = None) -> Registry:
  1537. """Return a registry given a registry name.
  1538. Args:
  1539. name: The name of the registry. This is without the `wandb-registry-`
  1540. prefix.
  1541. organization: The organization of the registry.
  1542. If no organization is set in the settings, the organization will be
  1543. fetched from the entity if the entity only belongs to one
  1544. organization.
  1545. Returns:
  1546. A registry object.
  1547. Examples:
  1548. Fetch and update a registry
  1549. ```python
  1550. import wandb
  1551. api = wandb.Api()
  1552. registry = api.registry(name="my-registry", organization="my-org")
  1553. registry.description = "This is an updated description"
  1554. registry.save()
  1555. ```
  1556. """
  1557. if not self._service_api.feature_enabled(pb.ARTIFACT_REGISTRY_SEARCH):
  1558. raise RuntimeError(
  1559. "api.registry() is not enabled on this wandb server version."
  1560. + " Please upgrade your server version or contact support"
  1561. + " at support@wandb.com."
  1562. )
  1563. organization = organization or fetch_org_from_settings_or_entity(
  1564. self.settings, self.default_entity
  1565. )
  1566. org_entity = fetch_org_entity_from_organization(self.client, organization)
  1567. registry = Registry(self.client, organization, org_entity, name)
  1568. registry.load()
  1569. return registry
  1570. @tracked
  1571. def create_registry(
  1572. self,
  1573. name: str,
  1574. visibility: Literal["organization", "restricted"],
  1575. organization: str | None = None,
  1576. description: str | None = None,
  1577. artifact_types: list[str] | None = None,
  1578. ) -> Registry:
  1579. """Create a new registry.
  1580. Args:
  1581. name: The name of the registry. Name must be unique within the organization.
  1582. visibility: The visibility of the registry.
  1583. organization: Anyone in the organization can view this registry. You can
  1584. edit their roles later from the settings in the UI.
  1585. restricted: Only invited members via the UI can access this registry.
  1586. Public sharing is disabled.
  1587. organization: The organization of the registry.
  1588. If no organization is set in the settings, the organization will be
  1589. fetched from the entity if the entity only belongs to one organization.
  1590. description: The description of the registry.
  1591. artifact_types: The accepted artifact types of the registry. A type is no
  1592. more than 128 characters and do not include characters `/` or `:`. If
  1593. not specified, all types are accepted.
  1594. Allowed types added to the registry cannot be removed later.
  1595. Returns:
  1596. A registry object.
  1597. Examples:
  1598. ```python
  1599. import wandb
  1600. api = wandb.Api()
  1601. registry = api.create_registry(
  1602. name="my-registry",
  1603. visibility="restricted",
  1604. organization="my-org",
  1605. description="This is a test registry",
  1606. artifact_types=["model"],
  1607. )
  1608. ```
  1609. """
  1610. if not self._service_api.feature_enabled(
  1611. pb.INCLUDE_ARTIFACT_TYPES_IN_REGISTRY_CREATION,
  1612. ):
  1613. raise RuntimeError(
  1614. "create_registry api is not enabled on this wandb server version."
  1615. + " Please upgrade your server version or contact support"
  1616. + " at support@wandb.com."
  1617. )
  1618. organization = organization or fetch_org_from_settings_or_entity(
  1619. self.settings, self.default_entity
  1620. )
  1621. try:
  1622. existing_registry = self.registry(name=name, organization=organization)
  1623. except ValueError:
  1624. existing_registry = None
  1625. if existing_registry:
  1626. raise ValueError(
  1627. f"Registry {name!r} already exists in organization {organization!r},"
  1628. " please use a different name."
  1629. )
  1630. return Registry.create(
  1631. self.client,
  1632. organization,
  1633. name,
  1634. visibility,
  1635. description,
  1636. artifact_types,
  1637. )
  1638. @tracked
  1639. def integrations(
  1640. self,
  1641. entity: str | None = None,
  1642. *,
  1643. per_page: int = 50,
  1644. ) -> Iterator[Integration]:
  1645. """Return an iterator of all integrations for an entity.
  1646. Args:
  1647. entity: The entity (e.g. team name) for which to
  1648. fetch integrations. If not provided, the user's default entity
  1649. will be used.
  1650. per_page: Number of integrations to fetch per page.
  1651. Defaults to 50. Usually there is no reason to change this.
  1652. Yields:
  1653. Iterator[SlackIntegration | WebhookIntegration]: An iterator of any supported integrations.
  1654. """
  1655. from wandb.apis.public.integrations import Integrations
  1656. variables = {"entity": entity or self.default_entity}
  1657. return Integrations(self.client, variables=variables, per_page=per_page)
  1658. @tracked
  1659. def webhook_integrations(
  1660. self, entity: str | None = None, *, per_page: int = 50
  1661. ) -> Iterator[WebhookIntegration]:
  1662. """Returns an iterator of webhook integrations for an entity.
  1663. Args:
  1664. entity: The entity (e.g. team name) for which to
  1665. fetch integrations. If not provided, the user's default entity
  1666. will be used.
  1667. per_page: Number of integrations to fetch per page.
  1668. Defaults to 50. Usually there is no reason to change this.
  1669. Yields:
  1670. Iterator[WebhookIntegration]: An iterator of webhook integrations.
  1671. Examples:
  1672. Get all registered webhook integrations for the team "my-team":
  1673. ```python
  1674. import wandb
  1675. api = wandb.Api()
  1676. webhook_integrations = api.webhook_integrations(entity="my-team")
  1677. ```
  1678. Find only webhook integrations that post requests to "https://my-fake-url.com":
  1679. ```python
  1680. webhook_integrations = api.webhook_integrations(entity="my-team")
  1681. my_webhooks = [
  1682. ig
  1683. for ig in webhook_integrations
  1684. if ig.url_endpoint.startswith("https://my-fake-url.com")
  1685. ]
  1686. ```
  1687. """
  1688. from wandb.apis.public.integrations import WebhookIntegrations
  1689. variables = {"entity": entity or self.default_entity}
  1690. return WebhookIntegrations(self.client, variables=variables, per_page=per_page)
  1691. @tracked
  1692. def slack_integrations(
  1693. self, *, entity: str | None = None, per_page: int = 50
  1694. ) -> Iterator[SlackIntegration]:
  1695. """Returns an iterator of Slack integrations for an entity.
  1696. Args:
  1697. entity: The entity (e.g. team name) for which to
  1698. fetch integrations. If not provided, the user's default entity
  1699. will be used.
  1700. per_page: Number of integrations to fetch per page.
  1701. Defaults to 50. Usually there is no reason to change this.
  1702. Yields:
  1703. Iterator[SlackIntegration]: An iterator of Slack integrations.
  1704. Examples:
  1705. Get all registered Slack integrations for the team "my-team":
  1706. ```python
  1707. import wandb
  1708. api = wandb.Api()
  1709. slack_integrations = api.slack_integrations(entity="my-team")
  1710. ```
  1711. Find only Slack integrations that post to channel names starting with "team-alerts-":
  1712. ```python
  1713. slack_integrations = api.slack_integrations(entity="my-team")
  1714. team_alert_integrations = [
  1715. ig
  1716. for ig in slack_integrations
  1717. if ig.channel_name.startswith("team-alerts-")
  1718. ]
  1719. ```
  1720. """
  1721. from wandb.apis.public.integrations import SlackIntegrations
  1722. variables = {"entity": entity or self.default_entity}
  1723. return SlackIntegrations(self.client, variables=variables, per_page=per_page)
  1724. def _supports_automation(
  1725. self,
  1726. *,
  1727. event: EventType | None = None,
  1728. action: ActionType | None = None,
  1729. ) -> bool:
  1730. """Returns whether the server recognizes the automation event and/or action."""
  1731. from wandb.automations._utils import (
  1732. ALWAYS_SUPPORTED_ACTIONS,
  1733. ALWAYS_SUPPORTED_EVENTS,
  1734. )
  1735. supports_event = (
  1736. (event is None)
  1737. or (event in ALWAYS_SUPPORTED_EVENTS)
  1738. or self._service_api.feature_enabled(f"AUTOMATION_EVENT_{event.value}")
  1739. )
  1740. supports_action = (
  1741. (action is None)
  1742. or (action in ALWAYS_SUPPORTED_ACTIONS)
  1743. or self._service_api.feature_enabled(f"AUTOMATION_ACTION_{action.value}")
  1744. )
  1745. return supports_event and supports_action
  1746. def _omitted_automation_fragments(self) -> set[str]:
  1747. """Returns the names of unsupported automation-related fragments.
  1748. Older servers won't recognize newer GraphQL types, so a valid request may
  1749. unnecessarily error out because it won't recognize fragments defined on those types.
  1750. So e.g. if a server does not support `NO_OP` action types, then the following need to be
  1751. removed from the body of the GraphQL request:
  1752. - Fragment definition:
  1753. ```
  1754. fragment NoOpActionFields on NoOpTriggeredAction {
  1755. noOp
  1756. }
  1757. ```
  1758. - Fragment spread in selection set:
  1759. ```
  1760. {
  1761. ...NoOpActionFields
  1762. # ... other fields ...
  1763. }
  1764. ```
  1765. """
  1766. from wandb.automations import ActionType
  1767. from wandb.automations._generated import (
  1768. GenericWebhookActionFields,
  1769. NoOpActionFields,
  1770. NotificationActionFields,
  1771. QueueJobActionFields,
  1772. )
  1773. # Note: we can't currently define this as a constant outside the method
  1774. # and still keep it nearby in this module, because it relies on pydantic v2-only imports
  1775. fragment_names: dict[ActionType, str] = {
  1776. ActionType.NO_OP: nameof(NoOpActionFields),
  1777. ActionType.QUEUE_JOB: nameof(QueueJobActionFields),
  1778. ActionType.NOTIFICATION: nameof(NotificationActionFields),
  1779. ActionType.GENERIC_WEBHOOK: nameof(GenericWebhookActionFields),
  1780. }
  1781. return set(
  1782. name
  1783. for action in ActionType
  1784. if (not self._supports_automation(action=action))
  1785. and (name := fragment_names.get(action))
  1786. )
  1787. @tracked
  1788. def automation(
  1789. self,
  1790. name: str,
  1791. *,
  1792. entity: str | None = None,
  1793. ) -> Automation:
  1794. """Returns the only Automation matching the parameters.
  1795. Args:
  1796. name: The name of the automation to fetch.
  1797. entity: The entity to fetch the automation for.
  1798. Raises:
  1799. ValueError: If zero or multiple Automations match the search criteria.
  1800. Examples:
  1801. Get an existing automation named "my-automation":
  1802. ```python
  1803. import wandb
  1804. api = wandb.Api()
  1805. automation = api.automation(name="my-automation")
  1806. ```
  1807. Get an existing automation named "other-automation", from the entity "my-team":
  1808. ```python
  1809. automation = api.automation(name="other-automation", entity="my-team")
  1810. ```
  1811. """
  1812. return one(
  1813. self.automations(entity=entity, name=name),
  1814. too_short=ValueError("No automations found"),
  1815. too_long=ValueError("Multiple automations found"),
  1816. )
  1817. @tracked
  1818. def automations(
  1819. self,
  1820. entity: str | None = None,
  1821. *,
  1822. name: str | None = None,
  1823. per_page: int = 50,
  1824. ) -> Iterator[Automation]:
  1825. """Returns an iterator over all Automations that match the given parameters.
  1826. If no parameters are provided, the returned iterator will contain all
  1827. Automations that the user has access to.
  1828. Args:
  1829. entity: The entity to fetch the automations for.
  1830. name: The name of the automation to fetch.
  1831. per_page: The number of automations to fetch per page.
  1832. Defaults to 50. Usually there is no reason to change this.
  1833. Returns:
  1834. A list of automations.
  1835. Examples:
  1836. Fetch all existing automations for the entity "my-team":
  1837. ```python
  1838. import wandb
  1839. api = wandb.Api()
  1840. automations = api.automations(entity="my-team")
  1841. ```
  1842. """
  1843. from wandb.apis.public.automations import Automations
  1844. from wandb.automations._generated import (
  1845. GET_AUTOMATIONS_BY_ENTITY_GQL,
  1846. GET_AUTOMATIONS_GQL,
  1847. )
  1848. # For now, we need to use different queries depending on whether entity is given
  1849. variables = {"entity": entity}
  1850. if entity is None:
  1851. gql_str = GET_AUTOMATIONS_GQL # Automations for viewer
  1852. else:
  1853. gql_str = GET_AUTOMATIONS_BY_ENTITY_GQL # Automations for entity
  1854. # If needed, rewrite the GraphQL field selection set to omit unsupported fields/fragments/types
  1855. omit_fragments = self._omitted_automation_fragments()
  1856. query = gql_compat(gql_str, omit_fragments=omit_fragments)
  1857. iterator = Automations(
  1858. self.client, variables=variables, per_page=per_page, _query=query
  1859. )
  1860. # FIXME: this is crude, move this client-side filtering logic into backend
  1861. if name is not None:
  1862. iterator = filter(lambda x: x.name == name, iterator)
  1863. yield from iterator
  1864. @normalize_exceptions
  1865. @tracked
  1866. def create_automation(
  1867. self,
  1868. obj: NewAutomation,
  1869. *,
  1870. fetch_existing: bool = False,
  1871. **kwargs: Unpack[WriteAutomationsKwargs],
  1872. ) -> Automation:
  1873. """Create a new Automation.
  1874. Args:
  1875. obj:
  1876. The automation to create.
  1877. fetch_existing:
  1878. If True, and a conflicting automation already exists, attempt
  1879. to fetch the existing automation instead of raising an error.
  1880. **kwargs:
  1881. Any additional values to assign to the automation before
  1882. creating it. If given, these will override any values that may
  1883. already be set on the automation:
  1884. - `name`: The name of the automation.
  1885. - `description`: The description of the automation.
  1886. - `enabled`: Whether the automation is enabled.
  1887. - `scope`: The scope of the automation.
  1888. - `event`: The event that triggers the automation.
  1889. - `action`: The action that is triggered by the automation.
  1890. Returns:
  1891. The saved Automation.
  1892. Examples:
  1893. Create a new automation named "my-automation" that sends a Slack notification
  1894. when a run within a specific project logs a metric exceeding a custom threshold:
  1895. ```python
  1896. import wandb
  1897. from wandb.automations import OnRunMetric, RunEvent, SendNotification
  1898. api = wandb.Api()
  1899. project = api.project("my-project", entity="my-team")
  1900. # Use the first Slack integration for the team
  1901. slack_hook = next(api.slack_integrations(entity="my-team"))
  1902. event = OnRunMetric(
  1903. scope=project,
  1904. filter=RunEvent.metric("custom-metric") > 10,
  1905. )
  1906. action = SendNotification.from_integration(slack_hook)
  1907. automation = api.create_automation(
  1908. event >> action,
  1909. name="my-automation",
  1910. description="Send a Slack message whenever 'custom-metric' exceeds 10.",
  1911. )
  1912. ```
  1913. """
  1914. import requests
  1915. from wandb.automations import Automation
  1916. from wandb.automations._generated import CREATE_AUTOMATION_GQL, CreateAutomation
  1917. from wandb.automations._utils import prepare_to_create
  1918. gql_input = prepare_to_create(obj, **kwargs)
  1919. if not self._supports_automation(
  1920. event=(event := gql_input.triggering_event_type),
  1921. action=(action := gql_input.triggered_action_type),
  1922. ):
  1923. raise ValueError(
  1924. f"Automation event or action ({event!r} -> {action!r}) "
  1925. "is not supported on this wandb server version. "
  1926. "Please upgrade your server version, or contact support at "
  1927. "support@wandb.com."
  1928. )
  1929. # If needed, rewrite the GraphQL field selection set to omit unsupported fields/fragments/types
  1930. omit_fragments = self._omitted_automation_fragments()
  1931. mutation = gql_compat(CREATE_AUTOMATION_GQL, omit_fragments=omit_fragments)
  1932. variables = {"input": gql_input.model_dump()}
  1933. name = gql_input.name
  1934. try:
  1935. data = self.client.execute(mutation, variable_values=variables)
  1936. except requests.HTTPError as e:
  1937. status = HTTPStatus(e.response.status_code)
  1938. if status is HTTPStatus.CONFLICT: # 409
  1939. if fetch_existing:
  1940. wandb.termlog(f"Automation {name!r} exists. Fetching it instead.")
  1941. return self.automation(name=name)
  1942. raise ValueError(
  1943. f"Automation {name!r} exists. Unable to create another with the same name."
  1944. ) from None
  1945. raise
  1946. try:
  1947. result = CreateAutomation.model_validate(data).result
  1948. except ValidationError as e:
  1949. msg = f"Invalid response while creating automation {name!r}"
  1950. raise RuntimeError(msg) from e
  1951. if (result is None) or (result.trigger is None):
  1952. msg = f"Empty response while creating automation {name!r}"
  1953. raise RuntimeError(msg)
  1954. return Automation.model_validate(result.trigger)
  1955. @normalize_exceptions
  1956. @tracked
  1957. def update_automation(
  1958. self,
  1959. obj: Automation,
  1960. *,
  1961. create_missing: bool = False,
  1962. **kwargs: Unpack[WriteAutomationsKwargs],
  1963. ) -> Automation:
  1964. """Update an existing automation.
  1965. Args:
  1966. obj: The automation to update. Must be an existing automation.
  1967. create_missing (bool):
  1968. If True, and the automation does not exist, create it.
  1969. **kwargs:
  1970. Any additional values to assign to the automation before
  1971. updating it. If given, these will override any values that may
  1972. already be set on the automation:
  1973. - `name`: The name of the automation.
  1974. - `description`: The description of the automation.
  1975. - `enabled`: Whether the automation is enabled.
  1976. - `scope`: The scope of the automation.
  1977. - `event`: The event that triggers the automation.
  1978. - `action`: The action that is triggered by the automation.
  1979. Returns:
  1980. The updated automation.
  1981. Examples:
  1982. Disable and edit the description of an existing automation ("my-automation"):
  1983. ```python
  1984. import wandb
  1985. api = wandb.Api()
  1986. automation = api.automation(name="my-automation")
  1987. automation.enabled = False
  1988. automation.description = "Kept for reference, but no longer used."
  1989. updated_automation = api.update_automation(automation)
  1990. ```
  1991. OR
  1992. ```python
  1993. import wandb
  1994. api = wandb.Api()
  1995. automation = api.automation(name="my-automation")
  1996. updated_automation = api.update_automation(
  1997. automation,
  1998. enabled=False,
  1999. description="Kept for reference, but no longer used.",
  2000. )
  2001. ```
  2002. """
  2003. import requests
  2004. from wandb.automations import ActionType, Automation
  2005. from wandb.automations._generated import UPDATE_AUTOMATION_GQL, UpdateAutomation
  2006. from wandb.automations._utils import prepare_to_update
  2007. # Check if the server even supports updating automations.
  2008. #
  2009. # NOTE: Unfortunately, there is no current server feature flag for this. As a workaround,
  2010. # we check whether the server supports the NO_OP action, which is a reasonably safe proxy
  2011. # for whether it supports updating automations.
  2012. if not self._supports_automation(action=ActionType.NO_OP):
  2013. raise RuntimeError(
  2014. "Updating existing automations is not enabled on this wandb server version. "
  2015. "Please upgrade your server version, or contact support at support@wandb.com."
  2016. )
  2017. gql_input = prepare_to_update(obj, **kwargs)
  2018. if not self._supports_automation(
  2019. event=(event := gql_input.triggering_event_type),
  2020. action=(action := gql_input.triggered_action_type),
  2021. ):
  2022. raise ValueError(
  2023. f"Automation event or action ({event.value} -> {action.value}) "
  2024. "is not supported on this wandb server version. "
  2025. "Please upgrade your server version, or contact support at "
  2026. "support@wandb.com."
  2027. )
  2028. # If needed, rewrite the GraphQL field selection set to omit unsupported fields/fragments/types
  2029. omit_fragments = self._omitted_automation_fragments()
  2030. mutation = gql_compat(UPDATE_AUTOMATION_GQL, omit_fragments=omit_fragments)
  2031. variables = {"input": gql_input.model_dump()}
  2032. name = gql_input.name
  2033. try:
  2034. data = self.client.execute(mutation, variable_values=variables)
  2035. except requests.HTTPError as e:
  2036. status = HTTPStatus(e.response.status_code)
  2037. if status is HTTPStatus.NOT_FOUND: # 404
  2038. if create_missing:
  2039. wandb.termlog(f"Automation {name!r} not found. Creating it.")
  2040. return self.create_automation(obj)
  2041. raise ValueError(
  2042. f"Automation {name!r} not found. Unable to edit it."
  2043. ) from e
  2044. # Not a (known) recoverable HTTP error
  2045. wandb.termerror(f"Got response status {status!r}: {e.response.text!r}")
  2046. raise
  2047. try:
  2048. result = UpdateAutomation.model_validate(data).result
  2049. except ValidationError as e:
  2050. msg = f"Invalid response while updating automation {name!r}"
  2051. raise RuntimeError(msg) from e
  2052. if (result is None) or (result.trigger is None):
  2053. msg = f"Empty response while updating automation {name!r}"
  2054. raise RuntimeError(msg)
  2055. return Automation.model_validate(result.trigger)
  2056. @normalize_exceptions
  2057. @tracked
  2058. def delete_automation(self, obj: Automation | str) -> Literal[True]:
  2059. """Delete an automation.
  2060. Args:
  2061. obj: The automation to delete, or its ID.
  2062. Returns:
  2063. True if the automation was deleted successfully.
  2064. """
  2065. from wandb.automations._generated import DELETE_AUTOMATION_GQL, DeleteAutomation
  2066. from wandb.automations._utils import extract_id
  2067. id_ = extract_id(obj)
  2068. mutation = gql(DELETE_AUTOMATION_GQL)
  2069. variables = {"id": id_}
  2070. data = self.client.execute(mutation, variable_values=variables)
  2071. try:
  2072. result = DeleteAutomation.model_validate(data).result
  2073. except ValidationError as e:
  2074. msg = f"Invalid response while deleting automation {id_!r}"
  2075. raise RuntimeError(msg) from e
  2076. if result is None:
  2077. msg = f"Empty response while deleting automation {id_!r}"
  2078. raise RuntimeError(msg)
  2079. if not result.success:
  2080. raise RuntimeError(f"Failed to delete automation: {id_!r}")
  2081. return result.success