Open
Conversation
Contributor
There was a problem hiding this comment.
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
JsonElementandJsonElement?toSessionEventsJsonContext. - Regenerated
dotnet/src/Generated/SessionEvents.cswith 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.
stephentoub
reviewed
Mar 16, 2026
stephentoub
approved these changes
Mar 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This fixes a .NET SDK serialization gap where
SessionEvent.ToJson()could fail for valid session events containing payloads that are stored throughobject-typed members and materialize asJsonElementafter deserialization.Affected event shapes include:
assistant.messagewithtoolRequests[].argumentstool.execution_startwithargumentstool.execution_completewith nestedtoolTelemetrysession.shutdownwith nestedmodelMetricsProblem
SessionEvent.FromJson(...)could successfully deserialize these events, butSessionEvent.ToJson()could then throwSystem.NotSupportedExceptionbecause the source-generatedSessionEventsJsonContextdid not include metadata forJsonElement.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? ArgumentsDictionary<string, object>? ToolTelemetryDictionary<string, object> ModelMetricsWhen those are populated from JSON, their runtime values are often
JsonElement. The generatedSessionEventsJsonContextdid not includeJsonElement/JsonElement?, so re-serialization failed.Fix
Update the C# code generator to include:
JsonElementJsonElement?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:
and then regenerated into:
Tests
Added a regression test that builds typed C# session event objects and verifies
ToJson()round-trips event payloads that containJsonElement-backed dynamic members.Covered cases:
assistant.messagetool.execution_starttool.execution_completesession.shutdownThe test intentionally constructs real SDK event/data types so it will fail if the generated C# model shape changes.
Validation
Verified locally with:
The targeted regression tests pass.