Skip to main content

Fixed: The JSON value could not be converted to Expense. Path: $.category

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

I asked a Microsoft Agent Framework agent for an Expense through a response format, read the JSON the way Microsoft's example does, and got System.Text.Json.JsonException: The JSON value could not be converted to Expense. Path: $.category. The schema had asked for the enum as its name, and the options I read it with expected a number. Using the same options for both fixed it.

What I Was Doing​

I was writing the Structured Output tutorial. Its app reads a café receipt into an Expense record, which has an enum property:

Program.cs (the types)
record Expense(string Merchant, DateOnly Date, ExpenseCategory Category, string Currency, List<LineItem> Items, decimal Total);

enum ExpenseCategory { Meals, Travel, Office, Other }

RunAsync<Expense> had already worked. Then I added the second way Microsoft's documentation shows, a response format in the run's options, with the JSON read by JsonSerializer, as in its example:

Program.cs (before)
ChatResponseFormatJson format = ChatResponseFormat.ForJsonSchema<Expense>();

AgentResponse textResponse = await agent.RunAsync(
$"Extract the expense from this receipt:\n{Receipt}",
options: new AgentRunOptions { ResponseFormat = format });
Expense? fromText = JsonSerializer.Deserialize<Expense>(textResponse.Text, JsonSerializerOptions.Web);

What I Expected​

An Expense, like the one RunAsync<Expense> returned a moment earlier from the same receipt.

What I Got​

The run worked, and reading its JSON threw:

Output (the stack trace shortened)
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to Expense. Path: $.category | LineNumber: 0 | BytePositionInLine: 64.
at System.Text.Json.ThrowHelper.ThrowJsonException(String message)
at System.Text.Json.Serialization.Converters.EnumConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
…
at Program.<Main>$(String[] args) in /Users/you/source/ExpenseReader/Program.cs:line 55

macOS-style terminal window titled Terminal — ExpenseReader showing Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to Expense. Path: $.category, LineNumber 0, BytePositionInLine 64, with EnumConverter.Read in the stack trace and Program.cs line 55

What I Checked​

Where it failed. Path: $.category, in EnumConverter`1.Read: the enum property, and nothing else.

What the model wrote. The RunAsync<Expense> run just before had written the category as a name, "category":"Meals", and in JSON of that shape, byte 64 is where that value ends.

What the schema asked for. I printed format.Schema. Its category was {"type":"string","enum":["Meals","Travel","Office","Other"]}: the model was asked for a name, and did as asked.

The options on each side. ForJsonSchema was called without options, so it used Microsoft.Extensions.AI's default, AIJsonUtilities.DefaultOptions. Their reference: Enables string based enum serialization as implemented by JsonStringEnumConverter. The JSON was read with JsonSerializerOptions.Web, whose web defaults are case-insensitive camelCase names and numbers that may be strings, with no string enum converter, so it reads an enum only from a number.

Why Microsoft's example works. Its PersonInfo has a name, an age and an occupation: no enum, so the two sets of options never disagree.

Why It Happens​

The schema and the reader used different JsonSerializerOptions. ChatResponseFormat.ForJsonSchema<Expense>() described the enum as a string with the options that write enums as names, the model followed the schema, and JsonSerializerOptions.Web couldn't read a name into an enum.

The Fix​

Use one options object for both:

Program.cs (after)
// Build the schema and read the JSON with the same options, so enums are strings both ways.
JsonSerializerOptions jsonOptions = AIJsonUtilities.DefaultOptions;
ChatResponseFormatJson format = ChatResponseFormat.ForJsonSchema<Expense>(jsonOptions);

AgentResponse textResponse = await agent.RunAsync(
$"Extract the expense from this receipt:\n{Receipt}",
options: new AgentRunOptions { ResponseFormat = format });
Expense? fromText = JsonSerializer.Deserialize<Expense>(textResponse.Text, jsonOptions);

AIJsonUtilities is in Microsoft.Extensions.AI. Passing jsonOptions to ForJsonSchema changes nothing in the schema, since they were already the default: the printed schema was the same before and after. It makes the pairing visible, so nobody changes one side without the other.

If you only need the object, RunAsync<Expense> avoids the question entirely: it read the same JSON into an Expense without trouble.

After the Fix​

Output (the end)
From text: CORNER CAFE, 10.5 EUR

macOS-style terminal window titled Terminal — ExpenseReader showing the end of the run with From text: CORNER CAFE, 10.5 EUR

The JSON from the response-format run became an Expense.

Common Mistakes​

MistakeWhat happensDo this instead
Building the schema with one set of options and reading the JSON with anotherThe two can disagree, on enums and on anything else they format differentlyUse one options object for both
Copying the example's JsonSerializerOptions.Web for a type with an enumThis exceptionUse AIJsonUtilities.DefaultOptions on both sides
Making the enum a string to get rid of the errorThe model can write any categoryKeep the enum, and fix the options

Troubleshooting​

SymptomLikely causeFix
JsonException: The JSON value could not be converted to <type>. Path: $.<enum property>An enum written as a name, read by options without a string enum converterRead it with the options that built the schema
The error moves to another property after the enum is fixedThat property's format also differs between the two optionsThe same: one options object for both

FAQ​

Why did RunAsync<T> work?​

RunAsync<Expense> made the schema and read the answer itself, so both sides matched. The error only appears when you read the JSON yourself.

Are the enum names safe to send to the model?​

They're part of the schema, so the model reads them: choose names that say what each value means, as Meals and Travel do.

Conclusion​

The JSON value could not be converted to Expense. Path: $.category came from reading a structured output with different serializer options than the ones that built its schema: the schema asked for enum names, and JsonSerializerOptions.Web reads enums only as numbers. Build the schema with ChatResponseFormat.ForJsonSchema<T>(options) and read the JSON with the same options, such as AIJsonUtilities.DefaultOptions, or let RunAsync<T> do both.

Additional Resources​