@@ -23,6 +23,62 @@ def __new__(cls, identifier: str | None = None, **kwargs):
2323 else :
2424 return ObjectArtifact (identifier = identifier , ** kwargs )
2525
26+ @classmethod
27+ def from_run (
28+ cls ,
29+ run_id : str ,
30+ category : typing .Literal ["input" , "output" , "code" ] | None = None ,
31+ ** kwargs ,
32+ ) -> typing .Generator [tuple [str , FileArtifact | ObjectArtifact ], None , None ]:
33+ """Return artifacts associated with a given run.
34+
35+ Parameters
36+ ----------
37+ run_id : str
38+ The ID of the run to retriece artifacts from
39+ category : typing.Literal["input", "output", "code"] | None, optional
40+ The category of artifacts to return, by default all artifacts are returned
41+
42+ Returns
43+ -------
44+ typing.Generator[tuple[str, FileArtifact | ObjectArtifact], None, None]
45+ The artifacts
46+
47+ Yields
48+ ------
49+ Iterator[typing.Generator[tuple[str, FileArtifact | ObjectArtifact], None, None]]
50+ identifier for artifact
51+ the artifact itself as a class instance
52+
53+ Raises
54+ ------
55+ ObjectNotFoundError
56+ Raised if artifacts could not be found for that run
57+ """
58+ _temp = ArtifactBase (** kwargs )
59+ _url = URL (_temp ._user_config .server .url ) / f"runs/{ run_id } /artifacts"
60+ _response = sv_get (
61+ url = f"{ _url } " , params = {"category" : category }, headers = _temp ._headers
62+ )
63+ _json_response = get_json_from_response (
64+ expected_type = list ,
65+ response = _response ,
66+ expected_status = [http .HTTPStatus .OK , http .HTTPStatus .NOT_FOUND ],
67+ scenario = f"Retrieval of artifacts for run '{ run_id } '" ,
68+ )
69+
70+ if _response .status_code == http .HTTPStatus .NOT_FOUND or not _json_response :
71+ raise ObjectNotFoundError (
72+ _temp ._label , category , extra = f"for run '{ run_id } '"
73+ )
74+
75+ for _entry in _json_response :
76+ _id = _entry .pop ("id" )
77+ yield (
78+ _id ,
79+ Artifact (_local = True , _read_only = True , identifier = _id , ** _entry ),
80+ )
81+
2682 @classmethod
2783 def from_name (
2884 cls , run_id : str , name : str , ** kwargs
@@ -99,21 +155,9 @@ def get(
99155 if (_data := _json_response .get ("data" )) is None :
100156 raise RuntimeError (f"Expected key 'data' for retrieval of { _label } s" )
101157
102- _out_dict : dict [str , FileArtifact | ObjectArtifact ] = {}
103-
104158 for _entry in _data :
105159 _id = _entry .pop ("id" )
106- if _entry ["original_path" ]:
107- yield (
108- _id ,
109- FileArtifact (
110- _local = True , _read_only = True , identifier = _id , ** _entry
111- ),
112- )
113- else :
114- yield (
115- _id ,
116- ObjectArtifact (
117- _local = True , _read_only = True , identifier = _id , ** _entry
118- ),
119- )
160+ yield (
161+ _id ,
162+ Artifact (_local = True , _read_only = True , identifier = _id , ** _entry ),
163+ )
0 commit comments