fix: ModelTrainer and HyperparameterTuner missing environment variables (5613)#5725
Conversation
sagemaker-bot
left a comment
There was a problem hiding this comment.
🤖 AI Code Review
This PR fixes a bug where environment variables from ModelTrainer were not being propagated to the HyperParameterTuningJob definition. The fix is straightforward and moves the environment assignment into the constructor call. However, there are a few concerns: the use of getattr is unnecessary since environment is a defined attribute on ModelTrainer, the PR description mentions fixing _build_training_job_definitions (multi-trainer path) but only fixes _build_training_job_definition (single-trainer path), and the empty dict handling may be surprising to users.
| model_trainer.stopping_condition.max_wait_time_in_seconds | ||
| ) | ||
|
|
||
| # Get environment variables from model_trainer |
There was a problem hiding this comment.
The truthiness check not env will coerce an empty dict {} to True, causing it to be set to None. While the test covers this, silently converting a user-provided {} to None could be surprising. Consider whether an empty dict should be passed through as-is (the API would accept it), or if this is intentional. If intentional, a brief comment explaining why empty dicts are normalized to None would help future maintainers.
Also, the isinstance(env, dict) check is defensive — if ModelTrainer.environment has a type annotation of dict | None, Pydantic validation should already enforce this. Is this guard necessary?
|
|
||
| definition = tuner._build_training_job_definition(None) | ||
|
|
||
| assert definition.environment is None, ( |
There was a problem hiding this comment.
Nit: The assertion message says "Environment should be None when empty dict is provided" — this documents the behavior but consider whether this is actually the desired UX. A user who explicitly sets environment={} might not expect it to be silently dropped. If this is intentional, it's fine, but worth confirming with the team.
🤖 Iteration #1 — Review Comments AddressedDescriptionFix environment variables not being propagated from The
Changes Made
Testing
Comments reviewed: 5
|
sagemaker-bot
left a comment
There was a problem hiding this comment.
🤖 AI Code Review
This PR fixes a bug where environment variables from ModelTrainer were not being propagated to the HyperParameterTrainingJobDefinition. The fix simplifies the code by passing the environment directly in the constructor rather than setting it after the fact with conditional logic. The approach is clean and the tests cover the key scenarios well.
🤖 Iteration #2 — Review Comments AddressedDescriptionFix environment variables not being propagated from The
Changes Made
Testing
Comments reviewed: 30
|
Description
The HyperparameterTuner's _build_training_job_definition method (in the truncated portion of tuner.py, after line ~1058) constructs a TrainingJobDefinition object from the ModelTrainer's attributes but omits the 'environment' field. The SageMaker CreateHyperParameterTuningJob API supports an 'Environment' key in TrainingJobDefinition, but the tuner never reads model_trainer.environment and includes it. Similarly, for the multi-trainer dict path (_build_training_job_definitions), environment is also not propagated. The fix is to read model_trainer.environment in both _build_training_job_definition and _build_training_job_definitions methods and include it in the training job definition output.
Related Issue
Related issue: 5613
Changes Made
sagemaker-train/src/sagemaker/train/tuner.pysagemaker-train/tests/unit/train/test_tuner.pyAI-Generated PR
This PR was automatically generated by the PySDK Issue Agent.
Merge Checklist
prefix: descriptionformat