Skip to content

Commit b10f1bb

Browse files
CopilotCopilot
andcommitted
Fix ParseToolArguments to handle non-object JSON gracefully (microsoft#5897)
- Guard against non-object root elements (arrays, null, strings) by checking ValueKind before calling EnumerateObject() - Use double.TryParse instead of GetDouble() to avoid FormatException on out-of-range numeric values - Widen catch filter to include InvalidOperationException and FormatException as additional safety net - Add regression tests for array, null-literal, string, and malformed JSON argument inputs Co-authored-by: Copilot <[email protected]>
1 parent c18c3d9 commit b10f1bb

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,18 @@ private AgentResponseUpdate ConvertToAgentResponseUpdate(AssistantUsageEvent usa
468468
try
469469
{
470470
using var doc = JsonDocument.Parse(argumentsJson);
471+
if (doc.RootElement.ValueKind != JsonValueKind.Object)
472+
{
473+
return new Dictionary<string, object?> { ["_raw"] = argumentsJson };
474+
}
475+
471476
var result = new Dictionary<string, object?>();
472477
foreach (var property in doc.RootElement.EnumerateObject())
473478
{
474479
result[property.Name] = property.Value.ValueKind switch
475480
{
476481
JsonValueKind.String => property.Value.GetString(),
477-
JsonValueKind.Number => property.Value.GetDouble(),
482+
JsonValueKind.Number => property.Value.GetRawText() is string raw && double.TryParse(raw, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out double d) ? d : (object)property.Value.GetRawText(),
478483
JsonValueKind.True => true,
479484
JsonValueKind.False => false,
480485
JsonValueKind.Null => null,
@@ -484,7 +489,7 @@ private AgentResponseUpdate ConvertToAgentResponseUpdate(AssistantUsageEvent usa
484489

485490
return result;
486491
}
487-
catch (JsonException)
492+
catch (Exception ex) when (ex is JsonException or InvalidOperationException or FormatException)
488493
{
489494
return new Dictionary<string, object?> { ["_raw"] = argumentsJson };
490495
}

‎dotnet/tests/Microsoft.Agents.AI.GitHub.Copilot.UnitTests/ToolExecutionEventProjectionTests.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,61 @@ public void ToolExecutionCompleteEvent_NullData_ProducesFunctionResultContentWit
188188
Assert.Null(content.Result);
189189
}
190190

191+
[Theory]
192+
[InlineData("null")]
193+
[InlineData("[1, 2, 3]")]
194+
[InlineData("\"just a string\"")]
195+
public void ToolExecutionStartEvent_NonObjectArguments_FallsBackToRawDictionary(string arguments)
196+
{
197+
// Arrange
198+
var toolStartEvent = new ToolExecutionStartEvent
199+
{
200+
Data = new ToolExecutionStartData
201+
{
202+
ToolCallId = "call_nonobj",
203+
ToolName = "some_tool",
204+
Arguments = arguments
205+
}
206+
};
207+
208+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
209+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: "test-agent", tools: null);
210+
211+
// Act
212+
AgentResponseUpdate result = InvokeConvert(agent, toolStartEvent);
213+
214+
// Assert
215+
var content = Assert.IsType<FunctionCallContent>(result.Contents[0]);
216+
Assert.NotNull(content.Arguments);
217+
Assert.Equal(arguments, content.Arguments["_raw"]);
218+
}
219+
220+
[Fact]
221+
public void ToolExecutionStartEvent_MalformedJson_FallsBackToRawDictionary()
222+
{
223+
// Arrange
224+
var toolStartEvent = new ToolExecutionStartEvent
225+
{
226+
Data = new ToolExecutionStartData
227+
{
228+
ToolCallId = "call_malformed",
229+
ToolName = "some_tool",
230+
Arguments = "{not valid json"
231+
}
232+
};
233+
234+
CopilotClient copilotClient = new(new CopilotClientOptions { AutoStart = false });
235+
var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, id: "test-agent", tools: null);
236+
237+
// Act
238+
AgentResponseUpdate result = InvokeConvert(agent, toolStartEvent);
239+
240+
// Assert
241+
var content = Assert.IsType<FunctionCallContent>(result.Contents[0]);
242+
Assert.NotNull(content.Arguments);
243+
Assert.Equal("{not valid json", content.Arguments["_raw"]);
244+
}
245+
191246
/// <summary>
192247
/// Invokes the appropriate ConvertToAgentResponseUpdate method via reflection.
193248
/// </summary>

0 commit comments

Comments
 (0)