Skip to main content

Getting structured data out of an LLM

· 9 min read

A lot of useful LLM work is not conversation at all. You hand the model an invoice, a contract or an email and you want fields back: a number, a date, a list of lines, something your code can store and add up. The model is good at reading messy documents. It is not, by default, good at handing back something a program can rely on.

The gap between those two is where most extraction code goes wrong. This post walks through closing it in C#, with an invoice as the running example: ask for a shape, check what comes back as if a stranger sent it, retry with a reason, and know when to stop and ask a person.

Three ways to ask for JSON​

The weakest way is to write "answer in JSON" in the prompt. Most of the time you get JSON. Some of the time you get JSON wrapped in a Markdown code fence, or JSON followed by a friendly sentence, or a field called invoice_no where you expected invoiceNumber. Each of those is a parsing bug that shows up one document in fifty.

The next step up is a JSON mode, where the provider promises the output is valid JSON. That fixes the fences and the trailing prose. It says nothing about the shape: any valid JSON object passes, including one with half your fields missing.

The strongest is a structured output mode, where you send a JSON Schema with the request and the provider constrains generation to it. Not every provider or model supports it, and the ones that do don't all support every JSON Schema keyword. Where it works, it settles the shape question: property names, types and required fields come back as asked.

What none of the three settle is whether the values are right. A schema can say total is a number. It can't say the number is the one printed on the invoice.

The schema comes from a C# type​

I don't write JSON Schema by hand. The type I want to end up with already describes the shape, so I let the library derive the schema from it:

using System.ComponentModel;

public sealed record InvoiceLine(
string Description,
[property: Description("Quantity as printed. Greater than zero.")] decimal Quantity,
[property: Description("Price per unit, before tax.")] decimal UnitPrice,
[property: Description("Line amount as printed on the invoice.")] decimal LineTotal);

[Description("An invoice, using only values printed on the document.")]
public sealed record Invoice(
[property: Description("Invoice number exactly as printed.")] string InvoiceNumber,
DateOnly IssueDate,
[property: Description("ISO 4217 code, e.g. EUR.")] string Currency,
IReadOnlyList<InvoiceLine> Lines,
decimal Subtotal,
decimal Tax,
decimal Total,
[property: Description("Purchase order number, or null if none is printed.")] string? PurchaseOrder);

AIJsonUtilities.CreateJsonSchema(typeof(Invoice)) in Microsoft.Extensions.AI turns this into a schema. The [Description] attributes become description keywords, DateOnly becomes a string with "format": "date", and the nullable PurchaseOrder becomes ["string", "null"]. Print the schema once and read it. It is exactly what the model will be held to, and it is the fastest way to spot a property you forgot to describe.

The nullable field matters more than it looks. It is the extraction version of letting a RAG model say "I don't know": if a field can't be null, a model that can't find it will produce something anyway. Make the optional fields optional, and say in the description what null means.

Asking with GetResponseAsync<T>​

Microsoft.Extensions.AI has a generic GetResponseAsync<T> on IChatClient that does the plumbing. It builds the schema from T, sends it as the response format, and gives back a ChatResponse<T> that can deserialize the answer:

using System.Text.Json;
using Microsoft.Extensions.AI;

JsonSerializerOptions json = new(AIJsonUtilities.DefaultOptions)
{
RespectNullableAnnotations = true,
RespectRequiredConstructorParameters = true,
};

ChatResponse<Invoice> response =
await chat.GetResponseAsync<Invoice>(messages, json, cancellationToken: ct);

if (response.TryGetResult(out Invoice? invoice))
{
// invoice has the right shape. Whether it is right is the next section.
}

The two Respect options are worth the lines. By default System.Text.Json is lenient: a missing property gets a default value and a null lands happily in a non-nullable string. With these set, a missing invoiceNumber or a null where the type says there can't be one fails deserialization instead of reaching your code as null or 0.

Use TryGetResult rather than Result. The documentation is explicit that models are not guaranteed to honour the schema, and Result throws when the text doesn't parse. A response cut off by the token limit is the usual way to get there: check response.FinishReason for ChatFinishReason.Length and raise the limit rather than retrying the same request.

If your model has no native schema support, pass useJsonSchemaResponseFormat: false. The library then asks for plain JSON and puts the schema in a message instead, which is weaker but still better than a sentence in the prompt.

The result is untrusted input​

Once it parses, treat the invoice the way you treat a request body from the internet. The model read the document, but it also may have misread a digit, swapped two columns, or filled a gap with something plausible. Invoices are kind to us here, because they carry their own checksums: lines multiply out, lines add up to the subtotal, subtotal plus tax is the total.

