haystack icon indicating copy to clipboard operation
haystack copied to clipboard

More robust `default_from_dict`, `default_to_dict` that handle Secrets, StreamingCallbackT, Tool, Toolset automatically

Open julian-risch opened this issue 8 months ago • 1 comments

Is your feature request related to a problem? Please describe. When users create a custom component that accepts a Secret (an API key etc) in the init, they need to define from_dict and to_dict methods. In contrast, simpler custom components can directly use the default implementations default_from_dict and default_to_dict methods, making the implementation much more compact.

Describe the solution you'd like We should provide more robust versions of default_from_dict, default_to_dict that automatically handle the (de-)serialization of components that have a Secret in the init. Secrets can be a first example. Later, in a separate issue/PR, other functionality could follow, for example serialize_callable or serialize_tools_or_toolset. If all three were covered by the default_from_dict and default_to_dict, the OpenAIChatGenerator for example wouldn't need a custom implementation of from_dict and to_dict.

self.document_store.to_dict(), is another example

In addition, it would be a great simplification for users if the @component decorator automatically adds to_dict and from_dict, which internally call component_from_dict, component_to_dict

julian-risch avatar Apr 10 '25 07:04 julian-risch

Related https://github.com/deepset-ai/haystack/pull/9345#discussion_r2091046966

julian-risch avatar May 19 '25 07:05 julian-risch