Skip to content

Commit 5441f68

Browse files
docs: clarify component init serialization (#10205)
1 parent 606d918 commit 5441f68

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

haystack/core/component/component.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
dictionaries containing only such values. Anything else (objects, functions, etc) will raise an exception at init
3535
time. If there's the need for such values, consider serializing them to a string.
3636
37-
_(TODO explain how to use classes and functions in init. In the meantime see `test/components/test_accumulate.py`)_
37+
If you need to accept classes or callables, accept either a string import path or the callable itself. Resolve strings
38+
to objects in `__init__`, and serialize objects back to importable strings in `to_dict()` so that `from_dict()` can load
39+
them (for example, store `"module_path.symbol_name"` and load it via `importlib`). This keeps init parameters JSON
40+
serializable for pipeline save/load. See `haystack.testing.sample_components.accumulate.Accumulate` for a reference
41+
implementation.
3842
3943
The `__init__` must be extremely lightweight, because it's a frequent operation during the construction and
4044
validation of the pipeline. If a component has some heavy state to initialize (models, backends, etc...) refer to

0 commit comments

Comments
 (0)