Skip to content

.Net: Function Arguments Is Empty #9212

Closed

Description

@jichulu

arguments.Append(update.FunctionArgumentsUpdate.ToString());

This code should check FunctionArgumentsUpdate if IsEmpty.

if (update.FunctionArgumentsUpdate is not null && !update.FunctionArgumentsUpdate.ToMemory().IsEmpty)
{
...
}

Log

Trace: [2024/10/11 21:25:51] Request: POST https://xxx.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview
Accept: application/json
User-Agent: azsdk-net-AI.OpenAI/2.1.0-beta.1, (.NET 8.0.8; Microsoft Windows 10.0.22631)
x-ms-client-request-id: 0ae8cafc-d86d-48bb-a39c-b0ce1c7f9bc0
api-key: ***
Content-Type: application/json

{"messages":[{"role":"system","content":"You are a smart weather assistant"},{"role":"user","content":"Guangzhou weather"}],"model":"gpt-4o","frequency_penalty":0,"presence_penalty":0,"stream":true,"temperature":0.7,"top_p":1,"tools":[{"type":"function","function":{"description":"Get city weather based on area code","name":"WeatherPlugin-GetWeather","parameters":{"type":"object","required":["addressCode"],"properties":{"addressCode":{"type":"string","description":"City area address code. e.g. 110000, 440100..."}}}}}],"tool_choice":"auto","max_tokens":2048}

Trace: [2024/10/11 21:25:53] /openai/deployments/gpt-4o/chat/completions OK
Transfer-Encoding: chunked
...
Content-Type: text/event-stream; charset=utf-8

Trace: [2024/10/11 21:25:53] 
data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_yXkmOQ1GvYheU9tVolJngmLa","type":"function","function":{"name":"WeatherPlugin-GetWeather","arguments":""}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\n"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" "}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" \""}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"address"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Code"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" \""}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"440"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"100"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"\n"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"}"}}]},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-AH9wO192nxDoDKnTwpgdLCtAYLkjp","object":"chat.completion.chunk","created":1728653152,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_67802d9a6d","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}]}

data: [DONE]



Error: [2024/10/11 21:25:53]
System.ArgumentNullException: Value cannot be null. (Parameter 'bytes')
   at System.ArgumentNullException.Throw(String paramName)
   at System.Text.Encoding.GetString(Byte* bytes, Int32 byteCount)
   at System.BinaryData.ToString()
   at Microsoft.SemanticKernel.Connectors.OpenAI.OpenAIFunctionToolCall.TrackStreamingToolingUpdate(IReadOnlyList`1 updates, Dictionary`2& toolCallIdsByIndex, Dictionary`2& functionNamesByIndex, Dictionary`2& functionArgumentBuildersByIndex)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)+MoveNext()
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)+MoveNext()
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetStreamingChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
...

Activity

  1. changed the title [-]Function Arguments Is Empty[/-] [+].Net: Function Arguments Is Empty[/+] on Oct 11, 2024
  2. jichulu commented on Oct 11, 2024

    @jichulu
    ContributorAuthor

    Update

    if (string.IsNullOrEmpty(functionCallUpdate.ToolCallId) &&
    string.IsNullOrEmpty(functionCallUpdate.FunctionName) &&
    (functionCallUpdate.FunctionArgumentsUpdate is null || functionCallUpdate.FunctionArgumentsUpdate.ToMemory().IsEmpty))
    {
    continue;
    }
    openAIStreamingChatMessageContent.Items.Add(new StreamingFunctionCallUpdateContent(
    callId: functionCallUpdate.ToolCallId,
    name: functionCallUpdate.FunctionName,
    arguments: functionCallUpdate.FunctionArgumentsUpdate?.ToString(),
    functionCallIndex: functionCallUpdate.Index));

    to

    var isEmpty = (functionCallUpdate.FunctionArgumentsUpdate is null || functionCallUpdate.FunctionArgumentsUpdate.ToMemory().IsEmpty);
    if (string.IsNullOrEmpty(functionCallUpdate.ToolCallId) &&
        string.IsNullOrEmpty(functionCallUpdate.FunctionName) &&
        isEmpty)
    {
        continue;
    }
    string? _arguments = isEmpty ? null : functionCallUpdate.FunctionArgumentsUpdate?.ToString();
    openAIStreamingChatMessageContent.Items.Add(new StreamingFunctionCallUpdateContent(
        callId: functionCallUpdate.ToolCallId,
        name: functionCallUpdate.FunctionName,
        arguments: _arguments,
        functionCallIndex: functionCallUpdate.Index));
    

    Update

    if (update.FunctionArgumentsUpdate is not null)
    {
    if (!(functionArgumentBuildersByIndex ??= []).TryGetValue(update.Index, out StringBuilder? arguments))
    {
    functionArgumentBuildersByIndex[update.Index] = arguments = new();
    }
    arguments.Append(update.FunctionArgumentsUpdate.ToString());
    }

    to

    if (update.FunctionArgumentsUpdate is not null && !update.FunctionArgumentsUpdate.ToMemory().IsEmpty)
    {
        if (!(functionArgumentBuildersByIndex ??= []).TryGetValue(update.Index, out StringBuilder? arguments))
        {
            functionArgumentBuildersByIndex[update.Index] = arguments = new();
        }
    
        arguments.Append(update.FunctionArgumentsUpdate.ToString());
    }
    
  3. rogerbarreto commented on Oct 15, 2024

    @rogerbarreto
    Member

    jichulu, Thanks for the contribution!

    I added some UnitTests to the provided PR, but interestingly enough the payload provided in this issue didn't cover throw any argument null exception as you mentioned.

    I would appreciate if you can provide a payload that we can replicate the issue.

  4. added a commit that references this issue on Oct 16, 2024
    8f8c768
  5. moved this from Bug to Sprint: Done in Semantic Kernelon Oct 16, 2024
  6. jichulu commented on Oct 21, 2024

    @jichulu
    ContributorAuthor

    Roger Barreto (@rogerbarreto) I've found the specific reason. It's not exactly SK's bug, but SK can avoid it.

    is caused by the version of the package System.Memory.Data.
    The package Azure.AI.OpenAI[2.1.0-beta.1] references System.Memory.Data, Version=1.0.2.0, . NETStandard,Version=v2.0。

    My project uses the package Microsoft.Extensions.Azure, which references System.Memory.Data, Version=6.0.0.0, . NETCoreApp,Version=v6.0, so the null reference issue of System.BinaryData.ToString() is triggered.

    Upgrading System.Memory.Data to the latest version resolves the issue.
    But projects that use .NET 6.0 may still have problems.

    Steps to reproduce: Reference package <PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.6" /> or <PackageVersion Include="System.Memory.Data" Version="6.0.0" /> in the project "Connectors.AzureOpenAI.UnitTests"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETIssue or Pull requests regarding .NET codebugSomething isn't working

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions