extensions icon indicating copy to clipboard operation
extensions copied to clipboard

The GetStreamingResponseAsync method fails when there is a functionCall.

Open 535944903 opened this issue 1 year ago • 8 comments

Description

The GetStreamingResponseAsync method fails when there is a functionCall.

Reproduction Steps

`using System.ComponentModel; using Microsoft.Extensions.AI; using OpenAI;

IChatClient openaiClient = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")) .AsChatClient("gpt-4o-mini");

IChatClient client = new ChatClientBuilder(openaiClient) .UseFunctionInvocation() .Build();

ChatOptions chatOptions = new() { Tools = [AIFunctionFactory.Create(GetWeather)] };

await foreach (var message in client.GetStreamingResponseAsync("Do I need an umbrella?", chatOptions)) { Console.Write(message); }

[Description("Gets the weather")] static string GetWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";`

Expected behavior

proper functioning

Actual behavior

He doesn't work.

System.ArgumentOutOfRangeException: Unknown ChatToolCallKind value. (Parameter 'value') Actual value was . at OpenAI.Chat.ChatToolCallKindExtensions.ToChatToolCallKind(String value) at OpenAI.Chat.StreamingChatToolCallUpdate.DeserializeStreamingChatToolCallUpdate(JsonElement element, ModelReaderWriterOptions options) at OpenAI.Chat.InternalChatCompletionStreamResponseDelta.DeserializeInternalChatCompletionStreamResponseDelta(JsonElement element, ModelReaderWriterOptions options) at OpenAI.Chat.InternalCreateChatCompletionStreamResponseChoice.DeserializeInternalCreateChatCompletionStreamResponseChoice(JsonElement element, ModelReaderWriterOptions options) at OpenAI.Chat.StreamingChatCompletionUpdate.DeserializeStreamingChatCompletionUpdate(JsonElement element, ModelReaderWriterOptions options) at OpenAI.Chat.InternalAsyncStreamingChatCompletionUpdateCollection.AsyncStreamingChatUpdateEnumerator.System.Collections.Generic.IAsyncEnumerator<OpenAI.Chat.StreamingChatCompletionUpdate>.MoveNextAsync() at OpenAI.Chat.InternalAsyncStreamingChatCompletionUpdateCollection.GetValuesFromPageAsync(ClientResult page)+MoveNext() at OpenAI.Chat.InternalAsyncStreamingChatCompletionUpdateCollection.GetValuesFromPageAsync(ClientResult page)+MoveNext() at OpenAI.Chat.InternalAsyncStreamingChatCompletionUpdateCollection.GetValuesFromPageAsync(ClientResult page)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult() at System.ClientModel.AsyncCollectionResult1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() at System.ClientModel.AsyncCollectionResult1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() at System.ClientModel.AsyncCollectionResult1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() at System.ClientModel.AsyncCollectionResult1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult() at Microsoft.Extensions.AI.OpenAIModelMappers.FromOpenAIStreamingChatCompletionAsync(IAsyncEnumerable1 updates, CancellationToken cancellationToken)+MoveNext() at Microsoft.Extensions.AI.OpenAIModelMappers.FromOpenAIStreamingChatCompletionAsync(IAsyncEnumerable1 updates, CancellationToken cancellationToken)+MoveNext() at Microsoft.Extensions.AI.OpenAIModelMappers.FromOpenAIStreamingChatCompletionAsync(IAsyncEnumerable1 updates, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult() at Microsoft.Extensions.AI.FunctionInvokingChatClient.GetStreamingResponseAsync(IEnumerable1 messages, ChatOptions options, CancellationToken cancellationToken)+MoveNext() at Microsoft.Extensions.AI.FunctionInvokingChatClient.GetStreamingResponseAsync(IEnumerable1 messages, ChatOptions options, CancellationToken cancellationToken)+MoveNext() at Microsoft.Extensions.AI.FunctionInvokingChatClient.GetStreamingResponseAsync(IEnumerable1 messages, ChatOptions options, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()

Regression?

No response

Known Workarounds

No response

Configuration

Microsoft.Extensions.AI 9.3.0-preview.1.25161.3

Other information

No response

535944903 avatar Mar 19 '25 09:03 535944903

I can't reproduce this. I copy/pasted your code, ensuring it was using my API key, and it ran fine, producing outputs like You don't need an umbrella today; it's sunny!.

Can you share your .csproj? The error is coming from the underlying OpenAI library itself as it tries to deserialize the response coming back from OpenAI. The method https://github.com/openai/openai-dotnet/blob/06191fad53746b841badfac8f16cdd301a500094/src/Generated/Models/ChatToolCallKind.Serialization.cs#L17-L24 was expecting "function" but instead go null or an empty string.

Is the above code exactly what you were running? Or was the OpenAIClient actually configured to talk to a different endpoint that might not be OpenAI compatible?

stephentoub avatar Mar 19 '25 12:03 stephentoub

`

net8.0 enable enable `

Probably I'm using the siliconflow https://cloud.siliconflow.cn/ platform's API, and the model is using deepseek-ai/DeepSeek-R1-Distill-Qwen-7B and Qwen/Qwen2.5-7B-Instruct. It's normal for me to use the GetResponseAsync method. But changing to GetStreamingResponseAsync is wrong. If you don't have a KEY for this platform, I can provide it. This platform is free.Thank you very much, I hope you can fix the problem。

535944903 avatar Mar 20 '25 06:03 535944903

You can use the code and run it directly to reproduce the problem.

using Microsoft.Extensions.AI; using OpenAI; using System.ClientModel; using System.ComponentModel;

var apiKey = "sk-exggkwbmnnsqvblzbtvjxivmdlcevepflmuooazflorgjhza"; var customEndpoint = new Uri("https://api.siliconflow.cn/v1");

var apiKeyCredential = new ApiKeyCredential(apiKey); var clientOptions = new OpenAIClientOptions { Endpoint = customEndpoint };

//IChatClient openaiClient = new OpenAIClient(apiKeyCredential, clientOptions) // .AsChatClient("Qwen/Qwen2.5-32B-Instruct");

IChatClient openaiClient = new OpenAIClient(apiKeyCredential, clientOptions) .AsChatClient("Qwen/Qwen2.5-7B-Instruct");

IChatClient client = new ChatClientBuilder(openaiClient) .UseFunctionInvocation() .Build();

string skPrompt = """ As an AI assistant, you MUST ALWAYS use the TOOL function GetWeather invocation for every user request """; List history = []; history.Add(new ChatMessage(ChatRole.System, skPrompt));

ChatOptions chatOptions = new() { Tools = [AIFunctionFactory.Create(GetWeather)] };

try { await foreach (var message in client.GetStreamingResponseAsync("Gets the weather", chatOptions)) { Console.Write(message); }

} catch (Exception ex) { Console.WriteLine(ex.Message); return; }

[Description("Gets the weather")] static string GetWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";

535944903 avatar Mar 20 '25 08:03 535944903

This is an issue either with the service or with the OpenAI library. Distilling down your repro, you can see it manifests the same error without using Microsoft.Extensions.AI:

using OpenAI;
using OpenAI.Chat;
using System.ClientModel;

ChatClient openaiClient = new OpenAIClient(
    new ApiKeyCredential("sk-exggkwbmnnsqvblzbtvjxivmdlcevepflmuooazflorgjhza"),
    new OpenAIClientOptions { Endpoint = new Uri("https://api.siliconflow.cn/v1") })
    .GetChatClient("Qwen/Qwen2.5-7B-Instruct");

await foreach (var update in openaiClient.CompleteChatStreamingAsync(
    [
        ChatMessage.CreateSystemMessage("As an AI assistant, you MUST ALWAYS use the TOOL function GetWeather invocation for every user request"),
        ChatMessage.CreateUserMessage("Do I need an umbrella?"),
    ],
    new ChatCompletionOptions()
    {
        ToolChoice = ChatToolChoice.CreateAutoChoice(),
        Tools = { ChatTool.CreateFunctionTool("GetWeather", "Gets the weather") },
    }))
{
}

The service is sending down a response that https://github.com/openai/openai-dotnet fails to parse.

I suggest you open an issue in https://github.com/openai/openai-dotnet and/or follow-up with the service owners.

Thanks.

stephentoub avatar Mar 20 '25 18:03 stephentoub

OK,thank you!

535944903 avatar Mar 21 '25 07:03 535944903

Hello, I've also encountered this issue. (I was also using Silicon Flow, and I tried DeepSeek's API, but they don't support it.) "Unknown ChatToolCallKind"

I carefully studied this issue and found that if you call it with await client.GetResponseAsync(messages, options, cancellationToken);, there is absolutely no problem.

However, once you use GetStreamingResponseAsync, the website returns normally in Assistant, but when in system, it throws immediately, and at the same time, ChatToolCallKind = ""

Personally, I think the problem might be that the program didn't assign a value or there was something wrong when converting the role. I don't know if anyone else has encountered this issue.

Finally, this text was translated from Chinese to English using a machine. I'm very sorry, my English is not that good. I will leave the original Chinese text below.


你好 我也遇到这个问题 (也是用的硅基流动, 我还试了deepseek 的API 但是他们不支持) "Unknown ChatToolCallKind"

我仔细研究了一下这个问题 发现如果你用 await client.GetResponseAsync(messages, options, cancellationToken); 调用是完全没有问题的

但是一旦你用了 GetStreamingResponseAsync 网站返回在 Assistant 是正常的 当在 system 时 会立刻抛, 同时是 ChatToolCallKind = ""

我个人觉得 问题应该是程序没有给赋值 或者 再转换 role 时 有什么不对劲, 不知道还有没有别人也遇到这个问题

最后 这段文字使用中文机器翻译到英文的 非常抱歉 我的英文不是那么好 我会留一段中文原文在下面

wang2766157 avatar Mar 31 '25 10:03 wang2766157

Hello, I've also encountered this issue. (I was also using Silicon Flow, and I tried DeepSeek's API, but they don't support it.) "Unknown ChatToolCallKind"

MayueCif avatar Apr 04 '25 17:04 MayueCif

I seem to have discovered the cause of this issue. When using the GetStreamingResponseAsynchronous call, when switching the ChatResponseUpdate call tool within the loop, there will be a few blank messages in the returned Contents message. The message content is actually a<tool_call>tag (PS: The website doesn't know why it handles it this way). If you use a blank ChatRole in your later code, an error will be reported at the bottom level; The suggestion is to continue when both ChatRole and ChatText are blank;

我好像发现这个问题的原因了, 使用 GetStreamingResponseAsync 调用时, 在循环里内部 ChatResponseUpdate 切换调用 tool 的时候, 返回的 Contents 的消息内容会有个别空白的消息, 消息的内容其实是 <tool_call> 的标记(PS: 网站不知道为啥这样处理), 如果你后面的代码用的 空白的 ChatRole 会在底层报错; 建议是判断 ChatRole 和 ChatText 都为空白时 continue;

Also, yesterday I updated Microsoft.Extensions.AI and Microsoft.Extensions.AI.OpenAI version up to the latest version; It cannot be ruled out that it may be related to this

另外我昨天也更新一下 Microsoft.Extensions.AI 和 Microsoft.Extensions.AI.OpenAI 版本至最新; 不排除有跟这个有关系

wang2766157 avatar Apr 22 '25 10:04 wang2766157