Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions examples/Realtime/Example01_AudioFromFileWithToolsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace OpenAI.Examples;
Expand Down Expand Up @@ -209,9 +210,16 @@ public async Task Example01_AudioFromFileWithToolsAsync()
{
hasToolCalls = true;

Console.WriteLine($">> Calling {functionCallItem.FunctionName} function...");
Console.WriteLine($">> Calling {functionCallItem.FunctionName} function with arguments...");

string output = GetCurrentWeather(location: "San Francisco, CA");
using JsonDocument argumentsJson = JsonDocument.Parse(functionCallItem.FunctionArguments);
string location = argumentsJson.RootElement.GetProperty("location").GetString()
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetProperty("location") will throw (with a generic KeyNotFoundException) if the model omits the location argument, so the subsequent null-coalescing InvalidOperationException message won’t be reached. Use TryGetProperty("location", out ...) (and validate ValueKind == String) so missing/invalid arguments produce the intended, actionable error message.

Suggested change
string location = argumentsJson.RootElement.GetProperty("location").GetString()
if (!argumentsJson.RootElement.TryGetProperty("location", out JsonElement locationElement)
|| locationElement.ValueKind != JsonValueKind.String)
{
throw new InvalidOperationException("Tool call is missing a location.");
}
string location = locationElement.GetString()

Copilot uses AI. Check for mistakes.
?? throw new InvalidOperationException("Tool call is missing a location.");
string unit = argumentsJson.RootElement.TryGetProperty("unit", out JsonElement unitElement)
? unitElement.GetString() ?? "celsius"
: "celsius";

string output = GetCurrentWeather(location: location, unit: unit);

Comment on lines +213 to 223
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log message uses functionCallItem.FunctionName, but the handler always invokes GetCurrentWeather(...) regardless of the reported function name. To keep the sample accurate (and avoid misleading output if additional tools are added later), either validate FunctionName == nameof(GetCurrentWeather) or dispatch based on FunctionName before parsing/handling arguments.

Copilot uses AI. Check for mistakes.
RealtimeItem functionCallOutputItem = RealtimeItem.CreateFunctionCallOutputItem(
callId: functionCallItem.CallId,
Expand Down
Loading