Skip to content

.NET SDK: GetMessagesAsync fails entirely when CLI returns unknown event types #880

@Ron537

Description

@Ron537

Problem

When the Copilot CLI introduces new session event types before the SDK is updated, SessionEvent.FromJson() throws a JsonException because the generated SessionEvents.cs uses JsonUnknownDerivedTypeHandling.FailSerialization.

This breaks two call-sites:

  1. GetMessagesAsync — the entire conversation history fails to load because
    .Select(e => SessionEvent.FromJson(...)) throws on the first unrecognized event.
  2. OnSessionEvent (real-time events) — the RPC handler crashes with no try-catch,
    propagating the exception into the StreamJsonRpc layer.

Expected behavior

Unknown event types should be handled gracefully. The Python SDK already does this via a SessionEventType.UNKNOWN enum value with a _missing_() fallback — no events are lost and no exceptions are thrown.

Proposed solution

  1. Add an UnknownSessionEvent concrete type that preserves the raw JSON and type discriminator for events the SDK can't deserialize.
  2. Add a SessionEvent.TryFromJson method (via partial class, no generated code changes) that returns UnknownSessionEvent instead of throwing.
  3. Update GetMessagesAsync and OnSessionEvent to use TryFromJson.
  4. Log a warning when an unknown event is encountered.

Reproduction

Occurs whenever the CLI version is ahead of the SDK version and emits a new event type. Can be reproduced by manually injecting a JSON event with an unregistered "type"
value and calling GetMessagesAsync.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions