Skip to main content

Fixed: JsonTypeInfo metadata for type was not provided by TypeInfoResolver of type 'Microsoft.Extensions.AI.AIJsonUtilities+JsonContext'

· 11 min read
Jagdish Kumawat
Founder @ Dewiride

In a .NET 10 file-based app, turning a method into a Microsoft Agent Framework tool with AIFunctionFactory.Create crashed with System.NotSupportedException: JsonTypeInfo metadata for type ... was not provided by TypeInfoResolver of type 'Microsoft.Extensions.AI.AIJsonUtilities+JsonContext'. File-based apps target Native AOT by default, and that turns off reflection-based JSON. One #:property PublishAot=false line fixed it; a source-generated JSON context fixes it while keeping Native AOT.

What I Was Doing​

While writing Part 10 of my Microsoft Agent Framework series, I wanted to see the JSON schema each of the agent's tools sends to the model. The tools live in a class library, so I wrote a quick file-based app: a single .cs file that dotnet run builds and runs without a project file, new in .NET 10. It referenced the library with a #:project directive and created the tools with AIFunctionFactory.Create. It crashed before printing anything:

Output (the first line)
Unhandled exception. System.NotSupportedException: JsonTypeInfo metadata for type 'System.Collections.Generic.IReadOnlyList`1[Dewiride.ServiceDesk.Agent.Systems.Device]' was not provided by TypeInfoResolver of type 'Microsoft.Extensions.AI.AIJsonUtilities+JsonContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.

Here is the same problem in a file small enough to read at a glance. It turns one method into a tool, then calls the tool the way an agent would and prints the result. Save it as orders.cs in an empty folder:

orders.cs
#:package Microsoft.Agents.AI@1.28.0
using System.ComponentModel;
using Microsoft.Extensions.AI;

// Turn GetOpenOrders into a tool, then call it the way an agent would.
AIFunction getOpenOrders = AIFunctionFactory.Create(GetOpenOrders, name: "get_open_orders");
Console.WriteLine(await getOpenOrders.InvokeAsync(new AIFunctionArguments()));

[Description("Gets the customer's open orders.")]
static IReadOnlyList<Order> GetOpenOrders() =>
[
new Order("A-1001", "Keyboard"),
new Order("A-1002", "Mouse"),
];

record Order(string Number, string Item);

The first line, #:package Microsoft.Agents.AI@1.28.0, is how a file-based app adds a NuGet package: Microsoft Agent Framework 1.28.0, which brings Microsoft.Extensions.AI and its AIFunctionFactory along. The tool returns a list of Order records, a type of my own. Then run it from that folder:

Terminal
dotnet run orders.cs

What I Expected​

The two orders as JSON. The same AIFunctionFactory.Create call works in an ordinary project: the agent API in Part 10 creates exactly these tools at startup without a complaint.

What I Got​

Output
Unhandled exception. System.NotSupportedException: JsonTypeInfo metadata for type 'System.Collections.Generic.IReadOnlyList`1[Order]' was not provided by TypeInfoResolver of type 'Microsoft.Extensions.AI.AIJsonUtilities+JsonContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunctionDescriptor.GetReturnParameterMarshaller(DescriptorKey key, JsonSerializerOptions serializerOptions, Type& returnType)
at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunctionDescriptor..ctor(DescriptorKey key, JsonSerializerOptions serializerOptions)
at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunctionDescriptor.GetOrCreate(MethodInfo method, AIFunctionFactoryOptions options)
at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunction.Build(MethodInfo method, Object target, AIFunctionFactoryOptions options)
at Microsoft.Extensions.AI.AIFunctionFactory.Create(Delegate method, String name, String description, JsonSerializerOptions serializerOptions)
at Program.<Main>$(String[] args) in /Users/you/source/OrdersTool/orders.cs:line 6
at Program.<Main>(String[] args)

macOS-style terminal window showing dotnet run orders.cs ending in an unhandled System.NotSupportedException: JsonTypeInfo metadata for type IReadOnlyList of Order was not provided by TypeInfoResolver of type Microsoft.Extensions.AI.AIJsonUtilities+JsonContext, with a stack trace that runs through AIFunctionFactory.Create at line 6 of orders.cs

The app stopped at line 6, inside AIFunctionFactory.Create: before the tool ran, and with no AI model involved at all.

What I Checked​

