Skip to content

Streaming Tool Calling

Stream chat completion responses with tool calling using the IChatClient interface.

This example assumes using HuggingFace; is in scope and apiKey contains your HuggingFace API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using var client = new HuggingFaceInferenceClient(apiKey);
IChatClient chatClient = client;

// Stream chat responses with tool calling using the MEAI IChatClient interface.
// Define a simple tool that returns the current weather.
var tool = AIFunctionFactory.Create(
    (string location) => $"The weather in {location} is sunny, 72°F.",
    "get_weather",
    "Gets the current weather for a location.");

var functionCalls = new List<FunctionCallContent>();
ChatFinishReason? finishReason = null;

await foreach (var update in chatClient.GetStreamingResponseAsync(
    [new ChatMessage(ChatRole.User, "What's the weather in San Francisco?")],
    new ChatOptions
    {
        ModelId = "Qwen/Qwen2.5-Coder-32B-Instruct",
        Tools = [tool],
    }))
{
    functionCalls.AddRange(update.Contents.OfType<FunctionCallContent>());

    if (update.FinishReason is not null)
    {
        finishReason = update.FinishReason;
    }
}

// The model should respond with tool calls.