To generate a Word or PDF document from LLM output in C#, have the model return JSON against a
JSON Schema, then pass that schema to JsonDataSource and let a Word template produce the
document. The model never writes the document; it only supplies the data that goes into it.
Here is why that split, and that schema, are the whole trick.
Ask a language model to produce a customer statement and you get something that reads well and is wrong in every way that matters: the wrong font, no logo, numbering that does not match the last one you sent, and wording legal never saw. Ask it to read a contract and pull out the facts, and it is very good.
So split the work. The model returns data. A template produces the document.
The version of this that survives contact with production has one extra piece: a JSON Schema, used on both sides. The full sample is on GitHub — llm-json-to-branded-documents-net.
What goes wrong without a schema
new DocumentAssembler().AssembleDocument(
"Customer card.docx",
"statement.docx",
new DataSourceInfo(new JsonDataSource("llm-output.json"), "customer"));
That works until the model returns something like this, which it will, because this is what the source document said:
{ "Name": "Quayside Bistro", "Account": "00042", "Cards": [], "Loyalty": null }
Both of these then go wrong:
An error has been encountered at the end of expression 'customer.Cards.First().Type]>'.
Can not get the value of member 'Type' on type 'System.Data.DataRow'.
and, more quietly, the account number 00042 arrives in the document as 42.
Neither is the model’s fault. Without a schema the engine has to learn the shape of the data from the data: an empty array has no element type, and a string of digits looks like a number.
How a JSON Schema fixes it, on both sides
Write the schema once. Give it to the model as the shape of the answer, and to the assembler as the shape of the data.
New in 26.11 — JSON Schema support for
JsonDataSource.
JsonDataSource dataSource = new JsonDataSource(
"data/Customer.json",
"data/Customer.schema.json");
new DocumentAssembler().AssembleDocument(
"templates/Json schema.docx",
"output/statement.docx",
new DataSourceInfo(dataSource, "customer"));
The same data now renders: Customer: Quayside Bistro, Account number: 00042, No payment card on file, and an empty loyalty line. Members the schema declares may be used by the template even when the data has them empty, null or missing, and their types come from the schema rather than from the first value that happens to be there.
The prompt describes the data, not the document
Return only a JSON object that satisfies the JSON Schema below. No prose, no markdown fences.
1. Use exactly the property names of the schema. The template reads those names.
2. Keep the declared types. A value the schema calls a string stays a string, even when it
looks like a number: an account number 00042 must not become 42.
3. When the source does not mention something, leave it out, or use null, or an empty array —
whichever the schema allows. Do not invent a value.
4. Do not add properties the schema does not declare.
Rule 3 is the one that earns its keep. A model told to be helpful will fill a gap with something plausible, and an invented loyalty level is far worse than a blank line — it is wrong in a document with your logo on it.
Nothing in the prompt mentions Word, fonts or layout. That is the point: the same prompt and the same schema serve a DOCX today and a PDF or an e-mail tomorrow.
Is model output safe to pass to a document engine?
The model read somebody else’s document. If that document contained an image URL, the model may
hand it back to you, and a tag such as <<image [customer.PhotoUrl]>> would make your server
fetch it — possibly from an address inside your own network.
Since 26.11 that does not happen by default: a reference that comes from data is skipped, while one the template itself carries is loaded as before. Your designer’s logo keeps working; a value derived from an incoming e-mail does not reach the network.
LoadSaveOptions options = new LoadSaveOptions();
options.WhitelistedResources.Add("images.example.com"); // hosts you trust
options.ExternalResourceHandler = new LocalImageHandler(path); // and see every decision
The handler can supply the bytes itself, so the assembly step needs no outbound network at all:
public override void OnLoading(ExternalResourceLoadingArgs args)
{
Console.WriteLine($"{args.Origin} resource {args.Uri}: {(args.Skipped ? "skipped" : "allowed")}");
if (!args.Skipped && args.Uri.StartsWith("https://images.example.com/", StringComparison.OrdinalIgnoreCase))
args.Data = File.ReadAllBytes(mImagePath);
}
What to keep in mind
- Validation is still yours. The schema protects the template, not the truth of the data. A total that does not match the lines is a business rule, and business rules belong to you.
- Store the JSON. A document you cannot reproduce from stored data is a document you cannot explain six months later. Keep the model’s answer, not just the file.
- The library never sees the model. It sees a JSON file and a schema, so swapping the model, the SDK or moving to a local runtime changes nothing downstream.
Questions people ask
How do I generate a Word document from LLM output in C#?
Ask the model for JSON that matches a JSON Schema, build a JsonDataSource from the JSON and that
same schema, and call DocumentAssembler.AssembleDocument with a Word template. The document comes
from the template, never from the model.
Why does the model’s "00042" become 42 in the document?
Without a schema the engine infers types from the data, and a string of digits looks like a number.
Declare the field as "type": "string" in the schema and it stays 00042.
What happens when the model omits an optional field?
With a schema the field is known and renders empty. Without one, use
DocumentAssemblyOptions.AllowMissingMembers, and read
Templates that survive incomplete data
first — it is easy to hide a real problem with it.
Can the output be a PDF instead of a DOCX?
Yes, from the same call: add new LoadSaveOptions(FileFormat.Pdf).
Which models does this work with? Any that can return structured output against a JSON Schema — the schema is the only contract, and nothing in the template depends on the provider.
Try it
git clone https://github.com/groupdocs-assembly/llm-json-to-branded-documents-net.git
cd llm-json-to-branded-documents-net
dotnet run --project src/LlmDocuments -- generate
It runs offline with sample data. Set ANTHROPIC_API_KEY and use extract to put a real model in
front of it.
The step-by-step version is in the documentation: Generate documents from model output. If the data has holes for reasons that have nothing to do with a model, read Templates for incomplete data next.