{"id":7368,"library":"lightkube-models","title":"lightkube-models","description":"This Python module provides definitions of Kubernetes models and resources for use with the `lightkube` client library. The version of this package (major.minor.micro) directly corresponds to the Kubernetes API schema version it defines. Users should install the module version that matches their target Kubernetes installation or ensure compatibility. It is currently active, with new versions released to support the latest Kubernetes API schemas.","status":"active","version":"1.35.0.8","language":"en","source_language":"en","source_url":"https://github.com/gtsystem/lightkube-models","tags":["kubernetes","models","resources","dataclasses","api-client","k8s"],"install":[{"cmd":"pip install lightkube-models","lang":"bash","label":"Install lightkube-models"}],"dependencies":[{"reason":"Required for package execution.","package":"python","optional":false,"version_constraint":">=3.9"},{"reason":"Provides the Kubernetes API client that uses these models. While not a direct dependency of `lightkube-models`, they are designed to be used together, and `lightkube` itself depends on `lightkube-models`.","package":"lightkube","optional":true}],"imports":[{"note":"Kubernetes models are organized by API group and version (e.g., `meta_v1`, `apps_v1`). Direct imports from `lightkube.models` will result in an `ImportError`.","wrong":"from lightkube.models import ObjectMeta","symbol":"ObjectMeta","correct":"from lightkube.models.meta_v1 import ObjectMeta"},{"note":"Similar to `ObjectMeta`, specific model components are nested under their respective API group and version modules.","symbol":"PodSpec","correct":"from lightkube.models.core_v1 import PodSpec"}],"quickstart":{"code":"from lightkube.models.meta_v1 import ObjectMeta\nfrom lightkube.models.core_v1 import PodSpec, Container\n\n# Create a simple ObjectMeta instance\nmetadata = ObjectMeta(name='my-pod', namespace='default', labels={'app': 'my-app'})\nprint(f\"Created ObjectMeta: {metadata.name} in {metadata.namespace} with labels {metadata.labels}\")\n\n# Create a PodSpec\ncontainer = Container(name='nginx', image='nginx:latest')\npod_spec = PodSpec(containers=[container])\nprint(f\"Created PodSpec with container: {pod_spec.containers[0].name}\")\n","lang":"python","description":"Demonstrates how to import and instantiate basic Kubernetes model objects like `ObjectMeta` and `PodSpec` using their specific API group and version paths."},"warnings":[{"fix":"Install a `lightkube-models` version that aligns with your target Kubernetes cluster API version. For example, for Kubernetes 1.29, use `lightkube-models>=1.29,<1.30`.","message":"The version of `lightkube-models` (first three parts: major.minor.micro) directly corresponds to the Kubernetes API schema version it encapsulates. Mismatching this version with your Kubernetes cluster's version can lead to schema validation errors or missing fields.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When interacting with the `lightkube` client, prefer importing from `lightkube.resources.{group}_{version}` (e.g., `from lightkube.resources.core_v1 import Pod`) rather than `lightkube.models`. If you only need schema definition without client interaction, `lightkube.models` is appropriate.","message":"`lightkube.models` provides pure dataclass representations of Kubernetes schemas. The `lightkube` client (from the `lightkube` library) often expects `lightkube.resources` classes, which are subclasses of models with additional metadata for client operations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For `lightkube-models` versions older than 1.33, ensure `apiVersion` and `kind` are explicitly set in the model's constructor, or upgrade to a `lightkube-models` version >= 1.33, which sets these automatically during `post_init`.","message":"Prior to `lightkube-models` version 1.33, resource models did not automatically set `apiVersion` and `kind` during instantiation. These fields had to be manually provided when creating objects.","severity":"breaking","affected_versions":"<1.33.x.x"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Models are organized by Kubernetes API group and version. You need to import from the specific module, e.g., `from lightkube.models.core_v1 import Pod` for a Pod model, or `from lightkube.models.apps_v1 import Deployment` for a Deployment model.","cause":"Attempting to import a Kubernetes model directly from the top-level `lightkube.models` package instead of its versioned API group submodule.","error":"ModuleNotFoundError: No module named 'lightkube.models.Pod'"},{"fix":"For Custom Resources, you need to explicitly define a generic resource class using `lightkube.generic_resource.create_namespaced_resource` or `create_global_resource` before attempting to load or interact with instances of that CRD. Alternatively, `lightkube.generic_resource.load_in_cluster_generic_resources()` can load all CRDs from a cluster dynamically.","cause":"This error occurs when trying to load a Custom Resource Definition (CRD) object from a YAML file or dictionary using `lightkube.codecs.load_all_yaml()` without `lightkube` being aware of that custom resource's schema. `lightkube-models` only includes standard Kubernetes resources.","error":"lightkube.core.exceptions.LoadResourceError: No module named 'lightkube.resources.stable_example_com_v1'"}]}