Skip to content

OpenAIChatClient streaming tool calls may be broken #5567

Description

@SteveSandersonMS

Description

As of today, some app code that was working before is now failing with:

System.ArgumentNullException: Value cannot be null. (Parameter 'bytes')
2024-10-24T15:00:53          at System.ArgumentNullException.Throw(String paramName)
2024-10-24T15:00:53          at System.Text.StringBuilder.Append(Object value)
2024-10-24T15:00:53          at Microsoft.Extensions.AI.OpenAIChatClient.CompleteStreamingAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)+MoveNext()
2024-10-24T15:00:53          at Microsoft.Extensions.AI.OpenAIChatClient.CompleteStreamingAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)+MoveNext()
2024-10-24T15:00:53          at Microsoft.Extensions.AI.OpenAIChatClient.CompleteStreamingAsync(IList`1 chatMessages, ChatOptions options, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()

this happens if and only if I try to do function calling with CompleteStreamingAsync. It seems that the FunctionArgumentsUpdate may now be a BinaryData instance that internally holds an empty _data value. This causes the exception.

Image

Perhaps the underlying OpenAI/AzureOpenAI service has started returning data in a different form.

Or perhaps I've caused this in my own code by some hackery. But I've got a talk to do in a few hours so right now I'm just keeping track of this and putting in this workaround into CompleteStreamingAsync:

if (toolCallUpdate.FunctionArgumentsUpdate is not null
    /* workaround */ && toolCallUpdate.FunctionArgumentsUpdate.ToArray().Length > 0)
{
    _ = (existing.Arguments ??= new()).Append(toolCallUpdate.FunctionArgumentsUpdate);
}

Reproduction Steps

This is in eShopSupport.

If this doesn't repro with other streaming function calling, I'll come back to work out more repro details.

Expected behavior

No exception

Actual behavior

Exception

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.work in progress 🚧

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions