-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document strategies for handling invalid AI function call input #50896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds comprehensive documentation on handling invalid tool input from AI models when using Microsoft.Extensions.AI. It addresses common scenarios where AI models provide malformed function call arguments and presents three mitigation strategies.
- Documents three strategies:
IncludeDetailedErrorsproperty, customFunctionInvokerdelegates, and OpenAI's strict schema mode - Includes security guidance for production environments about exposing error details
- Provides four compilable code examples demonstrating each strategy
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| docs/ai/how-to/handle-invalid-tool-input.md | New documentation article covering error handling strategies with security considerations |
| docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/IncludeDetailedErrors.cs | Code example demonstrating the IncludeDetailedErrors property for AI self-correction |
| docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/FunctionInvoker.cs | Code example showing custom error handling with FunctionInvoker delegate |
| docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/StrictSchema.cs | Code example demonstrating OpenAI strict JSON schema mode |
| docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/HandleInvalidToolInput.csproj | Project file with .NET 10 target and required package references |
| docs/ai/toc.yml | Adds navigation entry under "Call functions" section |
| docs/ai/quickstarts/use-function-calling.md | Links to new article from "Next steps" section |
|
|
||
| List<ChatMessage> chatHistory = [ | ||
| new(ChatRole.System, "You are a helpful weather assistant."), | ||
| new(ChatRole.User, "What's the temperature in Paris?") |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation is inconsistent. The closing brace should be aligned with the opening of the ChatMessage list. All list items should maintain consistent indentation.
| new(ChatRole.User, "What's the temperature in Paris?") | |
| new(ChatRole.User, "What's the temperature in Paris?") |
| Tools = [AIFunctionFactory.Create((string location, int temperature) => | ||
| { | ||
| if (string.IsNullOrWhiteSpace(location)) | ||
| { | ||
| throw new ArgumentException("Location cannot be empty", nameof(location)); | ||
| } | ||
| return $"Recorded temperature of {temperature}°C for {location}"; | ||
| }, | ||
| "record_temperature", | ||
| "Records the temperature for a location")] | ||
| }; | ||
|
|
||
| List<ChatMessage> chatHistory = [ | ||
| new(ChatRole.System, "You are a weather data recorder."), | ||
| new(ChatRole.User, "Record the temperature as 25 degrees for London") |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation is inconsistent. Lines 46-54 should be properly indented to match the AIFunctionFactory.Create call structure, and line 59 should align with line 58.
| Tools = [AIFunctionFactory.Create((string location, int temperature) => | |
| { | |
| if (string.IsNullOrWhiteSpace(location)) | |
| { | |
| throw new ArgumentException("Location cannot be empty", nameof(location)); | |
| } | |
| return $"Recorded temperature of {temperature}°C for {location}"; | |
| }, | |
| "record_temperature", | |
| "Records the temperature for a location")] | |
| }; | |
| List<ChatMessage> chatHistory = [ | |
| new(ChatRole.System, "You are a weather data recorder."), | |
| new(ChatRole.User, "Record the temperature as 25 degrees for London") | |
| Tools = [ | |
| AIFunctionFactory.Create( | |
| (string location, int temperature) => | |
| { | |
| if (string.IsNullOrWhiteSpace(location)) | |
| { | |
| throw new ArgumentException("Location cannot be empty", nameof(location)); | |
| } | |
| return $"Recorded temperature of {temperature}°C for {location}"; | |
| }, | |
| "record_temperature", | |
| "Records the temperature for a location") | |
| ] | |
| }; | |
| List<ChatMessage> chatHistory = [ | |
| new(ChatRole.System, "You are a weather data recorder."), | |
| new(ChatRole.User, "Record the temperature as 25 degrees for London") |
AI models can provide malformed function call arguments (wrong types, missing fields, invalid JSON). This documents three mitigation strategies using Microsoft.Extensions.AI.
Changes
New article:
docs/ai/how-to/handle-invalid-tool-input.mdIncludeDetailedErrorsproperty - sends full exception details to enable model self-correctionFunctionInvokerdelegate - catch exceptions, return descriptive errors to guide retry attemptsStrictadditional propertyCode examples (4 compilable snippets demonstrating each strategy)
Navigation: Added to TOC under "Call functions", linked from function calling quickstart
Example: Custom Error Handling
Addresses dotnet/extensions#7110 discussion points.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Internal previews