44from typing import Any , Literal
55
66from haystack import component , default_from_dict , default_to_dict
7+ from haystack .components .generators .utils import _normalize_messages
78from haystack .dataclasses import (
89 AsyncStreamingCallbackT ,
910 ChatMessage ,
@@ -577,7 +578,7 @@ async def _chat_async(
577578 @component .output_types (replies = list [ChatMessage ])
578579 def run (
579580 self ,
580- messages : list [ChatMessage ],
581+ messages : list [ChatMessage ] | str ,
581582 generation_kwargs : dict [str , Any ] | None = None ,
582583 tools : ToolsType | None = None ,
583584 * ,
@@ -587,7 +588,8 @@ def run(
587588 Runs an Ollama Model on a given chat history.
588589
589590 :param messages:
590- A list of ChatMessage instances representing the input messages.
591+ A list of ChatMessage instances representing the input messages. If a string is provided, it is converted
592+ to a list containing a ChatMessage with user role.
591593 :param generation_kwargs:
592594 Per-call overrides for Ollama inference options.
593595 These are merged on top of the instance-level `generation_kwargs`.
@@ -603,6 +605,7 @@ def run(
603605 :returns: A dictionary with the following keys:
604606 - `replies`: A list of ChatMessages containing the model's response
605607 """
608+ messages = _normalize_messages (messages )
606609
607610 # Validate and select the streaming callback
608611 callback = select_streaming_callback (
@@ -636,7 +639,7 @@ def run(
636639 @component .output_types (replies = list [ChatMessage ])
637640 async def run_async (
638641 self ,
639- messages : list [ChatMessage ],
642+ messages : list [ChatMessage ] | str ,
640643 generation_kwargs : dict [str , Any ] | None = None ,
641644 tools : ToolsType | None = None ,
642645 * ,
@@ -646,7 +649,8 @@ async def run_async(
646649 Async version of run. Runs an Ollama Model on a given chat history.
647650
648651 :param messages:
649- A list of ChatMessage instances representing the input messages.
652+ A list of ChatMessage instances representing the input messages. If a string is provided, it is converted
653+ to a list containing a ChatMessage with user role.
650654 :param generation_kwargs:
651655 Per-call overrides for Ollama inference options.
652656 These are merged on top of the instance-level `generation_kwargs`.
@@ -659,6 +663,8 @@ async def run_async(
659663 :returns: A dictionary with the following keys:
660664 - `replies`: A list of ChatMessages containing the model's response
661665 """
666+ messages = _normalize_messages (messages )
667+
662668 # Validate and select the streaming callback
663669 callback = select_streaming_callback (self .streaming_callback , streaming_callback , requires_async = True )
664670
0 commit comments