```python:inference/core/workflows/execution_engine/introspection/schema_parser.py
import itertools
from collections import defaultdict
from typing import Optional, Set

from inference.core.workflows.execution_engine.entities.types import (
    KIND_KEY, REFERENCE_KEY, SELECTED_ELEMENT_KEY,
    SELECTOR_POINTS_TO_BATCH_KEY, Kind)
from inference.core.workflows.execution_engine.introspection.entities import (
    ReferenceDefinition, SelectorDefinition)

ITEMS_KEY = "items"
ALL_OF_KEY = "allOf"
ANY_OF_KEY = "anyOf"
ONE_OF_KEY = "oneOf"


def retrieve_selectors_from_simple_property(
    property_name: str,
    property_description: str,
    property_definition: dict,
    property_dimensionality_offset: int,
    is_dimensionality_reference_property: bool,
    inputs_accepting_batches: Set[str],
    inputs_accepting_batches_and_scalars: Set[str],
    inputs_enforcing_auto_batch_casting: Set[str],
    is_list_element: bool = False,
    is_dict_element: bool = False,
) -> Optional[SelectorDefinition]:
    if REFERENCE_KEY in property_definition:
        declared_points_to_batch = property_definition.get(
            SELECTOR_POINTS_TO_BATCH_KEY, False
        )
        if declared_points_to_batch == "dynamic":
            if property_name in inputs_accepting_batches_and_scalars:
                if property_name in inputs_enforcing_auto_batch_casting:
                    points_to_batch = {True}
                else:
                    points_to_batch = {True, False}
            else:
                points_to_batch = {
                    property_name in inputs_accepting_batches
                    or property_name in inputs_enforcing_auto_batch_casting
                }
        else:
            points_to_batch = {declared_points_to_batch}
        allowed_references = [
            ReferenceDefinition(
                selected_element=property_definition[SELECTED_ELEMENT_KEY],
                kind=[
                    Kind.model_validate(k)
                    for k in property_definition.get(KIND_KEY, [])
                ],
                points_to_batch=points_to_batch,
            )
        ]
        return SelectorDefinition(
            property_name=property_name,
            property_description=property_description,
            allowed_references=allowed_references,
            is_list_element=is_list_element,
            is_dict_element=is_dict_element,
            dimensionality_offset=property_dimensionality_offset,
            is_dimensionality_reference_property=is_dimensionality_reference_property,
        )
    if ITEMS_KEY in property_definition:
        if is_list_element or is_dict_element:
            # ignoring nested references above first level of depth
            return None
        return retrieve_selectors_from_simple_property(
            property_name=property_name,
            property_description=property_description,
            property_definition=property_definition[ITEMS_KEY],
            property_dimensionality_offset=property_dimensionality_offset,
            is_dimensionality_reference_property=is_dimensionality_reference_property,
            inputs_accepting_batches=inputs_accepting_batches,
            inputs_accepting_batches_and_scalars=inputs_accepting_batches_and_scalars,
            inputs_enforcing_auto_batch_casting=inputs_enforcing_auto_batch_casting,
            is_list_element=True,
        )
    # Inlined property_defines_union check
    if (
        ANY_OF_KEY in property_definition
        or ONE_OF_KEY in property_definition
        or ALL_OF_KEY in property_definition
    ):
        return retrieve_selectors_from_union_definition(
            property_name=property_name,
            property_description=property_description,
            union_definition=property_definition,
            is_list_element=is_list_element,
            is_dict_element=is_dict_element,
            property_dimensionality_offset=property_dimensionality_offset,
            is_dimensionality_reference_property=is_dimensionality_reference_property,
            inputs_accepting_batches=inputs_accepting_batches,
            inputs_accepting_batches_and_scalars=inputs_accepting_batches_and_scalars,
            inputs_enforcing_auto_batch_casting=inputs_enforcing_auto_batch_casting,
        )
    return None


def retrieve_selectors_from_union_definition(
    property_name: str,
    property_description: str,
    union_definition: dict,
    is_list_element: bool,
    is_dict_element: bool,
    property_dimensionality_offset: int,
    is_dimensionality_reference_property: bool,
    inputs_accepting_batches: Set[str],
    inputs_accepting_batches_and_scalars: Set[str],
    inputs_enforcing_auto_batch_casting: Set[str],
) -> Optional[SelectorDefinition]:
    results = []
    # Process union types more efficiently by iterating once
    for key in (ANY_OF_KEY, ONE_OF_KEY, ALL_OF_KEY):
        union_types = union_definition.get(key)
        if not union_types:
            continue
        for type_definition in union_types:
            result = retrieve_selectors_from_simple_property(
                property_name=property_name,
                property_description=property_description,
                property_definition=type_definition,
                property_dimensionality_offset=property_dimensionality_offset,
                is_dimensionality_reference_property=is_dimensionality_reference_property,
                inputs_accepting_batches=inputs_accepting_batches,
                inputs_accepting_batches_and_scalars=inputs_accepting_batches_and_scalars,
                inputs_enforcing_auto_batch_casting=inputs_enforcing_auto_batch_casting,
                is_list_element=is_list_element,
                is_dict_element=is_dict_element,
            )
            if result is not None:
                results.append(result)
    
    if not results:
        return None
    
    results_references_kind_by_selected_element = defaultdict(set)
    results_references_batch_pointing_by_selected_element = defaultdict(set)
    
    for result in results:
        for reference in result.allowed_references:
            results_references_kind_by_selected_element[reference.selected_element].update(
                reference.kind
            )
            results_references_batch_pointing_by_selected_element[
                reference.selected_element
            ].update(reference.points_to_batch)
    
    merged_references = [
        ReferenceDefinition(
            selected_element=reference_selected_element,
            kind=list(kind),
            points_to_batch=results_references_batch_pointing_by_selected_element[
                reference_selected_element
            ],
        )
        for reference_selected_element, kind in results_references_kind_by_selected_element.items()
    ]
    
    merged_is_list_element = is_list_element or any(r.is_list_element for r in results)
    merged_is_dict_element = is_dict_element or any(r.is_dict_element for r in results)
    return SelectorDefinition(
        property_name=property_name,
        property_description=property_description,
        allowed_references=merged_references,
        is_list_element=merged_is_list_element,
        is_dict_element=merged_is_dict_element,
        dimensionality_offset=property_dimensionality_offset,
        is_dimensionality_reference_property=is_dimensionality_reference_property,
    )
```