The stack trace. Read from the bottom up: AIFunctionFactory.Create builds a descriptor of the method, and GetReturnParameterMarshaller prepares how to turn the method's return value into JSON. That is where it failed: it found no JSON metadata for the return type, IReadOnlyList<Order>.

Which JSON options AIFunctionFactory.Create uses. Its documentation, the text your editor shows for the method, says: Return values are serialized to JsonElement using serializerOptions if provided, or else using DefaultOptions. I passed no options, so it used AIJsonUtilities.DefaultOptions, the AIJsonUtilities+JsonContext named in the error.

What those default options know. Microsoft's reference for AIJsonUtilities.DefaultOptions: For Native AOT or applications disabling IsReflectionEnabledByDefault this instance includes source generated contracts for all common exchange types contained in the Microsoft.Extensions.AI.Abstractions library. Its own types, then, and not my Order.

Whether this app was Native AOT. Microsoft's File-based apps page says: File-based apps enable native ahead-of-time (AOT) compilation by default. The Native AOT deployment page lists among its limitations that Native AOT requires trimming, and a .NET 8 change note says that turning trimming on automatically sets the JsonSerializerIsReflectionEnabledByDefault MSBuild property to false. The same Native AOT page adds that the PublishAot setting controls behaviors outside publish, so it isn't only about publishing.

Whether reflection-based JSON was really off under dotnet run. The documents above say it for publishing; I checked what dotnet run does with two tiny file-based apps that print JsonSerializer.IsReflectionEnabledByDefault. They are the same, except that the second has #:property PublishAot=false at the top:

