66from haystack .dataclasses import (
77 ChatMessage ,
88 ChatRole ,
9+ ComponentInfo ,
910 StreamingChunk ,
1011 TextContent ,
1112 ToolCall ,
@@ -287,6 +288,9 @@ def test_to_dict(self):
287288 "type" : "string" ,
288289 },
289290 },
291+ "outputs_to_string" : None ,
292+ "inputs_from_state" : None ,
293+ "outputs_to_state" : None ,
290294 },
291295 },
292296 ],
@@ -297,15 +301,6 @@ def test_to_dict(self):
297301 },
298302 }
299303
300- # add outputs_to_string, inputs_from_state and outputs_to_state tool parameters for compatibility with
301- # haystack-ai>=2.12.0
302- if hasattr (tool , "outputs_to_string" ):
303- expected_dict ["init_parameters" ]["tools" ][0 ]["data" ]["outputs_to_string" ] = tool .outputs_to_string
304- if hasattr (tool , "inputs_from_state" ):
305- expected_dict ["init_parameters" ]["tools" ][0 ]["data" ]["inputs_from_state" ] = tool .inputs_from_state
306- if hasattr (tool , "outputs_to_state" ):
307- expected_dict ["init_parameters" ]["tools" ][0 ]["data" ]["outputs_to_state" ] = tool .outputs_to_state
308-
309304 assert data == expected_dict
310305
311306 def test_from_dict (self ):
@@ -365,6 +360,30 @@ def test_from_dict(self):
365360 "properties" : {"name" : {"type" : "string" }, "age" : {"type" : "number" }},
366361 }
367362
363+ def test_build_chunk (self ):
364+ generator = OllamaChatGenerator ()
365+
366+ mock_chunk_response = Mock ()
367+ mock_chunk_response .model_dump .return_value = {
368+ "message" : {"role" : "assistant" , "content" : "Hello world" },
369+ "model" : "llama2" ,
370+ "created_at" : "2023-12-12T14:13:43.416799Z" ,
371+ "done" : False ,
372+ }
373+
374+ component_info = ComponentInfo .from_component (generator )
375+
376+ chunk = generator ._build_chunk (mock_chunk_response , component_info )
377+
378+ assert isinstance (chunk , StreamingChunk )
379+ assert chunk .content == "Hello world"
380+ assert chunk .component_info == component_info
381+ assert chunk .meta ["role" ] == "assistant"
382+ assert chunk .meta ["model" ] == "llama2"
383+ assert chunk .meta ["created_at" ] == "2023-12-12T14:13:43.416799Z"
384+ assert chunk .meta ["done" ] is False
385+ assert "tool_calls" not in chunk .meta
386+
368387 @patch ("haystack_integrations.components.generators.ollama.chat.chat_generator.Client" )
369388 def test_run (self , mock_client ):
370389 generator = OllamaChatGenerator ()
@@ -407,11 +426,10 @@ def test_run(self, mock_client):
407426
408427 @patch ("haystack_integrations.components.generators.ollama.chat.chat_generator.Client" )
409428 def test_run_streaming (self , mock_client ):
410- streaming_callback_called = False
429+ collected_chunks = []
411430
412- def streaming_callback (_ : StreamingChunk ) -> None :
413- nonlocal streaming_callback_called
414- streaming_callback_called = True
431+ def streaming_callback (chunk : StreamingChunk ) -> None :
432+ collected_chunks .append (chunk )
415433
416434 generator = OllamaChatGenerator (streaming_callback = streaming_callback )
417435
@@ -443,7 +461,16 @@ def streaming_callback(_: StreamingChunk) -> None:
443461
444462 result = generator .run (messages = [ChatMessage .from_user ("irrelevant" )])
445463
446- assert streaming_callback_called
464+ assert len (collected_chunks ) == 2
465+ assert collected_chunks [0 ].content == "first chunk "
466+ assert collected_chunks [1 ].content == "second chunk"
467+
468+ for chunk in collected_chunks :
469+ assert (
470+ chunk .component_info .type
471+ == "haystack_integrations.components.generators.ollama.chat.chat_generator.OllamaChatGenerator"
472+ )
473+ assert chunk .component_info .name is None # not in a pipeline
447474
448475 assert "replies" in result
449476 assert len (result ["replies" ]) == 1
0 commit comments