diff --git a/docs/ai/how-to/handle-invalid-tool-input.md b/docs/ai/how-to/handle-invalid-tool-input.md new file mode 100644 index 0000000000000..c2a26c0022425 --- /dev/null +++ b/docs/ai/how-to/handle-invalid-tool-input.md @@ -0,0 +1,73 @@ +--- +title: Handle invalid tool input from AI models +description: Learn strategies to handle invalid tool input when AI models provide incorrect or malformed function call parameters. +ms.date: 01/05/2026 +ai-usage: ai-assisted +--- + +# Handle invalid tool input from AI models + +When AI models call functions in your .NET code, they might sometimes provide invalid input that doesn't match the expected schema. The `Microsoft.Extensions.AI` library provides several strategies to handle these scenarios gracefully. + +## Common scenarios for invalid input + +AI models can provide invalid function call input in several ways: + +- Missing required parameters. +- Incorrect data types (for example, sending a string when an integer is expected). +- Malformed JSON that can't be deserialized. + +Without proper error handling, these issues can cause your application to fail or provide poor user experiences. + +## Enable detailed error messages + +By default, when a function invocation fails, the AI model receives a generic error message. You can enable detailed error reporting using the property. When this property is set to `true` and an error occurs during function invocation, the full exception message is added to the chat history. This allows the AI model to see what went wrong and potentially self-correct in subsequent attempts. + +:::code language="csharp" source="snippets/handle-invalid-tool-input/csharp/IncludeDetailedErrors.cs" id="BasicUsage"::: + +> [!NOTE] +> Setting `IncludeDetailedErrors` to `true` can expose internal system details to the AI model and potentially to end users. Ensure exception messages don't contain secrets, connection strings, or other sensitive information. To avoid leaking sensitive information, consider disabling detailed errors in production environments. + +## Implement custom error handling + +For more control over error handling, you can set a custom delegate. This allows you to intercept function calls, catch exceptions, and return custom error messages to the AI model. + +The following example shows how to implement a custom function invoker that catches serialization errors and provides helpful feedback: + +:::code language="csharp" source="snippets/handle-invalid-tool-input/csharp/FunctionInvoker.cs" id="BasicInvoker"::: + +By returning descriptive error messages instead of throwing exceptions, you allow the AI model to see what went wrong and try again with corrected input. + +### Best practices for error messages + +When returning error messages to enable AI self-correction, provide clear, actionable feedback: + +- **Be specific**: Explain exactly what was wrong with the input. +- **Provide examples**: Show the expected format or valid values. +- **Use consistent format**: Help the AI model learn from patterns. +- **Log errors**: Track error patterns for debugging and monitoring. + +## Use strict JSON schema (OpenAI only) + +When using OpenAI models, you can enable strict JSON schema mode to enforce that the model's output strictly adheres to your function's schema. This helps prevent type mismatches and missing required fields. + +Enable strict mode using the `Strict` additional property on your function metadata. When enabled, OpenAI models try to ensure their output matches your schema exactly: + +:::code language="csharp" source="snippets/handle-invalid-tool-input/csharp/StrictSchema.cs" id="StrictMode"::: + +For the latest list of models that support strict JSON schema, check the [OpenAI documentation](https://platform.openai.com/docs/guides/structured-outputs). + +### Limitations + +While strict mode significantly improves schema adherence, keep these limitations in mind: + +- Not all JSON Schema features are supported in strict mode. +- Complex schemas might still produce occasional errors. +- Always validate outputs even with strict mode enabled. +- Strict mode is OpenAI-specific and doesn't apply to other AI providers. + +## Next steps + +- [Access data in AI functions](access-data-in-functions.md) +- [Execute a local .NET function](../quickstarts/use-function-calling.md) +- [Build a chat app](../quickstarts/build-chat-app.md) diff --git a/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/FunctionInvoker.cs b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/FunctionInvoker.cs new file mode 100644 index 0000000000000..93d4ffbb050f1 --- /dev/null +++ b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/FunctionInvoker.cs @@ -0,0 +1,65 @@ +using Microsoft.Extensions.AI; +using Microsoft.Extensions.Configuration; +using OpenAI; +using System.Text.Json; + +class FunctionInvoker +{ + static async Task Main() + { + IConfigurationRoot config = new ConfigurationBuilder().AddUserSecrets().Build(); + string? model = config["ModelName"]; + string? key = config["OpenAIKey"]; + + // + IChatClient chatClient = new OpenAIClient(key).GetChatClient(model ?? "gpt-4o").AsIChatClient(); + + var functionInvokingClient = new FunctionInvokingChatClient(chatClient) + { + FunctionInvoker = async (context, cancellationToken) => + { + try + { + // Invoke the function normally. + return await context.Function.InvokeAsync(context.Arguments, cancellationToken); + } + catch (JsonException ex) + { + // Catch JSON serialization errors and provide helpful feedback. + return $"Error: Unable to parse the function arguments. {ex.Message}. " + + "Check the parameter types and try again."; + } + catch (Exception ex) + { + // Catch all other errors. + return $"Error: Function execution failed - {ex.Message}"; + } + } + }; + // + + IChatClient client = new ChatClientBuilder(functionInvokingClient).Build(); + + var chatOptions = new ChatOptions + { + 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 chatHistory = [ + new(ChatRole.System, "You are a weather data recorder."), + new(ChatRole.User, "Record the temperature as 25 degrees for London") + ]; + + ChatResponse response = await client.GetResponseAsync(chatHistory, chatOptions); + Console.WriteLine($"Assistant >>> {response.Text}"); + } +} diff --git a/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/HandleInvalidToolInput.csproj b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/HandleInvalidToolInput.csproj new file mode 100644 index 0000000000000..c56adaa4960c0 --- /dev/null +++ b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/HandleInvalidToolInput.csproj @@ -0,0 +1,17 @@ + + + + Library + net10.0 + enable + enable + + + + + + + + + + diff --git a/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/IncludeDetailedErrors.cs b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/IncludeDetailedErrors.cs new file mode 100644 index 0000000000000..2eff5cde6aa30 --- /dev/null +++ b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/IncludeDetailedErrors.cs @@ -0,0 +1,49 @@ +using Microsoft.Extensions.AI; +using Microsoft.Extensions.Configuration; +using OpenAI; + +class IncludeDetailedErrors +{ + static async Task Main() + { + IConfigurationRoot config = new ConfigurationBuilder().AddUserSecrets().Build(); + string? model = config["ModelName"]; + string? key = config["OpenAIKey"]; + + IChatClient client = new ChatClientBuilder( + new OpenAIClient(key).GetChatClient(model ?? "gpt-4o").AsIChatClient()) + .UseFunctionInvocation() + .Build(); + + // + // Enable detailed error messages to help the AI model self-correct. + FunctionInvokingChatClient? functionInvokingClient = client as FunctionInvokingChatClient; + functionInvokingClient?.IncludeDetailedErrors = true; + // + + var chatOptions = new ChatOptions + { + Tools = [AIFunctionFactory.Create((int temperature, string location) => + { + // Validate temperature is in a reasonable range. + if (temperature < -50 || temperature > 50) + { + throw new ArgumentOutOfRangeException( + nameof(temperature), + "Temperature must be between -50 and 50 degrees Celsius."); + } + return $"The temperature in {location} is {temperature}°C"; + }, + "get_temperature", + "Gets the current temperature for a location")] + }; + + List chatHistory = [ + new(ChatRole.System, "You are a helpful weather assistant."), + new(ChatRole.User, "What's the temperature in Paris?") + ]; + + ChatResponse response = await client.GetResponseAsync(chatHistory, chatOptions); + Console.WriteLine($"Assistant >>> {response.Text}"); + } +} diff --git a/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/StrictSchema.cs b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/StrictSchema.cs new file mode 100644 index 0000000000000..480aa966cacf0 --- /dev/null +++ b/docs/ai/how-to/snippets/handle-invalid-tool-input/csharp/StrictSchema.cs @@ -0,0 +1,52 @@ +using Microsoft.Extensions.AI; +using Microsoft.Extensions.Configuration; +using OpenAI; + +class StrictSchema +{ + static async Task Main() + { + IConfigurationRoot config = new ConfigurationBuilder().AddUserSecrets().Build(); + string? model = config["ModelName"]; + string? key = config["OpenAIKey"]; + + // Ensure your model configuration uses a compatible version (e.g., gpt-4o-2024-08-06 or later). + IChatClient client = new ChatClientBuilder( + new OpenAIClient(key).GetChatClient(model ?? "gpt-4o").AsIChatClient()) + .UseFunctionInvocation() + .Build(); + + // + AIFunction weatherFunction = AIFunctionFactory.Create( + (string location, int temperature, bool isRaining) => + { + string weather = isRaining ? "rainy" : "clear"; + return $"Weather in {location}: {temperature}°C and {weather}"; + }, + new AIFunctionFactoryOptions + { + Name = "report_weather", + Description = "Reports the weather for a location with temperature and rain status", + // Enable strict mode by adding the Strict property (OpenAI only). + AdditionalProperties = new Dictionary + { + { "Strict", true } + } + } + ); + // + + var chatOptions = new ChatOptions + { + Tools = [weatherFunction] + }; + + List chatHistory = [ + new(ChatRole.System, "You are a weather reporting assistant."), + new(ChatRole.User, "What's the weather in Tokyo? It's 22 degrees and not raining.") + ]; + + ChatResponse response = await client.GetResponseAsync(chatHistory, chatOptions); + Console.WriteLine($"Assistant >>> {response.Text}"); + } +} diff --git a/docs/ai/quickstarts/use-function-calling.md b/docs/ai/quickstarts/use-function-calling.md index b6b700c2241b9..e1c7b34ffc408 100644 --- a/docs/ai/quickstarts/use-function-calling.md +++ b/docs/ai/quickstarts/use-function-calling.md @@ -140,6 +140,7 @@ If you no longer need them, delete the Azure OpenAI resource and GPT-4 model dep ## Next steps +- [Handle invalid tool input from AI models](../how-to/handle-invalid-tool-input.md) - [Access data in AI functions](../how-to/access-data-in-functions.md) - [Quickstart - Build an AI chat app with .NET](build-chat-app.md) - [Generate text and conversations with .NET and Azure OpenAI Completions](/training/modules/open-ai-dotnet-text-completions/) diff --git a/docs/ai/toc.yml b/docs/ai/toc.yml index c140ef40ed2d5..cbe976e272735 100644 --- a/docs/ai/toc.yml +++ b/docs/ai/toc.yml @@ -68,6 +68,8 @@ items: href: quickstarts/use-function-calling.md - name: Access data in AI functions href: how-to/access-data-in-functions.md + - name: Handle invalid tool input + href: how-to/handle-invalid-tool-input.md - name: Text to image items: - name: Generate images using MEAI