-
Notifications
You must be signed in to change notification settings - Fork 1.1k
.NET: Fix: Checkpoint Deserialization breaks when JSON metadata properties are out of order #3442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
b523382 to
0eca39a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR makes JSON checkpoint deserialization resilient to out-of-order polymorphic metadata (e.g., $type) so that checkpoints stored in PostgreSQL jsonb (and other sources that reorder properties) can be deserialized successfully.
Changes:
- Extend
JsonMarshallerto respect a newJsonCheckpointManagerOptions.AllowOutOfOrderMetadataPropertiesflag via its internalJsonSerializerOptions. - Introduce
JsonCheckpointManagerOptionsand thread it throughCheckpointManager.CreateJsonso callers can opt into relaxed metadata ordering behavior. - Add targeted unit tests (and a helper) to reproduce the
jsonbmetadata-ordering issue and validate that enabling the new option restores successful deserialization.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/JsonSerializationTests.cs |
Adds regression tests simulating out-of-order $type metadata and verifying default failure vs. success when AllowOutOfOrderMetadataProperties is enabled, plus a helper to reorder JSON properties. |
dotnet/src/Microsoft.Agents.AI.Workflows/Checkpointing/JsonMarshaller.cs |
Updates the marshaller to accept JsonCheckpointManagerOptions and configure AllowOutOfOrderMetadataProperties on its internal JsonSerializerOptions. |
dotnet/src/Microsoft.Agents.AI.Workflows/Checkpointing/JsonCheckpointManagerOptions.cs |
Introduces a public options class with a flag to allow out-of-order metadata properties during JSON deserialization, with XML documentation. |
dotnet/src/Microsoft.Agents.AI.Workflows/CheckpointManager.cs |
Extends CreateJson with an optional JsonCheckpointManagerOptions parameter and passes it to JsonMarshaller so callers can opt in to relaxed metadata ordering. |
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/JsonSerializationTests.cs
Outdated
Show resolved
Hide resolved
0eca39a to
e9da6ec
Compare
e9da6ec to
b3065f8
Compare
| /// <param name="checkpointOptions">Optional checkpoint manager options to configure JSON serialization behavior.</param> | ||
| /// <returns>A CheckpointManager instance configured to serialize checkpoint data as JSON.</returns> | ||
| public static CheckpointManager CreateJson(ICheckpointStore<JsonElement> store, JsonSerializerOptions? customOptions = null) | ||
| public static CheckpointManager CreateJson(ICheckpointStore<JsonElement> store, JsonSerializerOptions? customOptions = null, JsonCheckpointManagerOptions? checkpointOptions = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider renaming customOptions to jsonSerializerOptions to disambiguate it better from checkpointOptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it feels a bit unintuitive to have an AllowOutOfOrderMetadataProperties on checkpointOptions, which is also on customOptions. The comment for checkpointOptions is that it is to configure JSON serialization behavior. I would expect that that is what the customOptions is for. Without reading the code I would be confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The customOptions is for configuring the serialization behaviour of user types. It does not impact the serialization of system types. Will think on how to make what is going on here clearer with names / docs.
Maybe look for that field in the user-specified options and rely on it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I like the idea of looking on the actual property on the type the user passes in. It seems unreasonable to only need to support it for one layer since we only ever give out the combined json clob to the underlying persistence layer, so if keys are randomized in some places, I'd expect them to be randomized in others.
b3065f8 to
4036b04
Compare
92c9a3e to
8cf4f7d
Compare
Motivation and Context
JSON Checkpoint serialization is currently strict in the ordering of the metadata properties, such as the polymorphic type specifier. This causes problems when deserializing checkpoints stored as JSONB in Postgres, which provides a different order on read.
Description
Create a way to let the user specify that a relaxed ordering is also allowed.
Contribution Checklist
Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.