Using Responses API with streaming and function call in .Net OpenAPI (C# SDK 2.2.0-beta.4)

Hi! I am trying to set up a ChatGPT solution in my .Net API, using streaming and function calls with the Responses API. I see this as fantastic AI-solution for my project. But code samples for this in .Net is not easy to find (yet). I hope Responses streaming+function call will be added to the github “How to”-demos…

The thing I now struggle with is the function part, more specifically how to return a function result back in the “streaming loop”. I got it to work without streaming, but with streaming I cannot figure out these things - really appreciate if someone has any tips :slight_smile:

My c# code basically looks like this:

//Main loop after getting a prompt from user
await foreach (StreamingResponseUpdate update in
_responseClient.CreateResponseStreamingAsync(userInputText: prompt.UserMessage,
              responseOptions))
{
    if (update is StreamingResponseCreatedUpdate created)
    {
       //Must remember this for later function calls (I guess):
       newResponseId = created.Response.Id;
     }
     if (update is ...)
     ...
     else if (update is StreamingResponseFunctionCallArgumentsDoneUpdate funcCall)
     {
         //I guess this is the right place to call the function
        string toolResult = CallMyFunction(funcCall.Arguments)

        //I create new Input Items with function results:
        var inputItems = new List<ResponseItem>();
        inputItems.Add(new FunctionCallOutputResponseItem(funcCall.ItemId, toolResult));

        //New loop for calling OpenAI with function results:
        await foreach (StreamingResponseUpdate updateFunc in _responseClient.CreateResponseStreamingAsync(inputItems, new ResponseCreationOptions
                            {                                
                                PreviousResponseId = newResponseId,
                                Tools = { getCurrentWeatherTool },
                            }))
        {
           //this code is never reached, the CreateResponseStreamingAsync call above creates an  exception
        }
    }    
}

I get exception “No tool output found for function call call…xxx”

I don’t know what I am missing here, I hope it is just some syntax problem, maybe my function result should not be returned in StreamingResponseFunctionCallArgumentsDoneUpdate at all, but I can’t find another logical place to execute the function and return the result. I guess I also should have a “CallId”, but there is no such thing in the StreamingResponseFunctionCallArgumentsDoneUpdate class.

Or maybe this is simply not working in this beta :grimacing:

Do you still need help with this one?

It is a bit more complex, i manage to make it work, something like this:

{...
AsyncCollectionResult<StreamingResponseUpdate> updates = responseClient.CreateResponseStreamingAsync([systemMessage, userMessage], responseCreationOptions, cancellationToken);
responseId = await GetUpdates(responseClient, userId, updates, cancellationToken);

}


private async Task<string> GetUpdates(OpenAIResponseClient responseClient, Guid userId, AsyncCollectionResult<StreamingResponseUpdate> updates, CancellationToken cancellationToken, bool isFunctionCall = false)
{
    string responseId = null;
    bool firstRun = true;
    Dictionary<string, (string CallId, string FunctionName)> functionsCalling = [];
    List<FunctionCallOutputResponseItem> toolOutputs = [];
    await foreach (StreamingResponseUpdate streamingUpdate in updates)
    {
        if (streamingUpdate is StreamingResponseCreatedUpdate createdUpdate)
        {
            responseId = createdUpdate.Response.Id;
            if (!isFunctionCall)
            {
                await LogMessage("Run started!");
            }
        }
        if (toolOutputs.Count == 0 && streamingUpdate is StreamingResponseCompletedUpdate)
        {
            await LogMessage("Finished");
        }
        if (streamingUpdate is StreamingResponseOutputTextDeltaUpdate deltaUpdate)
        {
            if (firstRun)
            {
                await LogMessage("Sending data...");
                firstRun = false;
            }

            await StreamResponse(deltaUpdate.Delta);
        }
        if (streamingUpdate is StreamingResponseOutputItemAddedUpdate itemAdded && itemAdded.Item is FunctionCallResponseItem functionCallResponseItem)
        {
            functionsCalling[functionCallResponseItem.Id] = (functionCallResponseItem.CallId, functionCallResponseItem.FunctionName);
        }
        if (streamingUpdate is StreamingResponseFunctionCallArgumentsDoneUpdate functionCall)
        {
            if (functionsCalling.ContainsKey(functionCall.ItemId))
            {
                FunctionCallOutputResponseItem result = await GetFunctionOutput(functionsCalling[functionCall.ItemId].CallId, functionsCalling[functionCall.ItemId].FunctionName, functionCall.Arguments);
                toolOutputs.Add(result);
            }
        }
    }

    if (toolOutputs.Count > 0 && !string.IsNullOrWhiteSpace(responseId))
    {
        AsyncCollectionResult<StreamingResponseUpdate> newUpdates = responseClient.
            CreateResponseStreamingAsync(toolOutputs, GetResponseCreationOptions(userId, responseId), cancellationToken);
        responseId = await GetUpdates(responseClient, userId, newUpdates, cancellationToken, true);
    }
    return responseId;
}