Skip to content

Commit edc851c

Browse files
committed
fix(instance-preferences): stop pydantic serializer warnings for unset preference fields
Compute's validator converts Unassigned() to None on its own fields but not on the nested InstancePreference objects, so every submit with a preference list emitted one PydanticSerializationUnexpectedValue warning per element (for the unset training_plan_arns list) when _to_resource_config() called model_dump(). The request payload was already correct; the noise was the only effect. Both Compute classes now normalise each preference the same way. The serialized ResourceConfig is unchanged. A test asserts _to_resource_config() completes with warnings turned into errors.
1 parent f883b2f commit edc851c

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎sagemaker-core/src/sagemaker/core/modules/configs.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class Compute(shapes.ResourceConfig):
166166
def _model_validator(self) -> "Compute":
167167
"""Convert Unassigned values to None and validate instance_preferences."""
168168
converted = convert_unassigned_to_none(self)
169+
# Nested preferences keep Unassigned() on unset fields, which model_dump
170+
# flags per element; normalise them the same way as the top level.
171+
for preference in converted.instance_preferences or ():
172+
convert_unassigned_to_none(preference)
169173
validate_instance_preferences(converted)
170174
return converted
171175

‎sagemaker-core/src/sagemaker/core/training/configs.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ class Compute(shapes.ResourceConfig):
194194
def _model_validator(self) -> "Compute":
195195
"""Convert Unassigned values to None and validate instance_preferences."""
196196
converted = convert_unassigned_to_none(self)
197+
# Nested preferences keep Unassigned() on unset fields, which model_dump
198+
# flags per element; normalise them the same way as the top level.
199+
for preference in converted.instance_preferences or ():
200+
convert_unassigned_to_none(preference)
197201
validate_instance_preferences(converted)
198202
return converted
199203

‎sagemaker-core/tests/unit/test_compute_configs.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""Unit tests for Compute and HyperPodCompute config classes."""
22

3+
import warnings
4+
35
import pytest
6+
from sagemaker.core.modules.configs import Compute as ModulesCompute
7+
from sagemaker.core.shapes import InstancePreference
48
from sagemaker.core.training.configs import Compute, HyperPodCompute
59

610

@@ -337,3 +341,25 @@ def test_processing_cluster_config_single_type_still_works(self):
337341
)
338342
assert pcc.instance_type == "ml.m5.xlarge"
339343
assert pcc.instance_count == 1
344+
345+
346+
class TestInstancePreferencesSerialization:
347+
"""Unset fields on nested preferences must not surface as pydantic
348+
serializer warnings on every submit, and the request payload must omit them."""
349+
350+
@pytest.mark.parametrize("compute_cls", [Compute, ModulesCompute])
351+
def test_to_resource_config_emits_no_serializer_warning(self, compute_cls):
352+
compute = compute_cls(
353+
instance_preferences=[
354+
InstancePreference(instance_type="ml.m5.large"),
355+
InstancePreference(instance_type="ml.m5.xlarge"),
356+
],
357+
instance_count=1,
358+
)
359+
with warnings.catch_warnings():
360+
warnings.simplefilter("error")
361+
resource_config = compute._to_resource_config()
362+
prefs = resource_config.instance_preferences
363+
assert [p.instance_type for p in prefs] == ["ml.m5.large", "ml.m5.xlarge"]
364+
assert all(p.training_plan_arns is None and p.instance_count is None for p in prefs)
365+
assert resource_config.instance_count == 1

0 commit comments

Comments
 (0)