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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
"""Use the Public API to export or update data that you have saved to W&B.

Before using this API, you'll want to log data from your script β€” check the
[Quickstart](https://docs.wandb.ai/quickstart) for more details.

You might use the Public API to
 - update metadata or metrics for an experiment after it has been completed,
 - pull down your results as a dataframe for post-hoc analysis in a Jupyter notebook, or
 - check your saved model artifacts for those tagged as `ready-to-deploy`.

For more on using the Public API, check out [our guide](https://docs.wandb.com/guides/track/public-api-guide).
"""

import json
import logging
import os
import urllib
from http import HTTPStatus
from typing import (
    TYPE_CHECKING,
    Any,
    Dict,
    Iterator,
    List,
    Literal,
    Optional,
    Set,
    Union,
)

import requests
from pydantic import ValidationError
from typing_extensions import Unpack
from wandb_gql import Client, gql
from wandb_gql.client import RetryError

import wandb
from wandb import env, util
from wandb._iterutils import one
from wandb.apis import public
from wandb.apis.normalize import normalize_exceptions
from wandb.apis.public.const import RETRY_TIMEDELTA
from wandb.apis.public.registries.registries_search import Registries
from wandb.apis.public.registries.registry import Registry
from wandb.apis.public.registries.utils import _fetch_org_entity_from_organization
from wandb.apis.public.utils import (
    PathType,
    fetch_org_from_settings_or_entity,
    gql_compat,
    parse_org_from_registry_path,
)
from wandb.proto.wandb_deprecated import Deprecated
from wandb.proto.wandb_internal_pb2 import ServerFeature
from wandb.sdk.artifacts._validators import is_artifact_registry_project
from wandb.sdk.internal.internal_api import Api as InternalApi
from wandb.sdk.internal.thread_local_settings import _thread_local_api_settings
from wandb.sdk.launch.utils import LAUNCH_DEFAULT_PROJECT
from wandb.sdk.lib import retry, runid
from wandb.sdk.lib.deprecate import deprecate
from wandb.sdk.lib.gql_request import GraphQLSession

if TYPE_CHECKING:
    from wandb.automations import (
        ActionType,
        Automation,
        EventType,
        Integration,
        NewAutomation,
        SlackIntegration,
        WebhookIntegration,
    )
    from wandb.automations._utils import WriteAutomationsKwargs

logger = logging.getLogger(__name__)


class RetryingClient:
    INFO_QUERY = gql(
        """
        query ServerInfo{
            serverInfo {
                cliVersionInfo
                latestLocalVersionInfo {
                    outOfDate
                    latestVersionString
                    versionOnThisInstanceString
                }
            }
        }
        """
    )

    def __init__(self, client: Client):
        self._server_info = None
        self._client = client

    @property
    def app_url(self):
        return util.app_url(self._client.transport.url.replace("/graphql", "")) + "/"

    @retry.retriable(
        retry_timedelta=RETRY_TIMEDELTA,
        check_retry_fn=util.no_retry_auth,
        retryable_exceptions=(RetryError, requests.RequestException),
    )
    def execute(
        self, *args, **kwargs
    ):  # User not encouraged to use this class directly
        try:
            return self._client.execute(*args, **kwargs)
        except requests.exceptions.ReadTimeout:
            if "timeout" not in kwargs:
                timeout = self._client.transport.default_timeout
                wandb.termwarn(
                    f"A graphql request initiated by the public wandb API timed out (timeout={timeout} sec). "
                    f"Create a new API with an integer timeout larger than {timeout}, e.g., `api = wandb.Api(timeout={timeout + 10})` "
                    f"to increase the graphql timeout."
                )
            raise

    @property
    def server_info(self):
        if self._server_info is None:
            self._server_info = self.execute(self.INFO_QUERY).get("serverInfo")
        return self._server_info

    def version_supported(
        self, min_version: str
    ) -> bool:  # User not encouraged to use this class directly
        from packaging.version import parse

        return parse(min_version) <= parse(
            self.server_info["cliVersionInfo"]["max_cli_version"]
        )