I write the checks as two methods, for a reason the next section explains:

static List<string> Validate(Invoice invoice)
{
List<string> errors = [];
if (invoice.Currency.Length != 3 || !invoice.Currency.All(char.IsAsciiLetterUpper))
errors.Add($"currency '{invoice.Currency}' is not a three-letter ISO 4217 code.");
if (invoice.IssueDate > DateOnly.FromDateTime(DateTime.UtcNow))
errors.Add($"issueDate {invoice.IssueDate:yyyy-MM-dd} is in the future.");
if (invoice.Lines.Count == 0)
errors.Add("lines is empty.");
for (int i = 0; i < invoice.Lines.Count; i++)
if (invoice.Lines[i].Quantity <= 0)
errors.Add($"lines[{i}].quantity is {invoice.Lines[i].Quantity}; it must be greater than zero.");
return errors;
}

static List<string> CheckArithmetic(Invoice invoice)
{
List<string> errors = [];
foreach (InvoiceLine line in invoice.Lines)
if (Math.Abs(line.Quantity * line.UnitPrice - line.LineTotal) > 0.01m)
errors.Add($"'{line.Description}': {line.Quantity} x {line.UnitPrice} is not {line.LineTotal}.");

decimal sum = invoice.Lines.Sum(l => l.LineTotal);
if (sum != invoice.Subtotal)
errors.Add($"the line totals add up to {sum}, but subtotal is {invoice.Subtotal}.");
if (invoice.Subtotal + invoice.Tax != invoice.Total)
errors.Add($"subtotal + tax is {invoice.Subtotal + invoice.Tax}, but total is {invoice.Total}.");
return errors;
}

Money is decimal, never double. The line check allows a cent of rounding because unit prices are often printed with more precision than the line amounts; the sums don't, because adding printed amounts should be exact. Each message names the field and both values, because these strings have a second reader.

Both together prove the invoice is consistent. They don't prove it matches the paper. A model that reads 18.00 as 13.00 on a line and in the subtotal passes every check above. That gap is why the last section exists.

A bounded retry that says what was wrong​

When validation fails, asking again with the same prompt is a coin toss. Asking again with the errors is a correction: the model sees its own answer and a list of exactly what didn't hold.

But not every error should go back to the model. "The line totals add up to 20.00, but subtotal is 25.00" can mean the model misread something, or it can mean the invoice itself is wrong; suppliers do send invoices that don't add up. Tell a model about the mismatch and it will make the numbers consistent, by changing whichever one is easiest. You end up with a clean record of an invoice that was never issued.

So only Validate runs inside the loop. Those are errors the model causes and can fix: a malformed currency, a skipped line, a date it invented.

for (int attempt = 1; attempt <= 3; attempt++)
{
ChatResponse<Invoice> response =
await chat.GetResponseAsync<Invoice>(messages, json, cancellationToken: ct);

errors = response.TryGetResult(out Invoice? invoice)
? Validate(invoice)
: ["The response did not match the schema."];
if (errors.Count == 0)
return (invoice, errors);

messages.AddMessages(response);
messages.Add(new(ChatRole.User,
"The result failed these checks:\n- " + string.Join("\n- ", errors) +
"\nRe-read the document and correct only what is wrong."));
}

return (null, errors); // goes to a person, with the errors attached

AddMessages appends the model's answer to the conversation, so the follow-up refers to something the model can see. The loop is bounded because every attempt costs a full request, and because a model that failed the same checks twice is unlikely to pass them on the fifth try. Three is a reasonable default; measure your own documents before changing it.

CheckArithmetic runs once, on the invoice the loop returns. If it reports anything, the invoice goes to a person, who can look at the paper. The model can't, and shouldn't be asked to guess.

When a person should look​

Some items should reach a person even when every check passes. A total above an amount your process cares about. A supplier you have never seen. A currency you don't usually pay in. Those are business rules, not extraction rules, and they belong in ordinary code after validation.

I don't ask the model how confident it is. A self-reported confidence score is another generated value, with nothing behind it that you can check. A failed check is evidence; a "0.92" is not.

What I do instead is sample. Take a few extractions that passed everything and compare them to the source documents by hand. That is the only way to find the misread digit that kept the arithmetic intact, and it tells you whether the human queue is too empty as well as whether it is too full.

None of this is specific to invoices. Ask for a shape the provider can enforce, deserialize strictly, check what the schema can't express, retry with reasons a bounded number of times, and give the rest to someone who can look at the source. The model does the reading; your code decides what to believe.