{"record":{"id":"0365c7bfd9071c45","repo":"deepset-ai/haystack","slug":"serialization-of-lambdas-is-not-supported","errorCode":null,"errorMessage":"Serialization of lambdas is not supported.","messagePattern":"Serialization of lambdas is not supported\\.","errorType":"exception","errorClass":"SerializationError","httpStatus":null,"severity":"error","filePath":"haystack/utils/callable_serialization.py","lineNumber":45,"sourceCode":"def serialize_callable(callable_handle: Callable) -> str:\n    \"\"\"\n    Serializes a callable to its full path.\n\n    :param callable_handle: The callable to serialize\n    :return: The full path of the callable\n    \"\"\"\n    try:\n        full_arg_spec = inspect.getfullargspec(callable_handle)\n        is_instance_method = bool(full_arg_spec.args and full_arg_spec.args[0] == \"self\")\n    except TypeError:\n        is_instance_method = False\n    if is_instance_method:\n        raise SerializationError(\"Serialization of instance methods is not supported.\")\n\n    # __qualname__ contains the fully qualified path we need for classmethods and staticmethods\n    qualname = getattr(callable_handle, \"__qualname__\", \"\")\n    if \"<lambda>\" in qualname:\n        raise SerializationError(\"Serialization of lambdas is not supported.\")\n    if \"<locals>\" in qualname:\n        raise SerializationError(\"Serialization of nested functions is not supported.\")\n\n    name = qualname or callable_handle.__name__\n\n    # Get the full package path of the function\n    module = inspect.getmodule(callable_handle)\n    if module is not None:\n        full_path = f\"{module.__name__}.{name}\"\n    else:\n        full_path = name\n\n    # Serialization succeeds, but a denied builtin (e.g. `eval`) won't reload without `unsafe=True`.\n    if _is_denied_builtin(callable_handle):\n        logger.warning(\n            \"Serialized callable '{full_path}' is a builtin that is blocked during deserialization; \"\n            \"the resulting pipeline will only be loadable with unsafe=True.\",\n            full_path=full_path,","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/utils/callable_serialization.py#L27-L63","documentation":"serialize_callable inspects __qualname__ and raises SerializationError when it contains '<lambda>', because a lambda has no importable module path and cannot be reconstructed by deserialize_callable. Only importable, named callables can be serialized.","triggerScenarios":"Passing a lambda as a callable parameter (e.g. TextSplitter(splitting_function=lambda t: t.lower())) and then calling to_dict or pipeline.dumps().","commonSituations":"Quick inline lambdas used in scripts or notebooks that later get saved to YAML; pipeline configs built at runtime.","solutions":["Replace the lambda with a named module-level function","Define the logic in a small helper function in an importable module","If configuration-only customization is needed, use a supported parameter instead of a callable"],"exampleFix":"// before\nTextSplitter(splitting_function=lambda t: t.lower())\n// after\n# mymodule/helpers.py\ndef lowercase(t): return t.lower()\nTextSplitter(splitting_function=lowercase)","handlingStrategy":"validation","validationCode":"def is_lambda(fn) -> bool:\n    return getattr(fn, \"__name__\", \"\") == \"<lambda>\"\n\n# reject before passing to component","typeGuard":"import types\nfrom collections.abc import Callable\n\ndef is_named_module_level_function(fn: Callable) -> bool:\n    return isinstance(fn, types.FunctionType) and \"<\" not in fn.__qualname__","tryCatchPattern":"try:\n    serialized = component.to_dict()\nexcept SerializationError as e:\n    if \"lambda\" in str(e):\n        logger.error(\"replace lambda with a named module-level function\")\n    raise","preventionTips":["Lint against lambdas in component init parameters","Replace lambdas with named helpers in an importable module","Review notebooks/scripts before saving pipelines to YAML"],"tags":["python","serialization","lambda","callable","haystack"],"backgroundTag":"unserializable-callable","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}