code: def _internal_convert(
      filters: Union[List[Any], Dict[str, Any]], previous_key=None
  ) -> Any:
      """
      Recursively convert filters from legacy to new style.
      """
      conditions = []
  
      if (
          isinstance(filters, list)
          and (result := _handle_list(filters, previous_key))
          is not None
      ):
          return result
  
      if not isinstance(filters, dict):
          return _handle_non_dict(filters, previous_key)
  
      for key, value in filters.items():
          if (
              previous_key is not None
              and previous_key not in ALL_LEGACY_OPERATORS_MAPPING
              and key not in ALL_LEGACY_OPERATORS_MAPPING
          ):
              msg = f"This filter ({filters}) seems to be malformed."
              raise FilterError(msg)
          if key not in ALL_LEGACY_OPERATORS_MAPPING:
              converted = _internal_convert(value, previous_key=key)
              if isinstance(converted, list):
                  conditions.extend(converted)
              else:
                  conditions.append(converted)
          elif key in LEGACY_LOGICAL_OPERATORS_MAPPING:
              if (
                  previous_key not in ALL_LEGACY_OPERATORS_MAPPING
                  and isinstance(value, list)
              ):
                  converted = [
                      _internal_convert({previous_key: v})
                      for v in value
                  ]
                  conditions.append(
                      {
                          "operator": ALL_LEGACY_OPERATORS_MAPPING[key],
                          "conditions": converted,
                      }
                  )
              else:
                  converted = _internal_convert(value, previous_key=key)
                  if key == "$not" and type(converted) not in [
                      dict,
                      list,
                  ]:
                      # This handles a corner when '$not' is used like this:
                      # '{"page": {"$not": 102}}'
                      # Without this check we would miss the implicit '$eq'
                      converted = {
                          "field": previous_key,
                          "operator": "==",
                          "value": value,
                      }
                  if not isinstance(converted, list):
                      converted = [converted]
                  conditions.append(
                      {
                          "operator": ALL_LEGACY_OPERATORS_MAPPING[key],
                          "conditions": converted,
                      }
                  )
          elif key in LEGACY_COMPARISON_OPERATORS_MAPPING:
              conditions.append(
                  {
                      "field": previous_key,
                      "operator": ALL_LEGACY_OPERATORS_MAPPING[key],
                      "value": value,
                  }
              )
  
      if len(conditions) == 1:
          return conditions[0]
  
      if previous_key is None:
          return {"operator": "AND", "conditions": conditions}
  
      return conditions
  
imports: from typing import List, Any, Union, Dict
  from dataclasses import fields
  from datetime import datetime
  import pandas
  from haystack.dataclasses import Document
  from haystack.errors import FilterError
label_desc: Use image node_id 16 for calling a global function with example usage: _internal_convert(filters, previous_key) and returns: conditions, result, _handle_non_dict, conditions, dict