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

deepset-ai / haystack / 14929518756

09 May 2025 01:00PM UTC coverage: 90.37% (-0.05%) from 90.417%
14929518756

Pull #9365

github

web-flow
Merge 78e2cff97 into 2ccdba3e9
Pull Request #9365: feat: Update `print_streaming_chunk` work with dict version of `ChoiceDeltaToolCall`

10914 of 12077 relevant lines covered (90.37%)

0.9 hits per line

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

13.64
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
            if isinstance(tool_call, ChoiceDeltaToolCall):
×
29
                tool_call_dict = tool_call.to_dict()
×
30
            else:
31
                tool_call_dict = tool_call
×
32

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

39
                # print the tool arguments
40
                if tool_call_dict["function"].get("arguments"):
×
41
                    if tool_call_dict["function"]["arguments"].startswith("{"):
×
42
                        print("\nArguments: ", flush=True, end="")
×
43
                    print(tool_call_dict["function"]["arguments"], flush=True, end="")
×
44
                    if tool_call_dict["function"]["arguments"].endswith("}"):
×
45
                        print("\n\n", flush=True, end="")
×
46

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

51
    # Print the main content of the chunk (from ChatGenerator)
52
    if chunk.content:
×
53
        print(chunk.content, 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