NVIDIA / NVIDIA/cloudai

Refactor private attributes in Pydantic models to use PrivateAttr

Open
#800 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
99
Forks
62
Avg merge
6d 12h
Merged PRs (30d)
17

Description

Description

In Pydantic v2, private attributes (those starting with underscore) should be declared using PrivateAttr() for stability and to follow the official API. While bare annotations like _attr: Type | None = None may work incidentally, they are not part of the guaranteed Pydantic API and can break with minor version updates.

Background

This issue was identified during review of PR #799:

Affected Files

The following files contain Pydantic models with private attributes that should be refactored to use PrivateAttr:

Workloads (19 files)
  1. src/cloudai/workloads/vllm/vllm.py - _docker_image, _hf_model
  2. src/cloudai/workloads/ucc_test/ucc.py - _docker_image
  3. src/cloudai/workloads/slurm_container/slurm_container.py - _docker_image
  4. src/cloudai/workloads/nixl_perftest/nixl_perftest.py - _docker_image
  5. src/cloudai/workloads/osu_bench/osu_bench.py - _osu_image
  6. src/cloudai/workloads/triton_inference/triton_inference.py - _server_image, _client_image
  7. src/cloudai/workloads/nemo_run/nemo_run.py - _docker_image
  8. src/cloudai/workloads/nemo_launcher/nemo_launcher.py - _docker_image, _python_executable
  9. src/cloudai/workloads/nixl_kvbench/nixl_kvbench.py - _docker_image
  10. src/cloudai/workloads/nixl_bench/nixl_bench.py - _nixl_image
  11. src/cloudai/workloads/nccl_test/nccl.py - _docker_image
  12. src/cloudai/workloads/jax_toolbox/nemotron.py - _docker_image
  13. src/cloudai/workloads/jax_toolbox/grok.py - _docker_image
  14. src/cloudai/workloads/jax_toolbox/gpt.py - _docker_image
  15. src/cloudai/workloads/megatron_run/megatron_run.py - _docker_image
  16. src/cloudai/workloads/deepep/deepep.py - _docker_image
  17. src/cloudai/workloads/chakra_replay/chakra_replay.py - _docker_image
  18. src/cloudai/workloads/ddlb/ddlb.py - _docker_image
  19. src/cloudai/workloads/megatron_bridge/megatron_bridge.py - _docker_image, _python_executable, _megatron_bridge_repo
  20. src/cloudai/workloads/ai_dynamo/ai_dynamo.py - _docker_image, _hf_model
Systems (1 file)
  1. src/cloudai/systems/kubernetes/kubernetes_system.py - _core_v1, _batch_v1, _custom_objects_api, _genai_perf_completed

Recommended Changes

For each affected file:

  1. Add PrivateAttr to the pydantic imports:

    from pydantic import Field, PrivateAttr
    
  2. Replace bare annotations with PrivateAttr:

    # Before
    _docker_image: DockerImage | None = None
    
    # After
    _docker_image: DockerImage | None = PrivateAttr(default=None)
    

Good Example

src/cloudai/systems/runai/runai_system.py already follows this pattern correctly:

from pydantic import Field, PrivateAttr

class RunAISystem(KubernetesSystem):
    _api_client: Optional[RunAIRestClient] = PrivateAttr(default=None)

Note

src/cloudai/_core/installables.py uses dataclass field(), not Pydantic, so it doesn't need to be changed.

References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read src/cloudai/systems/runai/runai_system.py for the existing PrivateAttr pattern, then inspect the listed workload and Kubernetes model files. Replace each affected bare private-attribute annotation with PrivateAttr while preserving its default and type. Done means all listed Pydantic attributes use the official pattern and src/cloudai/_core/installables.py remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.