deepset-ai / deepset-ai/haystack

Explore adding support for tool calling in `HuggingFaceAPIChatGenerator` when streaming

Open
#9,369 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P3
Dominant language
Python
Stars
26.6k
Forks
3.2k
Avg merge
1d 3h
Merged PRs (30d)
194

Description

Describe the Feature
It would be great to add support for tool calling when running HuggingFaceAPIChatGenerator in streaming mode.

As shown here https://github.com/deepset-ai/haystack/blob/2ccdba3e99024072c69b4752a6478284813dd182/haystack/components/generators/chat/hugging_face_api.py#L411-L412

we only process the generated text here and only store it as text content here https://github.com/deepset-ai/haystack/blob/2ccdba3e99024072c69b4752a6478284813dd182/haystack/components/generators/chat/hugging_face_api.py#L436

whereas we should properly populate the tool_calls param of ChatMessage if a tool call is present.

The underlying HuggingFace streaming chunk dataclass does contain tool call information

@dataclass_with_extra
class ChatCompletionStreamOutputDelta(BaseInferenceType):
    role: str
    content: Optional[str] = None
    tool_call_id: Optional[str] = None
    tool_calls: Optional[List[ChatCompletionStreamOutputDeltaToolCall]] = None

Additional context
It looks like _run_streaming would need to be updated to process tool calling streaming chunks.

To Reproduce

from haystack.tools import Tool
from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat.hugging_face_api import HuggingFaceAPIChatGenerator
from haystack.components.generators.utils import print_streaming_chunk

def get_weather(city: str) -> str:
    """Get weather information for a city."""
    return f"The weather in {city} is Sunny and 22 C"

tool_parameters = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
tool = Tool(
    name="weather",
    description="useful to determine the weather in a given location",
    parameters=tool_parameters,
    function=get_weather,
)

chat_messages = [ChatMessage.from_user("What's the weather like in Paris?")]
generator = HuggingFaceAPIChatGenerator(
    api_type=HFGenerationAPIType.SERVERLESS_INFERENCE_API,
    api_params={"model": "NousResearch/Hermes-3-Llama-3.1-8B"},
    generation_kwargs={"temperature": 0.5},
    streaming_callback=print_streaming_chunk,
)
results = generator.run(chat_messages, tools=[tool])

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading _run_streaming in haystack/components/generators/chat/hugging_face_api.py, especially the referenced lines where generated text is processed and stored in ChatMessage. Run the provided weather-tool reproduction to inspect streaming chunks and their tool call fields. Done means streaming tool-call chunks populate ChatMessage.tool_calls rather than only text content.

Written by the indexing model from the issue text.

Assessment

Tech stack
huggingface, python
Domain
ai, api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.