diff --git a/haystack/components/builders/chat_prompt_builder.py b/haystack/components/builders/chat_prompt_builder.py index a82f7eef7b..472e870a7b 100644 --- a/haystack/components/builders/chat_prompt_builder.py +++ b/haystack/components/builders/chat_prompt_builder.py @@ -254,7 +254,8 @@ def from_dict(cls, data: Dict[str, Any]) -> "ChatPromptBuilder": The deserialized component. """ init_parameters = data["init_parameters"] - template = init_parameters.get("template", []) - init_parameters["template"] = [ChatMessage.from_dict(d) for d in template] + template = init_parameters.get("template") + if template: + init_parameters["template"] = [ChatMessage.from_dict(d) for d in template] return default_from_dict(cls, data) diff --git a/releasenotes/notes/fix-chat-prompt-builder-from-dict-template-none-56c91effe61e823c.yaml b/releasenotes/notes/fix-chat-prompt-builder-from-dict-template-none-56c91effe61e823c.yaml new file mode 100644 index 0000000000..9cb1ae0f38 --- /dev/null +++ b/releasenotes/notes/fix-chat-prompt-builder-from-dict-template-none-56c91effe61e823c.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fix ChatPromptBuilder from_dict method when template value is None. diff --git a/test/components/builders/test_chat_prompt_builder.py b/test/components/builders/test_chat_prompt_builder.py index 8bc46edf10..4fa0870f62 100644 --- a/test/components/builders/test_chat_prompt_builder.py +++ b/test/components/builders/test_chat_prompt_builder.py @@ -535,3 +535,15 @@ def test_from_dict(self): ] assert component._variables == ["var", "required_var"] assert component._required_variables == ["required_var"] + + def test_from_dict_template_none(self): + component = ChatPromptBuilder.from_dict( + data={ + "type": "haystack.components.builders.chat_prompt_builder.ChatPromptBuilder", + "init_parameters": {"template": None}, + } + ) + + assert component.template is None + assert component._variables is None + assert component._required_variables is None