docs: use realtime function-call arguments in sample#1124
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the Realtime tool-calling example to consume the model-provided function arguments, making the sample closer to a real-world tool invocation flow (fixing #535).
Changes:
- Parse
RealtimeFunctionCallItem.FunctionArgumentsas JSON to extractlocationand optionalunit. - Use parsed arguments when calling
GetCurrentWeather(...)instead of a hard-coded location.
|
|
||
| string output = GetCurrentWeather(location: "San Francisco, CA"); | ||
| using JsonDocument argumentsJson = JsonDocument.Parse(functionCallItem.FunctionArguments); | ||
| string location = argumentsJson.RootElement.GetProperty("location").GetString() |
There was a problem hiding this comment.
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.
| 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() |
| 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() | ||
| ?? 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); | ||
|
|
There was a problem hiding this comment.
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.
jsquire
left a comment
There was a problem hiding this comment.
Thank you for your contribution, @Rohan5commit, and your interest in improving the OpenAI developer experience. I think there's merit in the approach that you're demonstrating here. I find myself agreeing with the copilot comments and would ask that we address those before moving forward.
I'd also like to pull in @joseharriaga, @ShivangiReja, and @christothes for additional perspective here.
Summary
Related issue
Guideline alignment
Validation
git diff --checkdotnet buildcould not be run in this environment because thedotnetCLI is unavailable.