ModelArtifacts.NET
0.1.1
dotnet add package ModelArtifacts.NET --version 0.1.1
NuGet\Install-Package ModelArtifacts.NET -Version 0.1.1
<PackageReference Include="ModelArtifacts.NET" Version="0.1.1" />
<PackageVersion Include="ModelArtifacts.NET" Version="0.1.1" />
<PackageReference Include="ModelArtifacts.NET" />
paket add ModelArtifacts.NET --version 0.1.1
#r "nuget: ModelArtifacts.NET, 0.1.1"
#:package ModelArtifacts.NET@0.1.1
#addin nuget:?package=ModelArtifacts.NET&version=0.1.1
#tool nuget:?package=ModelArtifacts.NET&version=0.1.1
ModelArtifacts.NET
ModelArtifacts.NET is a reusable .NET 10 library for safely acquiring, versioning, caching, verifying, activating, rolling back, and cleaning up AI/model artifacts.
It deliberately does not know how to run a model. It manages files and artifact snapshots. Your embedding library, reranker, LLM host, tokenizer package, classifier, or application decides what those files mean and whether a candidate snapshot is valid.
That boundary is the whole point.
Why this exists
Model consumers repeatedly need the same infrastructure:
- resolve a source revision;
- discover the files that belong to a runtime artifact set;
- download into isolated staging;
- verify sizes and SHA-256 hashes;
- keep partial downloads away from active files;
- retain a known-good current snapshot;
- survive remote outages by continuing from cache;
- prevent concurrent processes from corrupting the same cache;
- promote an update only after the consuming application proves it can actually use it.
ModelArtifacts.NET centralizes that machinery without importing ONNX Runtime, tokenizers, inference queues, vector semantics, prompts, context-window rules, or model-family presets.
The lifecycle: source → candidate → validation → promotion
A newly downloaded revision does not become current just because the transfer succeeded.
resolve source revision
↓
download to isolated staging
↓
verify transfer integrity
↓
create candidate snapshot
↓
RETURN CANDIDATE TO YOUR APPLICATION
↓
your application validates / loads it
↓
PromoteAsync(candidate)
↓
new snapshot becomes current atomically
↓
old snapshots may be cleaned
If application-specific validation fails, call DiscardAsync(candidate). The previous current snapshot remains untouched.
This is intentionally a two-phase activation protocol. A valid ZIP/download/hash is not necessarily a valid embedding model, reranker, tokenizer, GGUF, ONNX graph, or application-specific artifact set.
Install
dotnet add package ModelArtifacts.NET
The managed package targets .NET 10, enables nullable reference types, is trimming-friendly, and declares IsAotCompatible=true.
Candidate-first usage
using ModelArtifacts;
var source = new HuggingFaceArtifactSource(
repositoryId: "my-org/my-model",
selection: ArtifactSelection.Explicit(
"runtime-int8-v1",
"model.int8.onnx",
"tokenizer.json",
"config.json"));
using var artifacts = new ArtifactManager(source, new ArtifactManagerOptions
{
UpdatePolicy = ArtifactUpdatePolicy.OnStartup
});
using var candidate = await artifacts.ResolveCandidateAsync();
try
{
// This is intentionally application-owned policy.
// Example: construct your tokenizer, parse config, create an ONNX session,
// validate tensor names, load a GGUF file, etc.
await ValidateForMyApplicationAsync(candidate.Snapshot.DirectoryPath);
await artifacts.PromoteAsync(candidate);
}
catch
{
await artifacts.DiscardAsync(candidate);
throw;
}
ArtifactCandidate owns a temporary file lease while the application validates or loads its files. Dispose every candidate, including already-current candidates. PromoteAsync and DiscardAsync also release that lease. RequiresPromotion becomes false after promotion, discard, or disposal. Calling discard on a successfully promoted candidate cannot delete the current snapshot.
For an application that may open snapshot files throughout a model generation's lifetime, retain an independent lease before promotion:
using var candidate = await artifacts.ResolveCandidateAsync();
var generationLease = await artifacts.AcquireSnapshotLeaseAsync(candidate);
try
{
await ValidateForMyApplicationAsync(candidate.Snapshot.DirectoryPath);
await artifacts.PromoteAsync(candidate);
// Transfer generationLease to the loaded generation. Dispose it only after
// its model/session/tokenizer and every in-flight operation have drained.
}
catch
{
generationLease.Dispose();
await artifacts.DiscardAsync(candidate);
throw;
}
The lease names the exact candidate snapshot and therefore does not race another process's current-pointer update. AcquireCurrentAsync obtains a leased current snapshot without a remote check; GetCurrentAsync returns metadata only. Local-directory leases are harmless no-ops. The manager must outlive its candidates and generation leases: disposing it releases its outstanding leases.
Promotion's atomic current.json replacement is the activation commit point. Cancellation before it leaves the previous current intact. Cleanup failures or cancellation after it produce optional diagnostics, not a failed activation result. See migration and review follow-up for changed cache identities and caller contracts.
Artifact snapshots
ArtifactSnapshot exposes generic information only:
ArtifactSetIdSourceKindSourceIdentitySourceRevisionArtifactFingerprintDirectoryPath- selected
AssetPaths - whether the directory is managed by the cache
Use snapshot.GetAssetPath("relative/path") to get an absolute path rooted inside the snapshot.
The artifact fingerprint is deterministic over the selected managed artifact paths and file contents. Changing a managed artifact changes the fingerprint. The library makes no claim about embedding-space identity; a consumer may incorporate ArtifactFingerprint into a higher-level identity if useful.
Hugging Face
Hugging Face sources resolve the requested revision (default main) through the model API and use the concrete source SHA when Hugging Face provides it.
var source = new HuggingFaceArtifactSource(
"org/repository",
ArtifactSelection.Patterns(
"onnx-int8-runtime-v1",
"onnx/int8/*",
"tokenizer/*"),
revision: "main",
accessToken: Environment.GetEnvironmentVariable("HF_TOKEN"));
Bearer credentials are attached to both repository-resolution and asset-download requests when configured.
Asset selection is explicit
The Hugging Face source requires an ArtifactSelection. It does not blindly download an entire repository and it does not assume that .onnx or tokenizer.json are special.
Selections can be explicit paths:
ArtifactSelection.Explicit(
"fp32-runtime",
"model.onnx",
"model.onnx_data",
"tokenizer.json");
or patterns:
ArtifactSelection.Patterns("int4-export", "exports/int4/*");
ArtifactSelection.All("stable-selection-id") is available when downloading the full repository is genuinely intended.
The selection identity is part of cache identity. Two variants from the same repository and revision therefore remain isolated:
same repository
├── INT4 selection
├── INT8 selection
└── FP32 selection
HTTP manifests
HttpManifestArtifactSource supports a generic JSON manifest. It is not ONNX-specific.
{
"artifactSetId": "acme-reranker-int8",
"revision": "2026-08-12.1",
"assets": [
"tokenizer.json",
{
"path": "weights/model.bin",
"url": "https://cdn.example.com/releases/model.bin",
"size": 12345678,
"sha256": "0123456789abcdef..."
}
]
}
String assets resolve relative to the manifest URL. Object assets may use relative or absolute URLs and may supply expected size and sha256 values.
For compatibility with earlier consumers, modelId is also accepted when artifactSetId is absent.
Custom request headers can be provided to the manifest source for private/internal HTTP stores. They are sent only to the manifest's origin (scheme, host and port). An explicit trustedDownloadOrigins list may opt trusted CDN origins into receiving those headers. Other asset origins still download, without the source's private headers. HTTPS redirects never downgrade to HTTP.
The manager's default transport disables automatic redirects and follows each hop explicitly, reevaluating headers at every origin. If you supply your own HttpClient, configure its handler with AllowAutoRedirect=false and do not put sensitive credentials in DefaultRequestHeaders. An injected client's handler/default headers are outside the library's control and can bypass origin scoping. The same requirement applies when directly calling a source's ResolveAsync with an injected client.
Local directories
Existing local directories are not copied into managed cache snapshots by default:
var source = new LocalDirectoryArtifactSource("/models/my-model");
using var manager = new ArtifactManager(source);
var candidate = await manager.ResolveCandidateAsync();
Console.WriteLine(candidate.Snapshot.DirectoryPath); // original directory
Console.WriteLine(candidate.Snapshot.IsManaged); // false
A selection can be supplied when only part of the directory should participate in artifact identity.
Custom sources
Implement IModelArtifactSource to add S3, Azure Blob, GitHub Releases, ModelScope, internal registries, or another distribution mechanism:
public sealed class MyArtifactSource : IModelArtifactSource
{
public string SourceKind => "my-store";
public string SourceIdentity => "models/acme";
public string CacheVariant => "runtime-v2";
public Task<ResolvedArtifactSet> ResolveAsync(
HttpClient httpClient,
CancellationToken cancellationToken)
{
// Resolve a concrete revision and return generic ArtifactAsset records.
throw new NotImplementedException();
}
public void ApplyDownloadHeaders(HttpRequestMessage request)
{
// Optional authentication for asset downloads.
}
}
No changes to ModelArtifacts.NET are required for a custom source.
Cache identity and layout
The managed cache identity includes:
source kind
source identity
source cache variant / artifact selection identity
optional explicit cache key
A managed cache root contains approximately:
<cache-root>/<stable-source-key-hash>/
.lock
current.json
staging/
snapshots/
<revision>-<fingerprint-prefix>/
... selected artifacts ...
current.json is small and replaced atomically. It records the active snapshot, source/cache identity, source revision, selected asset paths, and artifact fingerprint.
Candidate snapshots live under snapshots/ but are not referenced by current.json until promotion succeeds. Candidate and reader leases hold OS file locks under candidate-leases/<snapshot>/<lease-id>.lease. Process termination releases the locks; later cleanup reclaims stale markers and unreferenced snapshots. Live validators/readers remain protected. Cleanup also works when no current snapshot exists.
Hugging Face cache identity includes the requested ref in addition to selection identity. Distinct branches, tags and pinned commits never share a current pointer. Source resolution uses the revision endpoint and downloads from the concrete returned SHA.
Update policies
ArtifactUpdatePolicy supports:
OnStartup— resolve the source revision when the manager resolves its startup candidate;Manual— use an existing current snapshot without checking remotely unless explicitly refreshed;Never— never check for an update while a current snapshot exists.
A first acquisition still resolves the source when no current snapshot exists.
Force a remote resolution with:
var candidate = await manager.RefreshAsync();
or:
var candidate = await manager.ResolveCandidateAsync(forceRemoteCheck: true);
Offline cached fallback
If remote resolution fails and a valid current snapshot already exists, ResolveCandidateAsync returns the current snapshot with:
candidate.IsOfflineFallback == true
This lets services continue starting and serving with their known-good cached artifacts during Hugging Face/CDN/network outages.
If no valid current snapshot exists, the source failure is surfaced normally. Caller cancellation always propagates. Resolution timeouts can fall back. Transient transfer failures also fall back by default, controlled by FallbackOnTransientDownloadFailure; hash/size mismatches and non-transient HTTP failures remain errors.
The default CacheValidation=ArtifactCacheValidation.Fingerprint checks selected files against the recorded aggregate SHA-256 fingerprint whenever accepting current. Missing, truncated or changed files invalidate current and trigger same-revision reacquisition. An explicit ExistenceAndSize setting reduces disk reads but cannot detect same-size corruption; legacy cache records without file-size metadata still receive a full hash check. Managed files must not be modified while in use. This is corruption detection, not authentication against a local actor who can replace both metadata and files.
SourceResolutionTimeout defaults to 100 seconds. DownloadTimeout defaults to 30 minutes per complete transfer attempt, including reading the body after headers; increase it for slow/large artifacts or use Timeout.InfiniteTimeSpan with your own cancellation. Both respect caller cancellation. Diagnostics optionally receives cache-validation, fallback and cleanup outcomes; a throwing diagnostic callback never changes the operation's result. Avoid logging exception details containing confidential source data without your own redaction.
Download and cache safety
Managed downloads use the following protections:
- isolated staging directories;
- path normalization and traversal rejection;
.partialfiles while transfer is incomplete;- atomic move into the final asset path only after transfer succeeds;
- expected-size validation when supplied;
- SHA-256 verification when supplied;
- bounded retries for transient HTTP/network/I/O failures;
- transient HTTP handling for 408, 429, 500, 502, 503, and 504;
Retry-Aftersupport;- cross-process cache-directory locking with cancellation-aware waits;
- atomic
current.jsonreplacement; - best-effort cleanup of old snapshots only after successful promotion;
- retry behavior for temporarily locked files;
- abandoned staging cleanup.
Downloads hash each chunk while writing and reuse those digests for the artifact fingerprint. They do not reread each completed download merely to compute the aggregate. MaxConcurrentDownloads is a bounded per-acquisition limit (default 1); increase it only after measuring your artifact mix and storage/network limits. Validation before promotion deliberately rechecks the candidate's contents after application validation.
A cleanup failure does not roll back or invalidate a successfully promoted current snapshot.
What the consuming model library should own
A consumer such as an embedding package should provide its own model policy:
which repository/export to select
which files are required
precision/flavor presets
model configuration interpretation
tokenizer requirements
model/session loading
tensor contracts
context-window semantics
embedding-space identity
ModelArtifacts.NET should provide the rest:
source resolution
downloads
retries
hash/size validation
staging
cache identity
candidate snapshots
promotion/discard
current snapshot metadata
offline fallback
locking
cleanup
artifact fingerprints
That means a future OnnxTextEmbeddings.NET refactor can load and validate an embedding candidate, then promote it only after its tokenizer and ONNX runtime initialize successfully—without the artifact package knowing what either of those things are.
The same flow works unchanged for rerankers, classifiers, GGUF files, tokenizers, ONNX LLMs, or auxiliary assets.
Native AOT
The managed package is designed for Native AOT and uses source-generated System.Text.Json metadata for its internal persisted/source records.
The repository also contains src/ModelArtifacts.Native, which publishes as a Native AOT shared library exposing a stable C ABI.
The public header is:
native/include/model_artifacts.h
The ABI uses:
- explicit ABI version discovery;
struct_size+abi_versionon creation options;- opaque manager/candidate handles;
- UTF-8 pointer + byte length for JSON configuration;
- stable integer status codes;
- library-owned returned buffers;
ma_buffer_freefor allocator-safe release;- last-error retrieval;
- managed exception containment at every export.
Complex candidate metadata is returned as versionable JSON to keep the ABI small and evolvable.
CI and release
CI validates managed build/tests and Native AOT integration on Linux, Windows, and macOS. The Native AOT integration publishes the shared library, compiles a standalone C consumer against the public header, and exercises real local artifact-manager behavior including buffer/error handling.
Merging package-affecting changes into release triggers .github/workflows/publish-nuget.yml. It detects package changes, resolves the next version from both Git tags and NuGet.org, validates across major OSes, packs the exact version, validates the .nupkg, authenticates with NuGet trusted publishing, tags vX.Y.Z, and creates a GitHub Release.
README, icon, license, source, and build-property changes are considered package-affecting because they are shipped by the package.
License
Apache-2.0.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ModelArtifacts.NET:
| Package | Downloads |
|---|---|
|
OnnxTextEmbeddings.NET
CPU-friendly ONNX text embeddings and lightweight semantic search for .NET. |
GitHub repositories
This package is not used by any popular GitHub repositories.