code: def _enforce_tensor_spec(
      values: Union[np.ndarray, "csc_matrix", "csr_matrix"],
      tensor_spec: TensorSpec,
  ):
      """
      Enforce the input tensor shape and type matches the provided tensor spec.
      """
      expected_shape = tensor_spec.shape
      expected_type = tensor_spec.type
      actual_shape = values.shape
      actual_type = (
          values.dtype
          if isinstance(values, np.ndarray)
          else values.data.dtype
      )
  
      # This logic is for handling "ragged" arrays. The first check is for a standard numpy shape
      # representation of a ragged array. The second is for handling a more manual specification
      # of shape while support an input which is a ragged array.
      if (
          len(expected_shape) == 1
          and expected_shape[0] == -1
          and expected_type == np.dtype("O")
      ):
          # Sample spec: Tensor('object', (-1,))
          # Will pass on any provided input
          return values
      if (
          len(expected_shape) > 1
          and -1 in expected_shape[1:]
          and len(actual_shape) == 1
          and actual_type == np.dtype("O")
      ):
          # Sample spec: Tensor('float64', (-1, -1, -1, 3))
          # Will pass on inputs which are ragged arrays: shape==(x,), dtype=='object'
          return values
  
      if len(expected_shape) != len(actual_shape):
          raise MlflowException(
              f"Shape of input {actual_shape} does not match expected shape {expected_shape}."
          )
      for expected, actual in zip(expected_shape, actual_shape):
          if expected == -1:
              continue
          if expected != actual:
              raise MlflowException(
                  f"Shape of input {actual_shape} does not match expected shape {expected_shape}."
              )
      if clean_tensor_type(actual_type) != expected_type:
          raise MlflowException(
              f"dtype of input {actual_type} does not match expected dtype {expected_type}"
          )
      return values
  
imports: import datetime
  import decimal
  import json
  import logging
  import os
  from copy import deepcopy
  from typing import Any, Dict, List, Optional, Union
  import numpy
  import pandas
  from mlflow.exceptions import INVALID_PARAMETER_VALUE, MlflowException
  from mlflow.models import Model
  from mlflow.store.artifact.utils.models import get_model_name_and_version
  from mlflow.types import DataType, ParamSchema, ParamSpec, Schema, TensorSpec
  from mlflow.types.schema import Array, Object, Property
  from mlflow.types.utils import TensorsNotSupportedException, _infer_param_schema, clean_tensor_type
  from mlflow.utils.annotations import experimental
  from mlflow.utils.proto_json_utils import NumpyEncoder, dataframe_from_parsed_json, parse_tf_serving_input
  from mlflow.utils.uri import get_databricks_profile_uri_from_artifact_uri
label_desc: Use image node_id 9 for calling a global function with example usage: _enforce_tensor_spec(values, tensor_spec) and returns: values, values, values