Skip to content

docs: use realtime function-call arguments in sample#1124

Open
Rohan5commit wants to merge 1 commit into
openai:mainfrom
Rohan5commit:docs/realtime-sample-function-arguments-20260425
Open

docs: use realtime function-call arguments in sample#1124
Rohan5commit wants to merge 1 commit into
openai:mainfrom
Rohan5commit:docs/realtime-sample-function-arguments-20260425

Conversation

@Rohan5commit
Copy link
Copy Markdown
Contributor

Summary

  • Updates the realtime tool-calling sample to use the function arguments returned by the model instead of a hard-coded location.
  • Keeps the sample aligned with how a real tool invocation would be wired up.

Related issue

Guideline alignment

  • Single-file example improvement
  • No generated files touched

Validation

  • git diff --check
  • Local dotnet build could not be run in this environment because the dotnet CLI is unavailable.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.FunctionArguments as JSON to extract location and optional unit.
  • 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()
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.
Comment on lines +213 to 223
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);

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.
Copy link
Copy Markdown
Collaborator

@jsquire jsquire left a comment

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQ] Please improve the Realtime sample

4 participants