Skip to content

Commit 13e7582

Browse files
icycarogerbarreto
andauthored
.Net: Fix #13232: Add empty parameters schema for NonInvocableTool placeholder (#13278)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> 1. Why is this change required? NonInvocableTool placeholder is created without a parameters field, which causes 422 errors when used with Mistral models via LiteLLM since Mistral enforces stricter JSON schema validation than other AI providers. 2. What problem does it solve? Current Issue: When Semantic Kernel has no invocable tools but needs to send tools to keep the service happy, it includes the NonInvocableTool placeholder. This lacks a parameters field, causing Mistral APIs to reject requests. 3. What scenario does it contribute to? This fix enables SK to work with: - Mistral cloud APIs - Any AI provider with strict schema validation requirements Use Cases: Applications using Mistral as their AI provider Enterprise applications with strict API validation requirements 4. Fixes: #13232 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Solution Updated the NonInvocableTool creation to include a proper empty parameters schema that satisfies Mistral's validation requirements while maintaining backward compatibility with all other providers Changes Made File: src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs Line 61-64: Enhanced s_nonInvocableFunctionTool with proper parameters schema Added: Comprehensive unit tests verifying schema structure and Mistral compatibility Existing Tests: All 474 OpenAI connector tests pass (1 skipped) Integration Tests: Verified with local Mistral via Ollama + LiteLLM Backward Compatibility: Confirmed no breaking changes with OpenAI and other providers Note: I 've noticed that this similar empty schema is used as a property called `s_zeroFunctionParametersSchema` that exists at `OpenAIFunction.cs` ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
1 parent 70b7bdb commit 13e7582

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Core/ClientCoreTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,78 @@ private void AddMessagesWithFunctionCallsWithValidFunctionName()
399399
}), false);
400400
}
401401
}
402+
403+
[Fact]
404+
public void NonInvocableToolHasValidParametersSchema()
405+
{
406+
// Arrange & Act
407+
// Access the NonInvocableTool through reflection since it's protected
408+
var clientCoreType = typeof(ClientCore);
409+
var nonInvocableToolField = clientCoreType.GetField("s_nonInvocableFunctionTool",
410+
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
411+
412+
Assert.NotNull(nonInvocableToolField);
413+
414+
var nonInvocableTool = (ChatTool)nonInvocableToolField.GetValue(null)!;
415+
416+
// Assert
417+
Assert.NotNull(nonInvocableTool);
418+
Assert.Equal("NonInvocableTool", nonInvocableTool.FunctionName);
419+
Assert.Equal("A placeholder tool used when no real tools are available", nonInvocableTool.FunctionDescription);
420+
421+
// Verify that parameters are not null (this is the key fix for Mistral compatibility)
422+
Assert.NotNull(nonInvocableTool.FunctionParameters);
423+
424+
// Verify the parameters contain a valid JSON schema
425+
var parametersJson = nonInvocableTool.FunctionParameters.ToString();
426+
Assert.Contains("\"type\":\"object\"", parametersJson);
427+
Assert.Contains("\"required\":[]", parametersJson);
428+
Assert.Contains("\"properties\":{}", parametersJson);
429+
430+
// Verify it's valid JSON
431+
var parsedJson = JsonSerializer.Deserialize<JsonElement>(parametersJson);
432+
Assert.Equal(JsonValueKind.Object, parsedJson.ValueKind);
433+
Assert.True(parsedJson.TryGetProperty("type", out var typeProperty));
434+
Assert.Equal("object", typeProperty.GetString());
435+
Assert.True(parsedJson.TryGetProperty("required", out var requiredProperty));
436+
Assert.Equal(JsonValueKind.Array, requiredProperty.ValueKind);
437+
Assert.Equal(0, requiredProperty.GetArrayLength());
438+
Assert.True(parsedJson.TryGetProperty("properties", out var propertiesProperty));
439+
Assert.Equal(JsonValueKind.Object, propertiesProperty.ValueKind);
440+
}
441+
442+
[Fact]
443+
public void NonInvocableToolSchemaIsCompatibleWithMistral()
444+
{
445+
// Arrange & Act
446+
var clientCoreType = typeof(ClientCore);
447+
var nonInvocableToolField = clientCoreType.GetField("s_nonInvocableFunctionTool",
448+
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
449+
450+
var nonInvocableTool = (ChatTool)nonInvocableToolField!.GetValue(null)!;
451+
452+
// Assert
453+
// This test verifies that the tool schema meets Mistral's requirements:
454+
// 1. Has a parameters field (not null)
455+
// 2. Parameters field contains valid JSON schema
456+
// 3. Schema has required type, properties, and required fields
457+
458+
Assert.NotNull(nonInvocableTool.FunctionParameters);
459+
460+
var parametersJson = nonInvocableTool.FunctionParameters.ToString();
461+
var schema = JsonSerializer.Deserialize<JsonElement>(parametersJson);
462+
463+
// Verify all required fields for Mistral compatibility
464+
Assert.True(schema.TryGetProperty("type", out _), "Schema must have 'type' field");
465+
Assert.True(schema.TryGetProperty("properties", out _), "Schema must have 'properties' field");
466+
Assert.True(schema.TryGetProperty("required", out _), "Schema must have 'required' field");
467+
468+
// Verify the schema structure matches what Mistral expects
469+
Assert.Equal("object", schema.GetProperty("type").GetString());
470+
Assert.Equal(JsonValueKind.Object, schema.GetProperty("properties").ValueKind);
471+
Assert.Equal(JsonValueKind.Array, schema.GetProperty("required").ValueKind);
472+
473+
// This ensures the tool won't cause 422 errors with Mistral APIs
474+
// as described in GitHub issue #13232
475+
}
402476
}

dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ protected record ToolCallingConfig(IList<ChatTool>? Tools, ChatToolChoice? Choic
5858
protected const int MaxInflightAutoInvokes = 128;
5959

6060
/// <summary>Singleton tool used when tool call count drops to 0 but we need to supply tools to keep the service happy.</summary>
61-
protected static readonly ChatTool s_nonInvocableFunctionTool = ChatTool.CreateFunctionTool("NonInvocableTool");
61+
protected static readonly ChatTool s_nonInvocableFunctionTool = ChatTool.CreateFunctionTool(
62+
functionName: "NonInvocableTool",
63+
functionDescription: "A placeholder tool used when no real tools are available",
64+
functionParameters: BinaryData.FromString("""{"type":"object","required":[],"properties":{}}"""));
6265

6366
/// <summary>
6467
/// Instance of <see cref="Meter"/> for metrics.

0 commit comments

Comments
 (0)