Skip to content

Commit 501cc6f

Browse files
atty57westey-m
andauthored
.NET: Fix AG-UI SSE events written with explicit nulls (microsoft#7920)
* .NET: Fix AG-UI SSE events written with explicit nulls (microsoft#7919) ConfigureAGUIJsonOptions appended its resolvers to the ASP.NET Core TypeInfoResolverChain, which already has a reflection-based resolver at its head. Both appended resolvers were therefore unreachable for every type reflection can handle, so the AG-UI wire-format rules never applied. This was invisible with AGUI 0.0.5, whose omit-empty rule rides on attributes the reflection resolver honors. AGUI 0.0.6 moved that rule to a type-info modifier on AGUIJsonUtilities.DefaultTypeInfoResolver, which only applies when that resolver is actually consulted. Bumping the pin without fixing the ordering makes every SSE event go out with explicit nulls for its optional fields ("parentRunId": null and similar), which @ag-ui/client rejects with a ZodError. Insert both resolvers ahead of the reflection resolver and compose AGUIJsonUtilities.DefaultTypeInfoResolver rather than the context directly, as the AGUI docs direct. The AGUI pin moves to 0.0.6 in the same change since DefaultTypeInfoResolver is new in that version. Adds a regression test asserting the serialized event carries no explicit nulls; it fails against the previous ordering. * .NET: Use xUnit assertions in AG-UI JSON regression test --------- Co-authored-by: westey <[email protected]>
1 parent 9127b89 commit 501cc6f

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore/ConfigureAGUIJsonOptions.cs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ public void Configure(JsonOptions options)
1616
{
1717
var chain = options.SerializerOptions.TypeInfoResolverChain;
1818

19-
// Agent Framework abstractions first to ensure M.E.AI types are handled via its resolver,
20-
// followed by the AG-UI wire-format resolver for protocol types (the AG-UI context is needed
21-
// on the net10 TypedResults.ServerSentEvents path, which serializes events through the
22-
// configured ASP.NET Core JsonSerializerOptions).
23-
chain.Add(AgentAbstractionsJsonUtilities.DefaultOptions.TypeInfoResolver!);
24-
chain.Add(AGUIJsonSerializerContext.Default.Options.TypeInfoResolver!);
19+
// Both resolvers must go in front of the reflection-based resolver ASP.NET Core already
20+
// placed at the head of the chain; appending leaves them unreachable for every type that
21+
// reflection can handle, which silently discards the AG-UI wire-format rules.
22+
//
23+
// AGUIJsonUtilities.DefaultTypeInfoResolver is the AG-UI context plus the modifier that omits
24+
// properties with no value. Without it the SSE events go out with explicit nulls for their
25+
// optional fields ("parentRunId": null and similar), which receiving SDKs reject. The AG-UI
26+
// resolver is needed on the net10 TypedResults.ServerSentEvents path, which serializes events
27+
// through the configured ASP.NET Core JsonSerializerOptions.
28+
//
29+
// Agent Framework abstractions follow so that M.E.AI types are handled via its resolver.
30+
chain.Insert(0, AGUIJsonUtilities.DefaultTypeInfoResolver);
31+
chain.Insert(1, AgentAbstractionsJsonUtilities.DefaultOptions.TypeInfoResolver!);
2532
}
2633
}

‎dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/ConfigureAGUIJsonOptionsTests.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public void AddAGUIServer_ConfiguresJsonOptions_ResolvesAgentAbstractionsTypes()
3232
Assert.Null(Record.Exception(() => options.GetTypeInfo(typeof(ChatMessage))));
3333
}
3434

35+
[Fact]
36+
public void AddAGUIServer_ConfiguresJsonOptions_OmitsOptionalFieldsWithNoValue()
37+
{
38+
JsonSerializerOptions options = BuildConfiguredSerializerOptions();
39+
40+
string json = JsonSerializer.Serialize<BaseEvent>(new RunStartedEvent { ThreadId = "thread", RunId = "run" }, options);
41+
42+
// AG-UI receivers declare the optional event fields as optional, not nullable, so writing them
43+
// as explicit nulls fails validation client-side. The configured options must omit them.
44+
Assert.DoesNotContain("null", json);
45+
Assert.Equal("""{"type":"RUN_STARTED","threadId":"thread","runId":"run"}""", json);
46+
}
47+
3548
private static JsonSerializerOptions BuildConfiguredSerializerOptions()
3649
{
3750
ServiceCollection services = new();

0 commit comments

Comments
 (0)