Kontent.Ai.ModelGenerator
10.2.0
dotnet tool install --global Kontent.Ai.ModelGenerator --version 10.2.0
dotnet new tool-manifest
dotnet tool install --local Kontent.Ai.ModelGenerator --version 10.2.0
#tool dotnet:?package=Kontent.Ai.ModelGenerator&version=10.2.0
nuke :add-package Kontent.Ai.ModelGenerator --version 10.2.0
Kontent.ai model generator utility for .NET
This utility generates strongly-typed record-based models for the Kontent.ai Delivery SDK for .NET (v19+).
This version targets the modern Delivery SDK (v19+) only. If you need to generate models for the legacy Delivery SDK (v18.x and earlier), the Management SDK, or Extended Delivery, use the previous stable release.
What's New in Updated Delivery Models
The generated models use modern C# features and patterns:
- Records - Immutable
recordtypes with{ get; init; }accessors - Modern types -
RichTextContent,Asset,TaxonomyTerm,IEmbeddedContent - Partial records - Easily extendable without modifying generated code
ContentTypeCodenameattribute - For source-generated TypeProvider discoveryContentTypeCodenameconstant - Access the content type codename at compile time (usable inswitch/caselabels, attribute arguments, and other contexts that require a compile-time constant) without reflection
If an element codename would produce a property or constant that collides with the built-in ContentTypeCodename constant (e.g., an element named content_type_codename or content_type), the element's member is automatically prefixed with an underscore (_ContentTypeCodename) to avoid conflicts. The [JsonPropertyName] attribute ensures deserialization still works correctly.
Installation & Usage
.NET Tool (Recommended)
The recommended way of obtaining this tool is installing it as a .NET Tool. You can install it as a global tool or per project as a local tool.
Global Tool
dotnet tool install -g Kontent.Ai.ModelGenerator
KontentModelGenerator --environmentId "<environmentId>" \
[--namespace "<custom-namespace>"] \
[--outputdir "<output-directory>"] \
[--baseRecord "<base-record-name>"] \
[--nullability strict|semantic]
Local Tool
dotnet new tool-manifest
dotnet tool install Kontent.Ai.ModelGenerator
dotnet tool run KontentModelGenerator --environmentId "<environmentId>" \
[--namespace "<custom-namespace>"] \
[--outputdir "<output-directory>"] \
[--baseRecord "<base-record-name>"] \
[--nullability strict|semantic]
Standalone apps for Windows, Linux, macOS
Self-contained apps are an ideal choice for machines without any version of .NET installed.
Latest release: Download
<details> <summary>Building a self-contained binary for a specific platform</summary>
- Clone the repository
- Navigate to
src/Kontent.Ai.ModelGenerator dotnet build -r <RID>to build (see the list of all RIDs)dotnet publish -c release -r <RID>to publish
</details>
Parameters
| Short key | Long key | Required | Default value | Description |
|---|---|---|---|---|
-i |
--environmentId |
Yes | null |
A GUID that can be found in Kontent.ai → Environment settings → Environment ID |
-n |
--namespace |
No | KontentAiModels |
A name of the C# namespace |
-o |
--outputdir |
No | ./ |
An output folder path |
-t |
--withtypeprovider |
No | false |
(Obsolete) TypeProvider is now source-generated by the Delivery SDK. |
-b, -r |
--baseRecord |
No | null |
If provided, a base record will be created and all generated records will derive from it via partial extender records |
--nullability |
No | strict |
Either strict or semantic. See Nullability mode. |
CLI Syntax
Short keys such as -n "MyModels" are interchangeable with the long keys --namespace "MyModels". Other possible syntax is -n=MyModels or --namespace=MyModels. Parameter values are case-insensitive. To see all aspects of the syntax, see the MS docs.
Config file
These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.
Advanced configuration (Preview API, Secure API)
There are two ways of configuring advanced Delivery SDK options (such as secure API access, preview API access, and others):
Command-line arguments:
--DeliveryOptions:UseSecureAccess true --DeliveryOptions:SecureAccessApiKey <SecuredApiKey>appSettings.json- suitable for the standalone app release
Generated Model Example
Generated file: Article.cs
// <auto-generated>
// This code was generated by Kontent.ai model generator tool
// (see https://github.com/kontent-ai/model-generator-net).
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// To extend this record, create a separate partial record with the same name.
// </auto-generated>
#nullable enable
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Kontent.Ai.Delivery.Abstractions;
using Kontent.Ai.Delivery.Attributes;
using Kontent.Ai.Delivery.ContentItems;
using Kontent.Ai.Delivery.ContentItems.RichText;
using Kontent.Ai.Delivery.SharedModels;
namespace KontentAiModels;
[ContentTypeCodename("article")]
public partial record Article
{
public const string BodyCopyCodename = "body_copy";
public const string CustomTrackingCodeCodename = "custom_tracking_code";
public const string PersonasCodename = "personas";
public const string PostDateCodename = "post_date";
public const string RelatedArticlesCodename = "related_articles";
public const string TeaserImageCodename = "teaser_image";
public const string TitleCodename = "title";
public const string UrlPatternCodename = "url_pattern";
public const string ContentTypeCodename = "article";
[JsonPropertyName("body_copy")]
public RichTextContent? BodyCopy { get; init; }
[JsonPropertyName("custom_tracking_code")]
public string? CustomTrackingCode { get; init; }
[JsonPropertyName("personas")]
public IEnumerable<TaxonomyTerm>? Personas { get; init; }
[JsonPropertyName("post_date")]
public DateTimeContent? PostDate { get; init; }
[JsonPropertyName("related_articles")]
public IEnumerable<IEmbeddedContent>? RelatedArticles { get; init; }
[JsonPropertyName("teaser_image")]
public IEnumerable<Asset>? TeaserImage { get; init; }
[JsonPropertyName("title")]
public string? Title { get; init; }
[JsonPropertyName("url_pattern")]
public string? UrlPattern { get; init; }
}
Nullability mode
The generator supports two nullability strategies for element properties via the --nullability flag:
strict (default)
Every element property is generated as a nullable type — string?, RichTextContent?, IEnumerable<Asset>?, etc. This is the conservative default. It's also useful if you use the Delivery SDK's projection features (WithElements / WithoutElements) and want the type system to distinguish "not fetched" (null) from "fetched and empty" — projected-away elements surface as null at runtime.
public string? Title { get; init; }
public RichTextContent? BodyCopy { get; init; }
public IEnumerable<IEmbeddedContent>? RelatedArticles { get; init; }
semantic
Element properties match the runtime semantics of the Delivery API: empty text, rich text and collection elements always come back populated, so they're generated as non-nullable with sensible default initializers. Numbers, dates and custom elements can be genuinely unset, so they remain nullable.
public string Title { get; init; } = string.Empty;
public RichTextContent BodyCopy { get; init; } = RichTextContent.Empty;
public IEnumerable<IEmbeddedContent> RelatedArticles { get; init; } = [];
public double? Rating { get; init; }
public DateTimeContent? PostDate { get; init; }
public string? CustomTrackingCode { get; init; }
When combined with projection (WithElements / WithoutElements), an omitted element surfaces as the type's default ("", [], RichTextContent.Empty) rather than null — so "not fetched" and "fetched and empty" look the same. That's fine if your code doesn't branch on that distinction; if it does, prefer strict.
--nullability semantic requires Delivery SDK 19.2.0+ (for RichTextContent.Empty). It will become the default in the next major version of the model generator.
Customizing Generated Models
Since the generated models are partial records, you can extend them by creating your own partial record file:
Generated file: Article.cs (auto-generated)
namespace KontentAiModels;
[ContentTypeCodename("article")]
public partial record Article
{
public const string TitleCodename = "title";
// ... other constants and properties
public const string ContentTypeCodename = "article";
[JsonPropertyName("title")]
public string? Title { get; init; }
}
Your custom file: Article.Custom.cs (your customizations)
namespace KontentAiModels;
public partial record Article
{
// Add computed properties
public string Slug => Title?.ToLowerInvariant().Replace(" ", "-") ?? string.Empty;
// Add custom methods
public bool IsPublished() => PostDate is { DateTime: var dt } && dt <= DateTime.Now;
// Add validation
public bool IsValid() => !string.IsNullOrEmpty(Title) && BodyCopy != null;
}
The generator creates the base model, and you maintain customizations in separate files that won't be overwritten.
Need Management SDK or Legacy Delivery SDK Support?
This version supports the modern Delivery SDK (v19+) only. For other use cases, use the previous stable release:
- Legacy Delivery SDK (v18.x and earlier) models
- Management SDK models
- Extended Delivery models
Feedback & Contributing
Found a bug or have a feature request? Open an issue. Pull requests are welcome!
Wall of Fame
We would like to express our thanks to the following people who contributed and made the project possible:
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.2.0 | 89 | 5/5/2026 |
| 10.1.1 | 117 | 4/23/2026 |
| 10.1.0 | 99 | 4/21/2026 |
| 10.0.0 | 124 | 4/19/2026 |
| 10.0.0-beta-5 | 119 | 3/3/2026 |
| 10.0.0-beta-4 | 103 | 3/3/2026 |
| 10.0.0-beta-3 | 116 | 2/20/2026 |
| 10.0.0-beta-2 | 119 | 1/11/2026 |
| 10.0.0-beta | 221 | 10/26/2025 |
| 9.0.0 | 2,437 | 4/8/2025 |
| 8.4.0 | 5,975 | 12/12/2023 |
| 8.3.3 | 2,904 | 6/4/2023 |
| 8.3.2 | 480 | 5/18/2023 |
| 8.3.1 | 454 | 5/5/2023 |
| 8.3.0 | 524 | 4/20/2023 |
| 8.3.0-beta.6 | 233 | 4/13/2023 |
| 8.3.0-beta.3 | 215 | 2/16/2023 |
| 8.3.0-beta.2 | 205 | 2/16/2023 |
| 8.3.0-beta.1 | 213 | 2/16/2023 |
| 8.2.0 | 1,222 | 2/9/2023 |