Skip to content

Commit 82bd803

Browse files
committed
fix: handle trial component status message longer than API supports
1 parent fd566bd commit 82bd803

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/sagemaker/experiments/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
TRIAL_NAME_TEMPLATE = "Default-Run-Group-{}"
6969
MAX_RUN_TC_ARTIFACTS_LEN = 30
7070
MAX_NAME_LEN_IN_BACKEND = 120
71+
MAX_STATUS_MESSAGE_LEN = 1024
7172
EXPERIMENT_NAME = "ExperimentName"
7273
TRIAL_NAME = "TrialName"
7374
RUN_NAME = "RunName"
@@ -759,7 +760,7 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
759760
if exc_value:
760761
self._trial_component.status = _api_types.TrialComponentStatus(
761762
primary_status=_TrialComponentStatusType.Failed.value,
762-
message=str(exc_value),
763+
message=(str(exc_value) or "")[:MAX_STATUS_MESSAGE_LEN],
763764
)
764765
else:
765766
self._trial_component.status = _api_types.TrialComponentStatus(

tests/unit/sagemaker/experiments/test_run.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,22 @@ def test_exit_fail(sagemaker_session, run_obj):
10781078
assert isinstance(run_obj._trial_component.end_time, datetime.datetime)
10791079

10801080

1081+
def test_exit_fail_message_too_long(sagemaker_session, run_obj):
1082+
sagemaker_session.sagemaker_client.update_trial_component.return_value = {}
1083+
# create an error message that is longer than the max status message length of 1024
1084+
# 3 x 342 = 1026
1085+
too_long_error_message = "Foo" * 342
1086+
try:
1087+
with run_obj:
1088+
raise ValueError(too_long_error_message)
1089+
except ValueError:
1090+
pass
1091+
1092+
assert run_obj._trial_component.status.primary_status == _TrialComponentStatusType.Failed.value
1093+
assert run_obj._trial_component.status.message == too_long_error_message[:1024]
1094+
assert isinstance(run_obj._trial_component.end_time, datetime.datetime)
1095+
1096+
10811097
@pytest.mark.parametrize(
10821098
"metric_value",
10831099
[1.3, "nan", "inf", "-inf", None],

0 commit comments

Comments
 (0)