• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deepset-ai / haystack / 15206349492

23 May 2025 08:53AM UTC coverage: 90.194% (-0.009%) from 90.203%
15206349492

Pull #9431

github

web-flow
Merge 216c432e2 into 3342f17f0
Pull Request #9431: feat: Improve formatting in print streaming chunk

11231 of 12452 relevant lines covered (90.19%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.29
haystack/components/generators/utils.py
1
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
from openai.types.chat.chat_completion_chunk import ChoiceDeltaToolCall
1✔
6

7
from haystack.dataclasses import StreamingChunk
1✔
8

9

10
def print_streaming_chunk(chunk: StreamingChunk) -> None:
1✔
11
    """
12
    Callback function to handle and display streaming output chunks.
13

14
    This function processes a `StreamingChunk` object by:
15
    - Printing tool call metadata (if any), including function names and arguments, as they arrive.
16
    - Printing tool call results when available.
17
    - Printing the main content (e.g., text tokens) of the chunk as it is received.
18

19
    The function outputs data directly to stdout and flushes output buffers to ensure immediate display during
20
    streaming.
21

22
    :param chunk: A chunk of streaming data containing content and optional metadata, such as tool calls and
23
        tool results.
24
    """
25
    # Print tool call metadata if available (from ChatGenerator)
26
    if chunk.meta.get("tool_calls"):
×
27
        for tool_call in chunk.meta["tool_calls"]:
×
28
            # Convert to dict if tool_call is a ChoiceDeltaToolCall
29
            if isinstance(tool_call, ChoiceDeltaToolCall):
×
30
                tool_call_dict = tool_call.to_dict()
×
31
            else:
32
                tool_call_dict = tool_call
×
33

34
            if tool_call_dict.get("function"):
×
35
                # print the tool name
36
                if tool_call_dict["function"].get("name"):
×
37
                    print("\n\n[TOOL CALL]\n", flush=True, end="")
×
38
                    print(f"Tool: {tool_call_dict['function']['name']} ", flush=True, end="")
×
39
                    print("\nArguments: ", flush=True, end="")
×
40

41
                # print the tool arguments
42
                if tool_call_dict["function"].get("arguments"):
×
43
                    print(tool_call_dict["function"]["arguments"], flush=True, end="")
×
44

45
    # Print tool call results if available (from ToolInvoker)
46
    if chunk.meta.get("tool_result"):
×
47
        print(f"\n\n[TOOL RESULT]\n{chunk.meta['tool_result']}", flush=True, end="")
×
48

49
    # Print the main content of the chunk (from ChatGenerator)
50
    if chunk.content:
×
51
        print(chunk.content, flush=True, end="")
×
52

53
    # End of LLM assistant message so we add two new lines
54
    # This ensures spacing between multiple LLM messages (e.g. Agent)
55
    if chunk.meta.get("finish_reason") is not None:
×
56
        print("\n\n", flush=True, end="")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc