deepset-ai/haystack · error · DeserializationError

Could not apply arguments {generic_args} to type {main_type}

Error message

Could not apply arguments {generic_args} to type {main_type}

What it means

deserialize_type() reconstructs generic types like list[int] by parsing the serialized generic arguments and subscripting the resolved main type. When main_type[...] raises TypeError or AttributeError (e.g. the base type is not subscriptable or does not accept those args), haystack wraps it in DeserializationError with this message. It signals the serialized type string is structurally incompatible with the resolved type.

Source

Thrown at haystack/utils/type_serialization.py:261

    if "[" in type_str and type_str.endswith("]"):
        main_type_str, generics_str = type_str.split("[", 1)
        generics_str = generics_str[:-1]

        main_type = deserialize_type(main_type_str)

        # Parse literal args with ast.literal_eval, which safely handles
        # str/int/bool/None/bytes and is quote-aware, so a comma inside a string value does not split the
        # arguments.
        if main_type is typing.Literal:
            return typing.Literal[ast.literal_eval(f"({generics_str},)")]

        generic_args = [_deserialize_type_arg(arg) for arg in _parse_generic_args(generics_str)]

        # Reconstruct
        try:
            return main_type[tuple(generic_args) if len(generic_args) > 1 else generic_args[0]]
        except (TypeError, AttributeError) as e:
            raise DeserializationError(f"Could not apply arguments {generic_args} to type {main_type}") from e

    # Handle non-generic types
    # First, check if there's a module prefix
    if "." in type_str:
        try:
            return _import_class_by_name(type_str)
        except ImportError as e:
            raise DeserializationError(str(e)) from e

    # No module prefix, check builtins and typing.
    # (None / NoneType / Ellipsis are handled at the top of this function, before they can reach the
    # builtin type gate below which would refuse them for not being types.)
    if hasattr(builtins, type_str):
        resolved = getattr(builtins, type_str)
        # This bare-name path never consults the allowlist. A type annotation must resolve to an
        # actual type, so builtin functions like `eval`/`exec` are rejected while types pass.
        _check_builtin_is_type(resolved, type_str)
        return resolved

View on GitHub (pinned to e318778c9b)

Solutions

  1. Check the serialized type string: verify the main type actually supports the generic arguments (arity and kinds)
  2. Fix or regenerate the serialized dict/pipeline YAML so the type matches the current code version
  3. If it's your own class, implement __class_getitem__ or make it subscriptable, or serialize it without generics
  4. Import the resolved main type in a REPL and test main_type[args] manually to see the underlying TypeError

Example fix

// before
deserialize_type("int[str]")  # DeserializationError
// after
deserialize_type("list[int]")  # or make the class generic-capable
Defensive patterns

Strategy: try-catch

Validate before calling

import builtins, typing
def check_generic(type_str):
    main = type_str.split("[")[0]
    return "." in main or hasattr(builtins, main) or hasattr(typing, main)

Type guard

def is_subscriptable(t):
    return hasattr(t, "__class_getitem__")

Try / catch

from haystack.utils import DeserializationError
try:
    t = deserialize_type(type_str)
except DeserializationError as e:
    log.error("bad serialized type: %s", e)
    t = fallback_type

Prevention

When it happens

Trigger: Calling deserialize_type() (directly or via from_dict/_schema_from_dict) on a string whose main type cannot take the parsed generic args, e.g. 'int[str]', 'dict[int]' with wrong arity, or a custom class that doesn't support __class_getitem__.

Common situations: Loading pipeline/component YAML written for a different haystack version where the type changed generic arity; hand-edited serialization dicts; a custom class renamed or replaced by a non-generic type after serialization.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/5f7c8dcd49bd99ab. Report an issue: GitHub.