Output (the plain file-based app)
IsReflectionEnabledByDefault: False
Output (with #:property PublishAot=false)
IsReflectionEnabledByDefault: True

Why It Happens​

A .NET 10 file-based app targets Native AOT unless it says otherwise, and with that, System.Text.Json's reflection-based serialization is off, even under dotnet run. Without reflection, a JsonSerializerOptions instance can serialize only the types it has source-generated contracts for. AIFunctionFactory.Create prepares the JSON for the tool's return value with AIJsonUtilities.DefaultOptions, whose contracts cover Microsoft.Extensions.AI's own types. A tool that returns a type of yours, such as IReadOnlyList<Order>, has none, so Create throws NotSupportedException before anything runs. A regular project, such as a console app or an ASP.NET Core API, doesn't target Native AOT unless you ask for it, so reflection fills the gap there and the same code works.

The Fix​

Choose by whether you want the app to be Native AOT.

If you don't need Native AOT, which is the case for a quick script, turn it off. Microsoft's File-based apps page shows exactly this line: If you need to disable native AOT, use the following setting. Add it below the #:package line:

orders.cs (the first two lines)
#:package Microsoft.Agents.AI@1.28.0
#:property PublishAot=false

If you want to keep Native AOT, give AIFunctionFactory.Create JSON options that know your type. Create a source-generated JSON context for the tool's return type, and put it in front of Microsoft.Extensions.AI's own contracts:

orders.cs
#:package Microsoft.Agents.AI@1.28.0
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.AI;

// Microsoft.Extensions.AI's own contracts, plus this app's contracts in front of them.
JsonSerializerOptions jsonOptions = new(AIJsonUtilities.DefaultOptions);
jsonOptions.TypeInfoResolverChain.Insert(0, OrdersJsonContext.Default);

// Turn GetOpenOrders into a tool, then call it the way an agent would.
AIFunction getOpenOrders = AIFunctionFactory.Create(
GetOpenOrders, name: "get_open_orders", serializerOptions: jsonOptions);
Console.WriteLine(await getOpenOrders.InvokeAsync(new AIFunctionArguments()));

[Description("Gets the customer's open orders.")]
static IReadOnlyList<Order> GetOpenOrders() =>
[
new Order("A-1001", "Keyboard"),
new Order("A-1002", "Mouse"),
];

record Order(string Number, string Item);

// Source-generated JSON contracts for the tool's return type.
[JsonSerializable(typeof(IReadOnlyList<Order>))]
partial class OrdersJsonContext : JsonSerializerContext;
PieceWhat it does
new(AIJsonUtilities.DefaultOptions)Copies Microsoft.Extensions.AI's default options, with their contracts and settings, into options you can change
TypeInfoResolverChain.Insert(0, OrdersJsonContext.Default)Puts your contracts first. System.Text.Json's guide on combining source generators shows this exact call, and says the options query each of the resolvers in their specified order and return the first result that's non-null
serializerOptions: jsonOptionsHands those options to AIFunctionFactory.Create, which uses them instead of the defaults
[JsonSerializable(typeof(IReadOnlyList<Order>))]Asks the source generator for contracts of the return type. Name the type the method declares; the same guide says to specify source generation for a collection, use [JsonSerializable] with the collection type
partial class OrdersJsonContext : JsonSerializerContextThe class the source generator fills in when the app builds

After the Fix​

With #:property PublishAot=false, the file runs and prints the tool's result:

Output
[
{
"number": "A-1001",
"item": "Keyboard"
},
{
"number": "A-1002",
"item": "Mouse"
}
]

macOS-style terminal window showing dotnet run orders.cs, now with Native AOT turned off, printing a JSON array with two orders: number A-1001, item Keyboard, and number A-1002, item Mouse

The source-generated version prints the same JSON with dotnet run. It also survives the real thing, a Native AOT publish. Publishing a file-based app needs the platform's Native AOT build tools, which Microsoft's Native AOT deployment page lists:

Terminal
dotnet publish orders.cs
Output
Restore complete (0.2s)
orders.cs net10.0 osx-arm64 succeeded (8.4s) → artifacts/orders/
Build succeeded in 8.7s

The executable lands in an artifacts folder next to the file, with a subdirectory named after the application, as the File-based apps page says. On my Mac it is a 6 MB native program, and running it prints the same two orders:

Terminal
./artifacts/orders/orders
Output
[
{
"number": "A-1001",
"item": "Keyboard"
},
{
"number": "A-1002",
"item": "Mouse"
}
]

The JSON is the same as with Native AOT turned off, with the same property names and indentation, because the options started as a copy of AIJsonUtilities.DefaultOptions. Its reference lists WriteIndented among the settings it turns on.

Common Mistakes​

MistakeWhat happensDo this instead
New, empty JsonSerializerOptions with only your contextThe tool works, but its JSON loses Microsoft.Extensions.AI's defaults: I got [{"Number":"A-1001","Item":"Keyboard"},{"Number":"A-1002","Item":"Mouse"}]Copy AIJsonUtilities.DefaultOptions and insert your context in front
[JsonSerializable(typeof(Order))] for a method that returns IReadOnlyList<Order>The list type still has no contractName the exact type the method returns
Turning off Native AOT in an app you meant to publish nativelyThe app runs, but it isn't Native AOT any moreKeep AOT and use a source-generated context

Troubleshooting​

SymptomLikely causeFix
The same error names a different typeAnother tool returns a type without a contractAdd a [JsonSerializable] for that tool's return type too
The error appears in a normal projectThe project sets PublishAot, PublishTrimmed or JsonSerializerIsReflectionEnabledByDefaultUse a source-generated context, as in the second fix

FAQ​

Why does it fail when the tool is created, not when it runs?​

AIFunctionFactory.Create prepares the JSON handling for the method's parameters and return value up front. The stack trace shows it: the exception comes from GetReturnParameterMarshaller, inside Create.

Does an agent built with Microsoft Agent Framework need any of this in a regular project?​

Not for reflection-based apps: a console app or an ASP.NET Core API created from the usual templates doesn't target Native AOT, and AIFunctionFactory.Create serializes your types through reflection. You need contracts as soon as reflection is off.

Is the warning-free publish proof that the whole agent is Native AOT ready?​

No. It shows that this file, with its one tool, publishes and runs natively. A real agent brings more packages, and Microsoft's Native AOT page says the publish analyzes the entire project and its dependencies for possible limitations and warns about each one, so publish your own app and read its warnings.

Conclusion​

JsonTypeInfo metadata for type ... was not provided by TypeInfoResolver of type 'Microsoft.Extensions.AI.AIJsonUtilities+JsonContext' means AIFunctionFactory.Create had to prepare JSON for a tool's return type with reflection turned off. In a .NET 10 file-based app that is the default, because file-based apps target Native AOT. Add #:property PublishAot=false when you don't need Native AOT, or keep it and pass AIFunctionFactory.Create options with a source-generated JsonSerializerContext for your return types, inserted in front of a copy of AIJsonUtilities.DefaultOptions.

Additional Resources​