code: def _pydantic_models_to_pydf(
      first_element: Any,
      data: Sequence[Any],
      schema: SchemaDefinition | None,
      schema_overrides: SchemaDict | None,
      infer_schema_length: int | None,
      **kwargs: Any,
  ) -> PyDataFrame:
      """Initialise DataFrame from pydantic model objects."""
      import pydantic  # note: must already be available in the env here
  
      old_pydantic = parse_version(pydantic.__version__) < (2, 0)
      model_fields = list(
          first_element.__fields__
          if old_pydantic
          else first_element.model_fields
      )
      (
          unpack_nested,
          column_names,
          schema_overrides,
          overrides,
      ) = _establish_dataclass_or_model_schema(
          first_element, schema, schema_overrides, model_fields
      )
      if unpack_nested:
          # note: this is an *extremely* slow path, due to the requirement to
          # use pydantic's 'dict()' method to properly unpack nested models
          dicts = (
              [md.dict() for md in data]
              if old_pydantic
              else [md.model_dump(mode="python") for md in data]
          )
          pydf = PyDataFrame.read_dicts(dicts, infer_schema_length)
  
      elif len(model_fields) > 50:
          # 'read_rows' is the faster codepath for models with a lot of fields...
          get_values = itemgetter(*model_fields)
          rows = [get_values(md.__dict__) for md in data]
          pydf = PyDataFrame.read_rows(
              rows, infer_schema_length, overrides
          )
      else:
          # ...and 'read_dicts' is faster otherwise
          dicts = [md.__dict__ for md in data]
          pydf = PyDataFrame.read_dicts(
              dicts, infer_schema_length, overrides
          )
  
      if overrides:
          structs = {
              c: tp
              for c, tp in overrides.items()
              if isinstance(tp, Struct)
          }
          pydf = _post_apply_columns(
              pydf, column_names, structs, schema_overrides
          )
  
      return pydf
  
imports: from __future__ import annotations
  import contextlib
  import warnings
  from datetime import date, datetime, time, timedelta
  from decimal import Decimal
  from functools import lru_cache, singledispatch
  from itertools import islice, zip_longest
  from operator import itemgetter
  from sys import version_info
  from typing import TYPE_CHECKING, Any, Callable, Generator, Iterable, Iterator, Mapping, MutableMapping, Sequence, get_type_hints
  import polars._reexport
  from polars import functions
  from polars.datatypes import INTEGER_DTYPES, N_INFER_DEFAULT, TEMPORAL_DTYPES, Boolean, Categorical, Date, Datetime, Duration, Enum, List, Null, Object, String, Struct, Time, UInt32, Unknown, dtype_to_py_type, is_polars_dtype, numpy_char_code_to_dtype, py_type_to_dtype
  from polars.datatypes.constructor import numpy_type_to_constructor, numpy_values_and_dtype, polars_type_to_constructor, py_type_to_constructor
  from polars.dependencies import _NUMPY_AVAILABLE, _check_for_numpy, _check_for_pandas, _check_for_pydantic, dataclasses, pydantic
  from polars.dependencies import numpy
  from polars.dependencies import pandas
  from polars.dependencies import pyarrow
  from polars.exceptions import ComputeError, ShapeError, TimeZoneAwareConstructorWarning
  from polars.utils._wrap import wrap_df, wrap_s
  from polars.utils.meta import get_index_type, threadpool_size
  from polars.utils.various import _is_generator, arrlen, find_stacklevel, parse_version, range_to_series
label_desc: Use image node_id 35 for calling a global function with example usage: _pydantic_models_to_pydf(first_element, data, schema, schema_overrides, infer_schema_length) and returns: pydf