class Api:
    """Used for querying the wandb server.

    Examples:
        Most common way to initialize
        >>> wandb.Api()

    Args:
        overrides: (dict) You can set `base_url` if you are using a wandb server
            other than https://api.wandb.ai.
            You can also set defaults for `entity`, `project`, and `run`.
    """

    _HTTP_TIMEOUT = env.get_http_timeout(19)
    DEFAULT_ENTITY_QUERY = gql(
        """
        query Viewer{
            viewer {
                id
                entity
            }
        }
        """
    )

    VIEWER_QUERY = gql(
        """
        query Viewer{
            viewer {
                id
                flags
                entity
                username
                email
                admin
                apiKeys {
                    edges {
                        node {
                            id
                            name
                            description
                        }
                    }
                }
                teams {
                    edges {
                        node {
                            name
                        }
                    }
                }
            }
        }
        """
    )
    USERS_QUERY = gql(
        """
        query SearchUsers($query: String) {
            users(query: $query) {
                edges {
                    node {
                        id
                        flags
                        entity
                        admin
                        email
                        deletedAt
                        username
                        apiKeys {
                            edges {
                                node {
                                    id
                                    name
                                    description
                                }
                            }
                        }
                        teams {
                            edges {
                                node {
                                    name
                                }
                            }
                        }
                    }
                }
            }
        }
        """
    )

    CREATE_PROJECT = gql(
        """
        mutation upsertModel(
            $description: String
            $entityName: String
            $id: String
            $name: String
            $framework: String
            $access: String
            $views: JSONString
        ) {
            upsertModel(
            input: {
                description: $description
                entityName: $entityName
                id: $id
                name: $name
                framework: $framework
                access: $access
                views: $views
            }
            ) {
            project {
                id
                name
                entityName
                description
                access
                views
            }
            model {
                id
                name
                entityName
                description
                access
                views
            }
            inserted
            }
        }
    """
    )

    def __init__(
        self,
        overrides: Optional[Dict[str, Any]] = None,
        timeout: Optional[int] = None,
        api_key: Optional[str] = None,
    ) -> None:
        self.settings = InternalApi().settings()

        _overrides = overrides or {}
        self.settings.update(_overrides)
        self.settings["base_url"] = self.settings["base_url"].rstrip("/")
        if "organization" in _overrides:
            self.settings["organization"] = _overrides["organization"]
        if "username" in _overrides and "entity" not in _overrides:
            wandb.termwarn(
                'Passing "username" to Api is deprecated. please use "entity" instead.'
            )
            self.settings["entity"] = _overrides["username"]

        self._api_key = api_key
        if self.api_key is None and _thread_local_api_settings.cookies is None:
            wandb.login(host=_overrides.get("base_url"))

        self._viewer = None
        self._projects = {}
        self._runs = {}
        self._sweeps = {}
        self._reports = {}
        self._default_entity = None
        self._timeout = timeout if timeout is not None else self._HTTP_TIMEOUT
        auth = None
        if not _thread_local_api_settings.cookies:
            auth = ("api", self.api_key)
        proxies = self.settings.get("_proxies") or json.loads(
            os.environ.get("WANDB__PROXIES", "{}")
        )
        self._base_client = Client(
            transport=GraphQLSession(
                headers={
                    "User-Agent": self.user_agent,
                    "Use-Admin-Privileges": "true",
                    **(_thread_local_api_settings.headers or {}),
                },
                use_json=True,
                # this timeout won't apply when the DNS lookup fails. in that case, it will be 60s
                # https://bugs.python.org/issue22889
                timeout=self._timeout,
                auth=auth,
                url="{}/graphql".format(self.settings["base_url"]),
                cookies=_thread_local_api_settings.cookies,
                proxies=proxies,
            )
        )
        self._client = RetryingClient(self._base_client)

    def create_project(self, name: str, entity: str) -> None:
        """Create a new project.

        Args:
            name: (str) The name of the new project.
            entity: (str) The entity of the new project.
        """
        self.client.execute(self.CREATE_PROJECT, {"entityName": entity, "name": name})

    def create_run(
        self,
        *,
        run_id: Optional[str] = None,
        project: Optional[str] = None,
        entity: Optional[str] = None,
    ) -> "public.Run":
        """Create a new run.

        Args:
            run_id: (str, optional) The ID to assign to the run, if given.  The run ID is automatically generated by
                default, so in general, you do not need to specify this and should only do so at your own risk.
            project: (str, optional) If given, the project of the new run.
            entity: (str, optional) If given, the entity of the new run.

        Returns:
            The newly created `Run`.
        """
        if entity is None:
            entity = self.default_entity
        return public.Run.create(self, run_id=run_id, project=project, entity=entity)

    def create_run_queue(
        self,
        name: str,
        type: "public.RunQueueResourceType",
        entity: Optional[str] = None,
        prioritization_mode: Optional["public.RunQueuePrioritizationMode"] = None,
        config: Optional[dict] = None,
        template_variables: Optional[dict] = None,
    ) -> "public.RunQueue":
        """Create a new run queue (launch).

        Args:
            name: (str) Name of the queue to create
            type: (str) Type of resource to be used for the queue. One of "local-container", "local-process", "kubernetes", "sagemaker", or "gcp-vertex".
            entity: (str) Optional name of the entity to create the queue. If None, will use the configured or default entity.
            prioritization_mode: (str) Optional version of prioritization to use. Either "V0" or None
            config: (dict) Optional default resource configuration to be used for the queue. Use handlebars (eg. `{{var}}`) to specify template variables.
            template_variables: (dict) A dictionary of template variable schemas to be used with the config. Expected format of:
                `{
                    "var-name": {
                        "schema": {
                            "type": ("string", "number", or "integer"),
                            "default": (optional value),
                            "minimum": (optional minimum),
                            "maximum": (optional maximum),
                            "enum": [..."(options)"]
                        }
                    }
                }`

        Returns:
            The newly created `RunQueue`

        Raises:
            ValueError if any of the parameters are invalid
            wandb.Error on wandb API errors
        """
        # TODO(np): Need to check server capabilities for this feature
        # 0. assert params are valid/normalized
        if entity is None:
            entity = self.settings["entity"] or self.default_entity
            if entity is None:
                raise ValueError(
                    "entity must be passed as a parameter, or set in settings"
                )

        if len(name) == 0:
            raise ValueError("name must be non-empty")
        if len(name) > 64:
            raise ValueError("name must be less than 64 characters")

        if type not in [
            "local-container",
            "local-process",
            "kubernetes",
            "sagemaker",
            "gcp-vertex",
        ]:
            raise ValueError(
                "resource_type must be one of 'local-container', 'local-process', 'kubernetes', 'sagemaker', or 'gcp-vertex'"
            )

        if prioritization_mode:
            prioritization_mode = prioritization_mode.upper()
            if prioritization_mode not in ["V0"]:
                raise ValueError("prioritization_mode must be 'V0' if present")

        if config is None:
            config = {}

        # 1. create required default launch project in the entity
        self.create_project(LAUNCH_DEFAULT_PROJECT, entity)

        api = InternalApi(
            default_settings={
                "entity": entity,
                "project": self.project(LAUNCH_DEFAULT_PROJECT),
            },
            retry_timedelta=RETRY_TIMEDELTA,
        )

        # 2. create default resource config, receive config id
        config_json = json.dumps({"resource_args": {type: config}})
        create_config_result = api.create_default_resource_config(
            entity, type, config_json, template_variables
        )
        if not create_config_result["success"]:
            raise wandb.Error("failed to create default resource config")
        config_id = create_config_result["defaultResourceConfigID"]

        # 3. create run queue
        create_queue_result = api.create_run_queue(
            entity,
            LAUNCH_DEFAULT_PROJECT,
            name,
            "PROJECT",
            prioritization_mode,
            config_id,
        )
        if not create_queue_result["success"]:
            raise wandb.Error("failed to create run queue")

        return public.RunQueue(
            client=self.client,
            name=name,
            entity=entity,
            prioritization_mode=prioritization_mode,
            _access="PROJECT",
            _default_resource_config_id=config_id,
            _default_resource_config=config,
        )

    def upsert_run_queue(
        self,
        name: str,
        resource_config: dict,
        resource_type: "public.RunQueueResourceType",
        entity: Optional[str] = None,
        template_variables: Optional[dict] = None,
        external_links: Optional[dict] = None,
        prioritization_mode: Optional["public.RunQueuePrioritizationMode"] = None,
    ):
        """Upsert a run queue (launch).

        Args:
            name: (str) Name of the queue to create
            entity: (str) Optional name of the entity to create the queue. If None, will use the configured or default entity.
            resource_config: (dict) Optional default resource configuration to be used for the queue. Use handlebars (eg. `{{var}}`) to specify template variables.
            resource_type: (str) Type of resource to be used for the queue. One of "local-container", "local-process", "kubernetes", "sagemaker", or "gcp-vertex".
            template_variables: (dict) A dictionary of template variable schemas to be used with the config. Expected format of:
                `{
                    "var-name": {
                        "schema": {
                            "type": ("string", "number", or "integer"),
                            "default": (optional value),
                            "minimum": (optional minimum),
                            "maximum": (optional maximum),
                            "enum": [..."(options)"]
                        }
                    }
                }`
            external_links: (dict) Optional dictionary of external links to be used with the queue. Expected format of:
                `{
                    "name": "url"
                }`
            prioritization_mode: (str) Optional version of prioritization to use. Either "V0" or None

        Returns:
            The upserted `RunQueue`.

        Raises:
            ValueError if any of the parameters are invalid
            wandb.Error on wandb API errors
        """
        if entity is None:
            entity = self.settings["entity"] or self.default_entity
            if entity is None:
                raise ValueError(
                    "entity must be passed as a parameter, or set in settings"
                )

        if len(name) == 0:
            raise ValueError("name must be non-empty")
        if len(name) > 64:
            raise ValueError("name must be less than 64 characters")

        prioritization_mode = prioritization_mode or "DISABLED"
        prioritization_mode = prioritization_mode.upper()
        if prioritization_mode not in ["V0", "DISABLED"]:
            raise ValueError(
                "prioritization_mode must be 'V0' or 'DISABLED' if present"
            )

        if resource_type not in [
            "local-container",
            "local-process",
            "kubernetes",
            "sagemaker",
            "gcp-vertex",
        ]:
            raise ValueError(
                "resource_type must be one of 'local-container', 'local-process', 'kubernetes', 'sagemaker', or 'gcp-vertex'"
            )

        self.create_project(LAUNCH_DEFAULT_PROJECT, entity)
        api = InternalApi(
            default_settings={
                "entity": entity,
                "project": self.project(LAUNCH_DEFAULT_PROJECT),
            },
            retry_timedelta=RETRY_TIMEDELTA,
        )
        # User provides external_links as a dict with name: url format
        # but backend stores it as a list of dicts with url and label keys.
        external_links = external_links or {}
        external_links = {
            "links": [
                {
                    "label": key,
                    "url": value,
                }
                for key, value in external_links.items()
            ]
        }
        upsert_run_queue_result = api.upsert_run_queue(
            name,
            entity,
            resource_type,
            {"resource_args": {resource_type: resource_config}},
            template_variables=template_variables,
            external_links=external_links,
            prioritization_mode=prioritization_mode,
        )
        if not upsert_run_queue_result["success"]:
            raise wandb.Error("failed to create run queue")
        schema_errors = (
            upsert_run_queue_result.get("configSchemaValidationErrors") or []
        )
        for error in schema_errors:
            wandb.termwarn(f"resource config validation: {error}")

        return public.RunQueue(
            client=self.client,
            name=name,
            entity=entity,
        )

    def create_user(self, email, admin=False):
        """Create a new user.

        Args:
            email: (str) The email address of the user
            admin: (bool) Whether this user should be a global instance admin

        Returns:
            A `User` object
        """
        return public.User.create(self, email, admin)

    def sync_tensorboard(self, root_dir, run_id=None, project=None, entity=None):
        """Sync a local directory containing tfevent files to wandb."""
        from wandb.sync import SyncManager  # TODO: circular import madness

        run_id = run_id or runid.generate_id()
        project = project or self.settings.get("project") or "uncategorized"
        entity = entity or self.default_entity
        # TODO: pipe through log_path to inform the user how to debug
        sm = SyncManager(
            project=project,
            entity=entity,
            run_id=run_id,
            mark_synced=False,
            app_url=self.client.app_url,
            view=False,
            verbose=False,
            sync_tensorboard=True,
        )
        sm.add(root_dir)
        sm.start()
        while not sm.is_done():
            _ = sm.poll()
        return self.run("/".join([entity, project, run_id]))

    @property
    def client(self) -> RetryingClient:
        return self._client

    @property
    def user_agent(self) -> str:
        return "W&B Public Client {}".format(wandb.__version__)

    @property
    def api_key(self) -> Optional[str]:
        # just use thread local api key if it's set
        if _thread_local_api_settings.api_key:
            return _thread_local_api_settings.api_key
        if self._api_key is not None:
            return self._api_key
        auth = requests.utils.get_netrc_auth(self.settings["base_url"])
        key = None
        if auth:
            key = auth[-1]
        # Environment should take precedence
        if os.getenv("WANDB_API_KEY"):
            key = os.environ["WANDB_API_KEY"]
        self._api_key = key  # memoize key
        return key

    @property
    def default_entity(self) -> Optional[str]:
        if self._default_entity is None:
            res = self._client.execute(self.DEFAULT_ENTITY_QUERY)
            self._default_entity = (res.get("viewer") or {}).get("entity")
        return self._default_entity

    @property
    def viewer(self) -> "public.User":
        if self._viewer is None:
            self._viewer = public.User(
                self._client, self._client.execute(self.VIEWER_QUERY).get("viewer")
            )
            self._default_entity = self._viewer.entity
        return self._viewer

    def flush(self):
        """Flush the local cache.

        The api object keeps a local cache of runs, so if the state of the run may
        change while executing your script you must clear the local cache with
        `api.flush()` to get the latest values associated with the run.
        """
        self._runs = {}

    def from_path(self, path):
        """Return a run, sweep, project or report from a path.

        Examples:
            ```
            project = api.from_path("my_project")
            team_project = api.from_path("my_team/my_project")
            run = api.from_path("my_team/my_project/runs/id")
            sweep = api.from_path("my_team/my_project/sweeps/id")
            report = api.from_path("my_team/my_project/reports/My-Report-Vm11dsdf")
            ```

        Args:
            path: (str) The path to the project, run, sweep or report

        Returns:
            A `Project`, `Run`, `Sweep`, or `BetaReport` instance.

        Raises:
            wandb.Error if path is invalid or the object doesn't exist
        """
        parts = path.strip("/ ").split("/")
        if len(parts) == 1:
            return self.project(path)
        elif len(parts) == 2:
            return self.project(parts[1], parts[0])
        elif len(parts) == 3:
            return self.run(path)
        elif len(parts) == 4:
            if parts[2].startswith("run"):
                return self.run(path)
            elif parts[2].startswith("sweep"):
                return self.sweep(path)
            elif parts[2].startswith("report"):
                if "--" not in parts[-1]:
                    if "-" in parts[-1]:
                        raise wandb.Error(
                            "Invalid report path, should be team/project/reports/Name--XXXX"
                        )
                    else:
                        parts[-1] = "--" + parts[-1]
                name, id = parts[-1].split("--")
                return public.BetaReport(
                    self.client,
                    {
                        "display_name": urllib.parse.unquote(name.replace("-", " ")),
                        "id": id,
                        "spec": "{}",
                    },
                    parts[0],
                    parts[1],
                )
        raise wandb.Error(
            "Invalid path, should be TEAM/PROJECT/TYPE/ID where TYPE is runs, sweeps, or reports"
        )

    def _parse_project_path(self, path):
        """Return project and entity for project specified by path."""
        project = self.settings["project"] or "uncategorized"
        entity = self.settings["entity"] or self.default_entity
        if path is None:
            return entity, project
        parts = path.split("/", 1)
        if len(parts) == 1:
            return entity, path
        return parts

    def _parse_path(self, path):
        """Parse url, filepath, or docker paths.

        Allows paths in the following formats:
        - url: entity/project/runs/id
        - path: entity/project/id
        - docker: entity/project:id

        Entity is optional and will fall back to the current logged-in user.
        """
        project = self.settings["project"] or "uncategorized"
        entity = self.settings["entity"] or self.default_entity
        parts = (
            path.replace("/runs/", "/").replace("/sweeps/", "/").strip("/ ").split("/")
        )
        if ":" in parts[-1]:
            id = parts[-1].split(":")[-1]
            parts[-1] = parts[-1].split(":")[0]
        elif parts[-1]:
            id = parts[-1]
        if len(parts) == 1 and project != "uncategorized":
            pass
        elif len(parts) > 1:
            project = parts[1]
            if entity and id == project:
                project = parts[0]
            else:
                entity = parts[0]
            if len(parts) == 3:
                entity = parts[0]
        else:
            project = parts[0]
        return entity, project, id

    def _parse_artifact_path(self, path):
        """Return project, entity and artifact name for project specified by path."""
        project = self.settings["project"] or "uncategorized"
        entity = self.settings["entity"] or self.default_entity
        if path is None:
            return entity, project

        path, colon, alias = path.partition(":")
        full_alias = colon + alias

        parts = path.split("/")
        if len(parts) > 3:
            raise ValueError("Invalid artifact path: {}".format(path))
        elif len(parts) == 1:
            return entity, project, path + full_alias
        elif len(parts) == 2:
            return entity, parts[0], parts[1] + full_alias
        parts[-1] += full_alias
        return parts

    def projects(
        self, entity: Optional[str] = None, per_page: int = 200
    ) -> "public.Projects":
        """Get projects for a given entity.

        Args:
            entity: (str) Name of the entity requested.  If None, will fall back to the
                default entity passed to `Api`.  If no default entity, will raise a `ValueError`.
            per_page: (int) Sets the page size for query pagination.  Usually there is no reason to change this.

        Returns:
            A `Projects` object which is an iterable collection of `Project` objects.
        """
        if entity is None:
            entity = self.settings["entity"] or self.default_entity
            if entity is None:
                raise ValueError(
                    "entity must be passed as a parameter, or set in settings"
                )
        if entity not in self._projects:
            self._projects[entity] = public.Projects(
                self.client, entity, per_page=per_page
            )
        return self._projects[entity]

    def project(self, name: str, entity: Optional[str] = None) -> "public.Project":
        """Return the `Project` with the given name (and entity, if given).

        Args:
            name: (str) The project name.
            entity: (str) Name of the entity requested.  If None, will fall back to the
                default entity passed to `Api`.  If no default entity, will raise a `ValueError`.

        Returns:
            A `Project` object.
        """
        # For registry artifacts, capture potential org user inputted before resolving entity
        org = entity if is_artifact_registry_project(name) else ""

        if entity is None:
            entity = self.settings["entity"] or self.default_entity

        # For registry artifacts, resolve org-based entity
        if is_artifact_registry_project(name):
            settings_entity = self.settings["entity"] or self.default_entity
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=org
            )
        return public.Project(self.client, entity, name, {})

    def reports(
        self, path: str = "", name: Optional[str] = None, per_page: int = 50
    ) -> "public.Reports":
        """Get reports for a given project path.

        WARNING: This api is in beta and will likely change in a future release

        Args:
            path: (str) path to project the report resides in, should be in the form: "entity/project"
            name: (str, optional) optional name of the report requested.
            per_page: (int) Sets the page size for query pagination.  Usually there is no reason to change this.

        Returns:
            A `Reports` object which is an iterable collection of `BetaReport` objects.
        """
        entity, project, _ = self._parse_path(path + "/fake_run")

        if name:
            name = urllib.parse.unquote(name)
            key = "/".join([entity, project, str(name)])
        else:
            key = "/".join([entity, project])

        if key not in self._reports:
            self._reports[key] = public.Reports(
                self.client,
                public.Project(self.client, entity, project, {}),
                name=name,
                per_page=per_page,
            )
        return self._reports[key]

    def create_team(self, team, admin_username=None):
        """Create a new team.

        Args:
            team: (str) The name of the team
            admin_username: (str) optional username of the admin user of the team, defaults to the current user.

        Returns:
            A `Team` object
        """
        return public.Team.create(self, team, admin_username)

    def team(self, team: str) -> "public.Team":
        """Return the matching `Team` with the given name.

        Args:
            team: (str) The name of the team.

        Returns:
            A `Team` object.
        """
        return public.Team(self.client, team)

    def user(self, username_or_email: str) -> Optional["public.User"]:
        """Return a user from a username or email address.

        Note: This function only works for Local Admins, if you are trying to get your own user object, please use `api.viewer`.

        Args:
            username_or_email: (str) The username or email address of the user

        Returns:
            A `User` object or None if a user couldn't be found
        """
        res = self._client.execute(self.USERS_QUERY, {"query": username_or_email})
        if len(res["users"]["edges"]) == 0:
            return None
        elif len(res["users"]["edges"]) > 1:
            wandb.termwarn(
                "Found multiple users, returning the first user matching {}".format(
                    username_or_email
                )
            )
        return public.User(self._client, res["users"]["edges"][0]["node"])

    def users(self, username_or_email: str) -> List["public.User"]:
        """Return all users from a partial username or email address query.

        Note: This function only works for Local Admins, if you are trying to get your own user object, please use `api.viewer`.

        Args:
            username_or_email: (str) The prefix or suffix of the user you want to find

        Returns:
            An array of `User` objects
        """
        res = self._client.execute(self.USERS_QUERY, {"query": username_or_email})
        return [
            public.User(self._client, edge["node"]) for edge in res["users"]["edges"]
        ]

    def runs(
        self,
        path: Optional[str] = None,
        filters: Optional[Dict[str, Any]] = None,
        order: str = "+created_at",
        per_page: int = 50,
        include_sweeps: bool = True,
    ):
        """Return a set of runs from a project that match the filters provided.

        Fields you can filter by include:
        - `createdAt`: The timestamp when the run was created. (in ISO 8601 format, e.g. "2023-01-01T12:00:00Z")
        - `displayName`: The human-readable display name of the run. (e.g. "eager-fox-1")
        - `duration`: The total runtime of the run in seconds.
        - `group`: The group name used to organize related runs together.
        - `host`: The hostname where the run was executed.
        - `jobType`: The type of job or purpose of the run.
        - `name`: The unique identifier of the run. (e.g. "a1b2cdef")
        - `state`: The current state of the run.
        - `tags`: The tags associated with the run.
        - `username`: The username of the user who initiated the run

        Additionally, you can filter by items in the run config or summary metrics.
        Such as `config.experiment_name`, `summary_metrics.loss`, etc.

        For more complex filtering, you can use MongoDB query operators.
        For details, see: https://docs.mongodb.com/manual/reference/operator/query
        The following operations are supported:
        - `$and`
        - `$or`
        - `$nor`
        - `$eq`
        - `$ne`
        - `$gt`
        - `$gte`
        - `$lt`
        - `$lte`
        - `$in`
        - `$nin`
        - `$exists`
        - `$regex`


        Examples:
            Find runs in my_project where config.experiment_name has been set to "foo"
            ```
            api.runs(
                path="my_entity/my_project",
                filters={"config.experiment_name": "foo"},
            )
            ```

            Find runs in my_project where config.experiment_name has been set to "foo" or "bar"
            ```
            api.runs(
                path="my_entity/my_project",
                filters={
                    "$or": [
                        {"config.experiment_name": "foo"},
                        {"config.experiment_name": "bar"},
                    ]
                },
            )
            ```

            Find runs in my_project where config.experiment_name matches a regex (anchors are not supported)
            ```
            api.runs(
                path="my_entity/my_project",
                filters={"config.experiment_name": {"$regex": "b.*"}},
            )
            ```

            Find runs in my_project where the run name matches a regex (anchors are not supported)
            ```
            api.runs(
                path="my_entity/my_project",
                filters={"display_name": {"$regex": "^foo.*"}},
            )
            ```

            Find runs in my_project where config.experiment contains a nested field "category" with value "testing"
            ```
            api.runs(
                path="my_entity/my_project",
                filters={"config.experiment.category": "testing"},
            )
            ```

            Find runs in my_project with a loss value of 0.5 nested in a dictionary under model1 in the summary metrics
            ```
            api.runs(
                path="my_entity/my_project",
                filters={"summary_metrics.model1.loss": 0.5},
            )
            ```

            Find runs in my_project sorted by ascending loss
            ```
            api.runs(path="my_entity/my_project", order="+summary_metrics.loss")
            ```

        Args:
            path: (str) path to project, should be in the form: "entity/project"
            filters: (dict) queries for specific runs using the MongoDB query language.
                You can filter by run properties such as config.key, summary_metrics.key, state, entity, createdAt, etc.
                For example: `{"config.experiment_name": "foo"}` would find runs with a config entry
                    of experiment name set to "foo"
            order: (str) Order can be `created_at`, `heartbeat_at`, `config.*.value`, or `summary_metrics.*`.
                If you prepend order with a + order is ascending.
                If you prepend order with a - order is descending (default).
                The default order is run.created_at from oldest to newest.
            per_page: (int) Sets the page size for query pagination.
            include_sweeps: (bool) Whether to include the sweep runs in the results.

        Returns:
            A `Runs` object, which is an iterable collection of `Run` objects.
        """
        entity, project = self._parse_project_path(path)
        filters = filters or {}
        key = (path or "") + str(filters) + str(order)
        if not self._runs.get(key):
            self._runs[key] = public.Runs(
                self.client,
                entity,
                project,
                filters=filters,
                order=order,
                per_page=per_page,
                include_sweeps=include_sweeps,
            )
        return self._runs[key]

    @normalize_exceptions
    def run(self, path=""):
        """Return a single run by parsing path in the form entity/project/run_id.

        Args:
            path: (str) path to run in the form `entity/project/run_id`.
                If `api.entity` is set, this can be in the form `project/run_id`
                and if `api.project` is set this can just be the run_id.

        Returns:
            A `Run` object.
        """
        entity, project, run_id = self._parse_path(path)
        if not self._runs.get(path):
            self._runs[path] = public.Run(self.client, entity, project, run_id)
        return self._runs[path]

    def queued_run(
        self,
        entity,
        project,
        queue_name,
        run_queue_item_id,
        project_queue=None,
        priority=None,
    ):
        """Return a single queued run based on the path.

        Parses paths of the form entity/project/queue_id/run_queue_item_id.
        """
        return public.QueuedRun(
            self.client,
            entity,
            project,
            queue_name,
            run_queue_item_id,
            project_queue=project_queue,
            priority=priority,
        )

    def run_queue(
        self,
        entity,
        name,
    ):
        """Return the named `RunQueue` for entity.

        To create a new `RunQueue`, use `wandb.Api().create_run_queue(...)`.
        """
        return public.RunQueue(
            self.client,
            name,
            entity,
        )

    @normalize_exceptions
    def sweep(self, path=""):
        """Return a sweep by parsing path in the form `entity/project/sweep_id`.

        Args:
            path: (str, optional) path to sweep in the form entity/project/sweep_id.  If `api.entity`
                is set, this can be in the form project/sweep_id and if `api.project` is set
                this can just be the sweep_id.

        Returns:
            A `Sweep` object.
        """
        entity, project, sweep_id = self._parse_path(path)
        if not self._sweeps.get(path):
            self._sweeps[path] = public.Sweep(self.client, entity, project, sweep_id)
        return self._sweeps[path]

    @normalize_exceptions
    def artifact_types(self, project: Optional[str] = None) -> "public.ArtifactTypes":
        """Return a collection of matching artifact types.

        Args:
            project: (str, optional) If given, a project name or path to filter on.

        Returns:
            An iterable `ArtifactTypes` object.
        """
        project_path = project
        entity, project = self._parse_project_path(project_path)
        # If its a Registry project, the entity is considered to be an org instead
        if is_artifact_registry_project(project):
            settings_entity = self.settings["entity"] or self.default_entity
            org = parse_org_from_registry_path(project_path, PathType.PROJECT)
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=org
            )
        return public.ArtifactTypes(self.client, entity, project)

    @normalize_exceptions
    def artifact_type(
        self, type_name: str, project: Optional[str] = None
    ) -> "public.ArtifactType":
        """Return the matching `ArtifactType`.

        Args:
            type_name: (str) The name of the artifact type to retrieve.
            project: (str, optional) If given, a project name or path to filter on.

        Returns:
            An `ArtifactType` object.
        """
        project_path = project
        entity, project = self._parse_project_path(project_path)
        # If its an Registry artifact, the entity is an org instead
        if is_artifact_registry_project(project):
            org = parse_org_from_registry_path(project_path, PathType.PROJECT)
            settings_entity = self.settings["entity"] or self.default_entity
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=org
            )
        return public.ArtifactType(self.client, entity, project, type_name)

    @normalize_exceptions
    def artifact_collections(
        self, project_name: str, type_name: str, per_page: int = 50
    ) -> "public.ArtifactCollections":
        """Return a collection of matching artifact collections.

        Args:
            project_name: (str) The name of the project to filter on.
            type_name: (str) The name of the artifact type to filter on.
            per_page: (int) Sets the page size for query pagination.  Usually there is no reason to change this.

        Returns:
            An iterable `ArtifactCollections` object.
        """
        entity, project = self._parse_project_path(project_name)
        # If iterating through Registry project, the entity is considered to be an org instead
        if is_artifact_registry_project(project):
            org = parse_org_from_registry_path(project_name, PathType.PROJECT)
            settings_entity = self.settings["entity"] or self.default_entity
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=org
            )
        return public.ArtifactCollections(
            self.client, entity, project, type_name, per_page=per_page
        )

    @normalize_exceptions
    def artifact_collection(
        self, type_name: str, name: str
    ) -> "public.ArtifactCollection":
        """Return a single artifact collection by type and parsing path in the form `entity/project/name`.

        Args:
            type_name: (str) The type of artifact collection to fetch.
            name: (str) An artifact collection name. May be prefixed with entity/project.

        Returns:
            An `ArtifactCollection` object.
        """
        entity, project, collection_name = self._parse_artifact_path(name)
        # If its an Registry artifact, the entity is considered to be an org instead
        if is_artifact_registry_project(project):
            org = parse_org_from_registry_path(name, PathType.ARTIFACT)
            settings_entity = self.settings["entity"] or self.default_entity
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=org
            )

        if entity is None:
            raise ValueError(
                "Could not determine entity. Please include the entity as part of the collection name path."
            )

        return public.ArtifactCollection(
            self.client, entity, project, collection_name, type_name
        )

    @normalize_exceptions
    def artifact_versions(self, type_name, name, per_page=50):
        """Deprecated, use `artifacts(type_name, name)` instead."""
        deprecate(
            field_name=Deprecated.api__artifact_versions,
            warning_message=(
                "Api.artifact_versions(type_name, name) is deprecated, "
                "use Api.artifacts(type_name, name) instead."
            ),
        )
        return self.artifacts(type_name, name, per_page=per_page)

    @normalize_exceptions
    def artifacts(
        self,
        type_name: str,
        name: str,
        per_page: int = 50,
        tags: Optional[List[str]] = None,
    ) -> "public.Artifacts":
        """Return an `Artifacts` collection from the given parameters.

        Args:
            type_name: (str) The type of artifacts to fetch.
            name: (str) An artifact collection name. May be prefixed with entity/project.
            per_page: (int) Sets the page size for query pagination.  Usually there is no reason to change this.
            tags: (list[str], optional) Only return artifacts with all of these tags.

        Returns:
            An iterable `Artifacts` object.
        """
        entity, project, collection_name = self._parse_artifact_path(name)
        # If its an Registry project, the entity is considered to be an org instead
        if is_artifact_registry_project(project):
            org = parse_org_from_registry_path(name, PathType.ARTIFACT)
            settings_entity = self.settings["entity"] or self.default_entity
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=org
            )
        return public.Artifacts(
            self.client,
            entity,
            project,
            collection_name,
            type_name,
            per_page=per_page,
            tags=tags,
        )

    @normalize_exceptions
    def _artifact(
        self, name: str, type: Optional[str] = None, enable_tracking: bool = False
    ):
        if name is None:
            raise ValueError("You must specify name= to fetch an artifact.")
        entity, project, artifact_name = self._parse_artifact_path(name)

        # If its an Registry artifact, the entity is an org instead
        if is_artifact_registry_project(project):
            organization = (
                name.split("/")[0]
                if name.count("/") == 2
                else self.settings["organization"]
            )
            # set entity to match the settings since in above code it was potentially set to an org
            settings_entity = self.settings["entity"] or self.default_entity
            # Registry artifacts are under the org entity. Because we offer a shorthand and alias for this path,
            # we need to fetch the org entity to for the user behind the scenes.
            entity = InternalApi()._resolve_org_entity_name(
                entity=settings_entity, organization=organization
            )

        if entity is None:
            raise ValueError(
                "Could not determine entity. Please include the entity as part of the artifact name path."
            )

        artifact = wandb.Artifact._from_name(
            entity=entity,
            project=project,
            name=artifact_name,
            client=self.client,
            enable_tracking=enable_tracking,
        )
        if type is not None and artifact.type != type:
            raise ValueError(
                f"type {type} specified but this artifact is of type {artifact.type}"
            )
        return artifact

    @normalize_exceptions
    def artifact(self, name: str, type: Optional[str] = None):
        """Return a single artifact by parsing path in the form `project/name` or `entity/project/name`.

        Args:
            name: (str) An artifact name. May be prefixed with project/ or entity/project/.
                    If no entity is specified in the name, the Run or API setting's entity is used.
                Valid names can be in the following forms:
                    name:version
                    name:alias
            type: (str, optional) The type of artifact to fetch.

        Returns:
            An `Artifact` object.

        Raises:
            ValueError: If the artifact name is not specified.
            ValueError: If the artifact type is specified but does not match the type of the fetched artifact.

        Note:
        This method is intended for external use only. Do not call `api.artifact()` within the wandb repository code.
        """
        return self._artifact(name=name, type=type, enable_tracking=True)

    @normalize_exceptions
    def job(self, name: Optional[str], path: Optional[str] = None) -> "public.Job":
        """Return a `Job` from the given parameters.

        Args:
            name: (str) The job name.
            path: (str, optional) If given, the root path in which to download the job artifact.

        Returns:
            A `Job` object.
        """
        if name is None:
            raise ValueError("You must specify name= to fetch a job.")
        elif name.count("/") != 2 or ":" not in name:
            raise ValueError(
                "Invalid job specification. A job must be of the form: <entity>/<project>/<job-name>:<alias-or-version>"
            )
        return public.Job(self, name, path)

    @normalize_exceptions
    def list_jobs(self, entity: str, project: str) -> List[Dict[str, Any]]:
        """Return a list of jobs, if any, for the given entity and project.

        Args:
            entity: (str) The entity for the listed job(s).
            project: (str) The project for the listed job(s).

        Returns:
            A list of matching jobs.
        """
        if entity is None:
            raise ValueError("Specify an entity when listing jobs")
        if project is None:
            raise ValueError("Specify a project when listing jobs")

        query = gql(
            """
        query ArtifactOfType(
            $entityName: String!,
            $projectName: String!,
            $artifactTypeName: String!,
        ) {
            project(name: $projectName, entityName: $entityName) {
                artifactType(name: $artifactTypeName) {
                    artifactCollections {
                        edges {
                            node {
                                artifacts {
                                    edges {
                                        node {
                                            id
                                            state
                                            aliases {
                                                alias
                                            }
                                            artifactSequence {
                                                name
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        """
        )

        try:
            artifact_query = self._client.execute(
                query,
                {
                    "projectName": project,
                    "entityName": entity,
                    "artifactTypeName": "job",
                },
            )

            if not artifact_query or not artifact_query["project"]:
                wandb.termerror(
                    f"Project: '{project}' not found in entity: '{entity}' or access denied."
                )
                return []

            if artifact_query["project"]["artifactType"] is None:
                return []

            artifacts = artifact_query["project"]["artifactType"][
                "artifactCollections"
            ]["edges"]

            return [x["node"]["artifacts"] for x in artifacts]
        except requests.exceptions.HTTPError:
            return False

    @normalize_exceptions
    def artifact_exists(self, name: str, type: Optional[str] = None) -> bool:
        """Return whether an artifact version exists within a specified project and entity.

        Args:
            name: (str) An artifact name. May be prefixed with entity/project.
                If entity or project is not specified, it will be inferred from the override params if populated.
                Otherwise, entity will be pulled from the user settings and project will default to "uncategorized".
                Valid names can be in the following forms:
                    name:version
                    name:alias
            type: (str, optional) The type of artifact

        Returns:
            True if the artifact version exists, False otherwise.
        """
        try:
            self._artifact(name, type)
        except wandb.errors.CommError:
            return False

        return True

    @normalize_exceptions
    def artifact_collection_exists(self, name: str, type: str) -> bool:
        """Return whether an artifact collection exists within a specified project and entity.

        Args:
            name: (str) An artifact collection name. May be prefixed with entity/project.
                If entity or project is not specified, it will be inferred from the override params if populated.
                Otherwise, entity will be pulled from the user settings and project will default to "uncategorized".
            type: (str) The type of artifact collection

        Returns:
            True if the artifact collection exists, False otherwise.
        """
        try:
            self.artifact_collection(type, name)
        except wandb.errors.CommError:
            return False

        return True

    def registries(
        self,
        organization: Optional[str] = None,
        filter: Optional[Dict[str, Any]] = None,
    ) -> Registries:
        """Returns a Registry iterator.

        Use the iterator to search and filter registries, collections,
        or artifact versions across your organization's registry.

        Examples:
            Find all registries with the names that contain "model"
            ```python
            import wandb

            api = wandb.Api()  # specify an org if your entity belongs to multiple orgs
            api.registries(filter={"name": {"$regex": "model"}})
            ```

            Find all collections in the registries with the name "my_collection" and the tag "my_tag"
            ```python
            api.registries().collections(filter={"name": "my_collection", "tag": "my_tag"})
            ```

            Find all artifact versions in the registries with a collection name that contains "my_collection" and a version that has the alias "best"
            ```python
            api.registries().collections(
                filter={"name": {"$regex": "my_collection"}}
            ).versions(filter={"alias": "best"})
            ```

            Find all artifact versions in the registries that contain "model" and have the tag "prod" or alias "best"
            ```python
            api.registries(filter={"name": {"$regex": "model"}}).versions(
                filter={"$or": [{"tag": "prod"}, {"alias": "best"}]}
            )
            ```

        Args:
            organization: (str, optional) The organization of the registry to fetch.
                If not specified, use the organization specified in the user's settings.
            filter: (dict, optional) MongoDB-style filter to apply to each object in the registry iterator.
                Fields available to filter for collections are
                    `name`, `description`, `created_at`, `updated_at`.
                Fields available to filter for collections are
                    `name`, `tag`, `description`, `created_at`, `updated_at`
                Fields available to filter for versions are
                    `tag`, `alias`, `created_at`, `updated_at`, `metadata`

        Returns:
            A registry iterator.
        """
        if not InternalApi()._server_supports(ServerFeature.ARTIFACT_REGISTRY_SEARCH):
            raise RuntimeError(
                "Registry search API is not enabled on this wandb server version. "
                "Please upgrade your server version or contact support at support@wandb.com."
            )

        organization = organization or fetch_org_from_settings_or_entity(
            self.settings, self.default_entity
        )
        return Registries(self.client, organization, filter)

    def registry(self, name: str, organization: Optional[str] = None) -> Registry:
        """Return a registry given a registry name.

        Args:
            name: The name of the registry. This is without the `wandb-registry-`
                prefix.
            organization: The organization of the registry.
                If no organization is set in the settings, the organization will be
                fetched from the entity if the entity only belongs to one
                organization.

        Returns:
            A registry object.

        Examples:
            Fetch and update a registry
            ```python
            import wandb

            api = wandb.Api()
            registry = api.registry(name="my-registry", organization="my-org")
            registry.description = "This is an updated description"
            registry.save()
            ```
        """
        if not InternalApi()._server_supports(ServerFeature.ARTIFACT_REGISTRY_SEARCH):
            raise RuntimeError(
                "api.registry() is not enabled on this wandb server version. "
                "Please upgrade your server version or contact support at support@wandb.com."
            )
        organization = organization or fetch_org_from_settings_or_entity(
            self.settings, self.default_entity
        )
        org_entity = _fetch_org_entity_from_organization(self.client, organization)
        registry = Registry(self.client, organization, org_entity, name)
        registry.load()
        return registry

    def create_registry(
        self,
        name: str,
        visibility: Literal["organization", "restricted"],
        organization: Optional[str] = None,
        description: Optional[str] = None,
        artifact_types: Optional[List[str]] = None,
    ) -> Registry:
        """Create a new registry.

        Args:
            name: The name of the registry. Name must be unique within the organization.
            visibility: The visibility of the registry.
                organization: Anyone in the organization can view this registry. You can
                    edit their roles later from the settings in the UI.
                restricted: Only invited members via the UI can access this registry.
                    Public sharing is disabled.
            organization: The organization of the registry.
                If no organization is set in the settings, the organization will be
                fetched from the entity if the entity only belongs to one organization.
            description: The description of the registry.
            artifact_types: The accepted artifact types of the registry. A type is no
                more than 128 characters and do not include characters `/` or `:`. If
                not specified, all types are accepted.
                Allowed types added to the registry cannot be removed later.

        Returns:
            A registry object.

        Examples:
            ```python
            import wandb

            api = wandb.Api()
            registry = api.create_registry(
                name="my-registry",
                visibility="restricted",
                organization="my-org",
                description="This is a test registry",
                artifact_types=["model"],
            )
            ```
        """
        if not InternalApi()._server_supports(
            ServerFeature.INCLUDE_ARTIFACT_TYPES_IN_REGISTRY_CREATION
        ):
            raise RuntimeError(
                "create_registry api is not enabled on this wandb server version. "
                "Please upgrade your server version or contact support at support@wandb.com."
            )

        organization = organization or fetch_org_from_settings_or_entity(
            self.settings, self.default_entity
        )

        try:
            existing_registry = self.registry(name=name, organization=organization)
        except ValueError:
            existing_registry = None
        if existing_registry:
            raise ValueError(
                f"Registry {name!r} already exists in organization {organization!r},"
                " please use a different name."
            )

        return Registry.create(
            self.client,
            organization,
            name,
            visibility,
            description,
            artifact_types,
        )

    def integrations(
        self,
        entity: Optional[str] = None,
        *,
        per_page: int = 50,
    ) -> Iterator["Integration"]:
        """Return an iterator of all integrations for an entity.

        Args:
            entity: The entity (e.g. team name) for which to
                fetch integrations.  If not provided, the user's default entity
                will be used.
            per_page: Number of integrations to fetch per page.
                Defaults to 50.  Usually there is no reason to change this.

        Yields:
            Iterator[SlackIntegration | WebhookIntegration]: An iterator of any supported integrations.
        """
        from wandb.apis.public.integrations import Integrations

        params = {"entityName": entity or self.default_entity}
        return Integrations(client=self.client, variables=params, per_page=per_page)

    def webhook_integrations(
        self, entity: Optional[str] = None, *, per_page: int = 50
    ) -> Iterator["WebhookIntegration"]:
        """Returns an iterator of webhook integrations for an entity.

        Args:
            entity: The entity (e.g. team name) for which to
                fetch integrations.  If not provided, the user's default entity
                will be used.
            per_page: Number of integrations to fetch per page.
                Defaults to 50.  Usually there is no reason to change this.

        Yields:
            Iterator[WebhookIntegration]: An iterator of webhook integrations.

        Examples:
            Get all registered webhook integrations for the team "my-team":
            ```python
            import wandb

            api = wandb.Api()
            webhook_integrations = api.webhook_integrations(entity="my-team")
            ```

            Find only webhook integrations that post requests to "https://my-fake-url.com":
            ```python
            webhook_integrations = api.webhook_integrations(entity="my-team")
            my_webhooks = [
                ig
                for ig in webhook_integrations
                if ig.url_endpoint.startswith("https://my-fake-url.com")
            ]
            ```
        """
        from wandb.apis.public.integrations import WebhookIntegrations

        params = {"entityName": entity or self.default_entity}
        return WebhookIntegrations(
            client=self.client, variables=params, per_page=per_page
        )

    def slack_integrations(
        self, *, entity: Optional[str] = None, per_page: int = 50
    ) -> Iterator["SlackIntegration"]:
        """Returns an iterator of Slack integrations for an entity.

        Args:
            entity: The entity (e.g. team name) for which to
                fetch integrations.  If not provided, the user's default entity
                will be used.
            per_page: Number of integrations to fetch per page.
                Defaults to 50.  Usually there is no reason to change this.

        Yields:
            Iterator[SlackIntegration]: An iterator of Slack integrations.

        Examples:
            Get all registered Slack integrations for the team "my-team":
            ```python
            import wandb

            api = wandb.Api()
            slack_integrations = api.slack_integrations(entity="my-team")
            ```

            Find only Slack integrations that post to channel names starting with "team-alerts-":
            ```python
            slack_integrations = api.slack_integrations(entity="my-team")
            team_alert_integrations = [
                ig
                for ig in slack_integrations
                if ig.channel_name.startswith("team-alerts-")
            ]
            ```
        """
        from wandb.apis.public.integrations import SlackIntegrations

        params = {"entityName": entity or self.default_entity}
        return SlackIntegrations(
            client=self.client, variables=params, per_page=per_page
        )

    def _supports_automation(
        self,
        *,
        event: Optional["EventType"] = None,
        action: Optional["ActionType"] = None,
    ) -> bool:
        """Returns whether the server recognizes the automation event and/or action."""
        from wandb.automations._utils import (
            ALWAYS_SUPPORTED_ACTIONS,
            ALWAYS_SUPPORTED_EVENTS,
        )

        api = InternalApi()
        supports_event = (
            (event is None)
            or (event in ALWAYS_SUPPORTED_EVENTS)
            or api._server_supports(f"AUTOMATION_EVENT_{event.value}")
        )
        supports_action = (
            (action is None)
            or (action in ALWAYS_SUPPORTED_ACTIONS)
            or api._server_supports(f"AUTOMATION_ACTION_{action.value}")
        )
        return supports_event and supports_action

    def _omitted_automation_fragments(self) -> Set[str]:
        """Returns the names of unsupported automation-related fragments.

        Older servers won't recognize newer GraphQL types, so a valid request may
        unnecessarily error out because it won't recognize fragments defined on those types.

        So e.g. if a server does not support `NO_OP` action types, then the following need to be
        removed from the body of the GraphQL request:

            - Fragment definition:
                ```
                fragment NoOpActionFields on NoOpTriggeredAction {
                    noOp
                }
                ```

            - Fragment spread in selection set:
                ```
                {
                    ...NoOpActionFields
                    # ... other fields ...
                }
                ```
        """
        from wandb.automations import ActionType
        from wandb.automations._generated import (
            GenericWebhookActionFields,
            NoOpActionFields,
            NotificationActionFields,
            QueueJobActionFields,
        )

        # Note: we can't currently define this as a constant outside the method
        # and still keep it nearby in this module, because it relies on pydantic v2-only imports
        fragment_names: dict[ActionType, str] = {
            ActionType.NO_OP: NoOpActionFields.__name__,
            ActionType.QUEUE_JOB: QueueJobActionFields.__name__,
            ActionType.NOTIFICATION: NotificationActionFields.__name__,
            ActionType.GENERIC_WEBHOOK: GenericWebhookActionFields.__name__,
        }

        return set(
            name
            for action in ActionType
            if (not self._supports_automation(action=action))
            and (name := fragment_names.get(action))
        )

    def automation(
        self,
        name: str,
        *,
        entity: Optional[str] = None,
    ) -> "Automation":
        """Returns the only Automation matching the parameters.

        Args:
            name: The name of the automation to fetch.
            entity: The entity to fetch the automation for.

        Raises:
            ValueError: If zero or multiple Automations match the search criteria.

        Examples:
            Get an existing automation named "my-automation":

            ```python
            import wandb

            api = wandb.Api()
            automation = api.automation(name="my-automation")
            ```

            Get an existing automation named "other-automation", from the entity "my-team":

            ```python
            automation = api.automation(name="other-automation", entity="my-team")
            ```
        """
        return one(
            self.automations(entity=entity, name=name),
            too_short=ValueError("No automations found"),
            too_long=ValueError("Multiple automations found"),
        )

    def automations(
        self,
        entity: Optional[str] = None,
        *,
        name: Optional[str] = None,
        per_page: int = 50,
    ) -> Iterator["Automation"]:
        """Returns an iterator over all Automations that match the given parameters.

        If no parameters are provided, the returned iterator will contain all
        Automations that the user has access to.

        Args:
            entity: The entity to fetch the automations for.
            name: The name of the automation to fetch.
            per_page: The number of automations to fetch per page.
                Defaults to 50.  Usually there is no reason to change this.

        Returns:
            A list of automations.

        Examples:
            Fetch all existing automations for the entity "my-team":

            ```python
            import wandb

            api = wandb.Api()
            automations = api.automations(entity="my-team")
            ```
        """
        from wandb.apis.public.automations import Automations
        from wandb.automations._generated import (
            GET_AUTOMATIONS_BY_ENTITY_GQL,
            GET_AUTOMATIONS_GQL,
        )

        # For now, we need to use different queries depending on whether entity is given
        variables = {"entityName": entity}
        if entity is None:
            gql_str = GET_AUTOMATIONS_GQL  # Automations for viewer
        else:
            gql_str = GET_AUTOMATIONS_BY_ENTITY_GQL  # Automations for entity

        # If needed, rewrite the GraphQL field selection set to omit unsupported fields/fragments/types
        omit_fragments = self._omitted_automation_fragments()
        query = gql_compat(gql_str, omit_fragments=omit_fragments)
        iterator = Automations(
            client=self.client, variables=variables, per_page=per_page, _query=query
        )

        # FIXME: this is crude, move this client-side filtering logic into backend
        if name is not None:
            iterator = filter(lambda x: x.name == name, iterator)
        yield from iterator

    @normalize_exceptions
    def create_automation(
        self,
        obj: "NewAutomation",
        *,
        fetch_existing: bool = False,
        **kwargs: Unpack["WriteAutomationsKwargs"],
    ) -> "Automation":
        """Create a new Automation.

        Args:
            obj:
                The automation to create.
            fetch_existing:
                If True, and a conflicting automation already exists, attempt
                to fetch the existing automation instead of raising an error.
            **kwargs:
                Any additional values to assign to the automation before
                creating it.  If given, these will override any values that may
                already be set on the automation:
                - `name`: The name of the automation.
                - `description`: The description of the automation.
                - `enabled`: Whether the automation is enabled.
                - `scope`: The scope of the automation.
                - `event`: The event that triggers the automation.
                - `action`: The action that is triggered by the automation.

        Returns:
            The saved Automation.

        Examples:
            Create a new automation named "my-automation" that sends a Slack notification
            when a run within a specific project logs a metric exceeding a custom threshold:

            ```python
            import wandb
            from wandb.automations import OnRunMetric, RunEvent, SendNotification

            api = wandb.Api()

            project = api.project("my-project", entity="my-team")

            # Use the first Slack integration for the team
            slack_hook = next(api.slack_integrations(entity="my-team"))

            event = OnRunMetric(
                scope=project,
                filter=RunEvent.metric("custom-metric") > 10,
            )
            action = SendNotification.from_integration(slack_hook)

            automation = api.create_automation(
                event >> action,
                name="my-automation",
                description="Send a Slack message whenever 'custom-metric' exceeds 10.",
            )
            ```
        """
        from wandb.automations import Automation
        from wandb.automations._generated import CREATE_AUTOMATION_GQL, CreateAutomation
        from wandb.automations._utils import prepare_to_create

        gql_input = prepare_to_create(obj, **kwargs)

        if not self._supports_automation(
            event=(event := gql_input.triggering_event_type),
            action=(action := gql_input.triggered_action_type),
        ):
            raise ValueError(
                f"Automation event or action ({event!r} -> {action!r}) "
                "is not supported on this wandb server version. "
                "Please upgrade your server version, or contact support at "
                "support@wandb.com."
            )

        # If needed, rewrite the GraphQL field selection set to omit unsupported fields/fragments/types
        omit_fragments = self._omitted_automation_fragments()
        mutation = gql_compat(CREATE_AUTOMATION_GQL, omit_fragments=omit_fragments)
        variables = {"params": gql_input.model_dump(exclude_none=True)}

        name = gql_input.name
        try:
            data = self.client.execute(mutation, variable_values=variables)
        except requests.HTTPError as e:
            status = HTTPStatus(e.response.status_code)
            if status is HTTPStatus.CONFLICT:  # 409
                if fetch_existing:
                    wandb.termlog(f"Automation {name!r} exists. Fetching it instead.")
                    return self.automation(name=name)

                raise ValueError(
                    f"Automation {name!r} exists. Unable to create another with the same name."
                ) from None
            raise

        try:
            result = CreateAutomation.model_validate(data).result
        except ValidationError as e:
            msg = f"Invalid response while creating automation {name!r}"
            raise RuntimeError(msg) from e

        if (result is None) or (result.trigger is None):
            msg = f"Empty response while creating automation {name!r}"
            raise RuntimeError(msg)

        return Automation.model_validate(result.trigger)

    @normalize_exceptions
    def update_automation(
        self,
        obj: "Automation",
        *,
        create_missing: bool = False,
        **kwargs: Unpack["WriteAutomationsKwargs"],
    ) -> "Automation":
        """Update an existing automation.

        Args:
            obj: The automation to update.  Must be an existing automation.
            create_missing (bool):
                If True, and the automation does not exist, create it.
            **kwargs:
                Any additional values to assign to the automation before
                updating it.  If given, these will override any values that may
                already be set on the automation:
                - `name`: The name of the automation.
                - `description`: The description of the automation.
                - `enabled`: Whether the automation is enabled.
                - `scope`: The scope of the automation.
                - `event`: The event that triggers the automation.
                - `action`: The action that is triggered by the automation.

        Returns:
            The updated automation.

        Examples:
            Disable and edit the description of an existing automation ("my-automation"):

            ```python
            import wandb

            api = wandb.Api()

            automation = api.automation(name="my-automation")
            automation.enabled = False
            automation.description = "Kept for reference, but no longer used."

            updated_automation = api.update_automation(automation)
            ```

            OR:

            ```python
            import wandb

            api = wandb.Api()

            automation = api.automation(name="my-automation")

            updated_automation = api.update_automation(
                automation,
                enabled=False,
                description="Kept for reference, but no longer used.",
            )
            ```
        """
        from wandb.automations import ActionType, Automation
        from wandb.automations._generated import UPDATE_AUTOMATION_GQL, UpdateAutomation
        from wandb.automations._utils import prepare_to_update

        # Check if the server even supports updating automations.
        #
        # NOTE: Unfortunately, there is no current server feature flag for this.  As a workaround,
        # we check whether the server supports the NO_OP action, which is a reasonably safe proxy
        # for whether it supports updating automations.
        if not self._supports_automation(action=ActionType.NO_OP):
            raise RuntimeError(
                "Updating existing automations is not enabled on this wandb server version. "
                "Please upgrade your server version, or contact support at support@wandb.com."
            )

        gql_input = prepare_to_update(obj, **kwargs)

        if not self._supports_automation(
            event=(event := gql_input.triggering_event_type),
            action=(action := gql_input.triggered_action_type),
        ):
            raise ValueError(
                f"Automation event or action ({event.value} -> {action.value}) "
                "is not supported on this wandb server version. "
                "Please upgrade your server version, or contact support at "
                "support@wandb.com."
            )

        # If needed, rewrite the GraphQL field selection set to omit unsupported fields/fragments/types
        omit_fragments = self._omitted_automation_fragments()
        mutation = gql_compat(UPDATE_AUTOMATION_GQL, omit_fragments=omit_fragments)
        variables = {"params": gql_input.model_dump(exclude_none=True)}

        name = gql_input.name
        try:
            data = self.client.execute(mutation, variable_values=variables)
        except requests.HTTPError as e:
            status = HTTPStatus(e.response.status_code)
            if status is HTTPStatus.NOT_FOUND:  # 404
                if create_missing:
                    wandb.termlog(f"Automation {name!r} not found. Creating it.")
                    return self.create_automation(obj)

                raise ValueError(
                    f"Automation {name!r} not found. Unable to edit it."
                ) from e

            # Not a (known) recoverable HTTP error
            wandb.termerror(f"Got response status {status!r}: {e.response.text!r}")
            raise

        try:
            result = UpdateAutomation.model_validate(data).result
        except ValidationError as e:
            msg = f"Invalid response while updating automation {name!r}"
            raise RuntimeError(msg) from e

        if (result is None) or (result.trigger is None):
            msg = f"Empty response while updating automation {name!r}"
            raise RuntimeError(msg)

        return Automation.model_validate(result.trigger)

    @normalize_exceptions
    def delete_automation(self, obj: Union["Automation", str]) -> Literal[True]:
        """Delete an automation.

        Args:
            obj: The automation to delete, or its ID.

        Returns:
            True if the automation was deleted successfully.
        """
        from wandb.automations._generated import DELETE_AUTOMATION_GQL, DeleteAutomation
        from wandb.automations._utils import extract_id

        id_ = extract_id(obj)
        mutation = gql(DELETE_AUTOMATION_GQL)
        variables = {"id": id_}

        data = self.client.execute(mutation, variable_values=variables)

        try:
            result = DeleteAutomation.model_validate(data).result
        except ValidationError as e:
            msg = f"Invalid response while deleting automation {id_!r}"
            raise RuntimeError(msg) from e

        if result is None:
            msg = f"Empty response while deleting automation {id_!r}"
            raise RuntimeError(msg)

        if not result.success:
            raise RuntimeError(f"Failed to delete automation: {id_!r}")

        return result.success