Skip to content

Fix(dotnet): serialization of SessionEvent#868

Open
xoofx wants to merge 1 commit intogithub:mainfrom
xoofx:fix-to-json
Open

Fix(dotnet): serialization of SessionEvent#868
xoofx wants to merge 1 commit intogithub:mainfrom
xoofx:fix-to-json

Conversation

@xoofx
Copy link

@xoofx xoofx commented Mar 16, 2026

Summary

This fixes a .NET SDK serialization gap where SessionEvent.ToJson() could fail for valid session events containing payloads that are stored through object-typed members and materialize as JsonElement after deserialization.

Affected event shapes include:

  • assistant.message with toolRequests[].arguments
  • tool.execution_start with arguments
  • tool.execution_complete with nested toolTelemetry
  • session.shutdown with nested modelMetrics

Problem

SessionEvent.FromJson(...) could successfully deserialize these events, but SessionEvent.ToJson() could then throw System.NotSupportedException because the source-generated SessionEventsJsonContext did not include metadata for JsonElement.

This is a real issue for SDK consumers that subscribe to events and re-serialize them for logging, capture, persistence, or forwarding.

A concrete symptom is that richer runtime events can appear to be “missing” from application-side captures even though the Copilot session emitted and persisted them correctly.

Root cause

Some generated session event models contain members such as:

  • object? Arguments
  • Dictionary<string, object>? ToolTelemetry
  • Dictionary<string, object> ModelMetrics

When those are populated from JSON, their runtime values are often JsonElement. The generated SessionEventsJsonContext did not include JsonElement / JsonElement?, so re-serialization failed.

Fix

Update the C# code generator to include:

  • JsonElement
  • JsonElement?

in the generated SessionEventsJsonContext.

This keeps the fix source-generated and AOT/trimming-friendly, instead of relying on reflection-based serialization.

Generator change

The fix was made in the generator, not only in generated output:

  • csharp.ts

and then regenerated into:

  • SessionEvents.cs

Tests

Added a regression test that builds typed C# session event objects and verifies ToJson() round-trips event payloads that contain JsonElement-backed dynamic members.

Covered cases:

  • assistant.message
  • tool.execution_start
  • tool.execution_complete
  • session.shutdown

The test intentionally constructs real SDK event/data types so it will fail if the generated C# model shape changes.

Validation

Verified locally with:

dotnet build C:\code\github\copilot-sdk\dotnet\src\GitHub.Copilot.SDK.csproj -c Release
dotnet test C:\code\github\copilot-sdk\dotnet\test\GitHub.Copilot.SDK.Test.csproj -c Release --filter SessionEventSerializationTests

The targeted regression tests pass.

@xoofx xoofx requested a review from a team as a code owner March 16, 2026 07:02
Copilot AI review requested due to automatic review settings March 16, 2026 07:02
@xoofx xoofx changed the title Fix serialization of SessionEvent Fix(dotnet): serialization of SessionEvent Mar 16, 2026
Copy link
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

Fixes a .NET SDK gap where SessionEvent.ToJson() could throw when event payloads include object/Dictionary<string, object> members that materialize as JsonElement, by ensuring the source-generated serializer context includes JsonElement metadata.

Changes:

  • Updated the C# session-events code generator to add JsonElement and JsonElement? to SessionEventsJsonContext.
  • Regenerated dotnet/src/Generated/SessionEvents.cs with the updated source-gen context annotations.
  • Added a regression test covering round-trip serialization for multiple JsonElement-backed event shapes.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
scripts/codegen/csharp.ts Extends generated SessionEventsJsonContext to include JsonElement/JsonElement? metadata for AOT-safe serialization.
dotnet/src/Generated/SessionEvents.cs Regenerated output reflecting the updated source-gen context.
dotnet/test/SessionEventSerializationTests.cs Adds regression coverage validating ToJson() round-trips dynamic JsonElement payloads.

You can also share your feedback on Copilot code review. Take the survey.

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.

3 participants