Skip to content

fix: Add PipelineVariable support to ModelTrainer fields (fixes #5524)#5608

Open
amitmodi wants to merge 2 commits intoaws:masterfrom
amitmodi:fix/pipeline-variable-model-trainer
Open

fix: Add PipelineVariable support to ModelTrainer fields (fixes #5524)#5608
amitmodi wants to merge 2 commits intoaws:masterfrom
amitmodi:fix/pipeline-variable-model-trainer

Conversation

@amitmodi
Copy link

@amitmodi amitmodi commented Mar 7, 2026

Summary

This PR extends PipelineVariable support to ModelTrainer direct fields, unblocking V2-V3 migration for SageMaker Pipelines users.

Fixes: #5524

Problem

ModelTrainer uses Pydantic BaseModel with strict validation. Fields like training_image are typed as Optional[str], causing Pydantic to reject PipelineVariable objects (e.g. ParameterString) with a ValidationError. This blocks any customer using SageMaker Pipelines from migrating to V3.

Solution

Follow the existing V3 pattern already established by SourceCode, OutputDataConfig, and Compute - use the StrPipeVar type alias (Union[str, PipelineVariable]) which already exists in pipeline_variable.py with Pydantic compatibility via get_pydantic_core_schema().

Changes (1 file, 5 insertions, 4 deletions)

  • training_image: Optional[str] -> Optional[StrPipeVar]
  • algorithm_name: Optional[str] -> Optional[StrPipeVar]
  • training_input_mode: Optional[str] -> Optional[StrPipeVar]
  • environment: Dict[str, str] -> Dict[str, StrPipeVar]

Testing

Before: ModelTrainer(training_image=ParameterString(name='img')) raises ValidationError
After: ModelTrainer(training_image=ParameterString(name='img')) works

Note

This is step 1. Integer/boolean fields in nested configs (Compute, Networking) need IntPipeVar/BoolPipeVar in a follow-up PR.

)

Extend StrPipeVar type to ModelTrainer's direct fields:
- training_image: Optional[str] -> Optional[StrPipeVar]
- algorithm_name: Optional[str] -> Optional[StrPipeVar]
- training_input_mode: Optional[str] -> Optional[StrPipeVar]
- environment: Dict[str, str] -> Dict[str, StrPipeVar]

This follows the existing V3 pattern already used by SourceCode,
OutputDataConfig, and Compute (for instance_type). The StrPipeVar
type alias and PipelineVariable.__get_pydantic_core_schema__()
already exist in the codebase.

This unblocks V2->V3 migration for SageMaker Pipelines users who
need to pass ParameterString to ModelTrainer fields.

Fixes aws#5524
…ble-safe logging

- Add test_model_trainer_pipeline_variable.py with 9 tests:
  - 4 PipelineVariable acceptance tests (training_image, algorithm_name,
    training_input_mode, environment)
  - 4 regression tests (real string values still work)
  - 1 invalid type rejection test
- Fix PipelineVariable-safe logging in model_post_init (avoid __str__
  on PipelineVariable which raises TypeError)

All 57 tests pass (48 existing + 9 new, 0 regressions).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant