Skip to content
Closed
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
13 changes: 11 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,17 @@ public async Task Example01_AudioFromFileWithToolsAsync()
{
hasToolCalls = true;

Console.WriteLine($">> Calling {functionCallItem.FunctionName} function...");
using JsonDocument argumentsJson = JsonDocument.Parse(functionCallItem.FunctionArguments.ToString());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle empty arguments before JSON parsing

functionCallItem.FunctionArguments is a string payload and can be empty in realtime function-call events; parsing it unconditionally with JsonDocument.Parse(...) will throw JsonException and stop the sample before it can submit tool output. This is a regression from the previous behavior (which continued with defaults) and can surface whenever the model returns an empty/malformed arguments string, so the example should defensively fall back (for example, treat empty input as {}) before parsing.

Useful? React with 👍 / 👎.

string location = argumentsJson.RootElement.TryGetProperty("location", out JsonElement locationElement)
? locationElement.GetString() ?? "San Francisco, CA"
: "San Francisco, CA";
string unit = argumentsJson.RootElement.TryGetProperty("unit", out JsonElement unitElement)
? unitElement.GetString() ?? "celsius"
: "celsius";
Comment on lines +213 to +219

string output = GetCurrentWeather(location: "San Francisco, CA");
Console.WriteLine($">> Calling {functionCallItem.FunctionName} function for {location}...");

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

RealtimeItem functionCallOutputItem = RealtimeItem.CreateFunctionCallOutputItem(
callId: functionCallItem.CallId,
Expand Down
Loading