LLMParser 2.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package LLMParser --version 2.0.5
                    
NuGet\Install-Package LLMParser -Version 2.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LLMParser" Version="2.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LLMParser" Version="2.0.5" />
                    
Directory.Packages.props
<PackageReference Include="LLMParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LLMParser --version 2.0.5
                    
#r "nuget: LLMParser, 2.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LLMParser@2.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LLMParser&version=2.0.5
                    
Install as a Cake Addin
#tool nuget:?package=LLMParser&version=2.0.5
                    
Install as a Cake Tool

LLMParser

NuGet Downloads

LLMParser is a .NET library that converts and validates outputs from Large Language Models (LLMs) into strongly typed, reliable objects.

Why LLMParser

  • LLM‑focused: Built specifically to handle outputs from Large Language Models, not just generic JSON.
  • Automatic schema generation: Create validation schemas directly from your DTOs without boilerplate.
  • Reliable validation: Ensures responses strictly match expected structure before deserialization.
  • Flexible text conversion: Converts free‑form text like Name: Mark\nAge: 30 into valid JSON.
  • Format detection: Recognizes JSON and Key‑Value text formats automatically.
  • Error handling: Provides clear exceptions for invalid or unrecognized outputs.

Installation

dotnet add package LLMParser

Basic Usage


Person.cs:

public class Person
{
    public string Name { get; set; }          // Simple text
    public int Age { get; set; }              // Integer
    public decimal Salary { get; set; }       // Decimal
    public DateTime BirthDate { get; set; }   // Date
    public bool IsActive { get; set; }        // Boolean
}

Program.cs:

using LLMParser.Core;
using LLMParser.Json;
using LLMParser.Text;
using LLMParser.Schema;

string llmResponse = "{ \"name\": \"Mark\", \"age\": 30, \"salary\": 3000.50, \"birthDate\": \"1990-05-20\", \"isActive\": true }";

// Check if the response matches a recognized format (JSON or Key:Value text)
if (!ParserUtils.IsRecognizedFormat(llmResponse))
{
    Console.WriteLine("LLM output format not recognized. Cannot parse.");
    return;
}

if (ParserUtils.IsJson(llmResponse))
{
    try
    {
        // Validate and deserialize JSON
        var schema = SchemaFactory.For<Person>();
        var result = JsonParser.EnsureValidJson<Person>(llmResponse, schema);

        Console.WriteLine(
            $"Name: {result.Name}, " +
            $"Age: {result.Age}, " +
            $"Salary: {result.Salary}, " +
            $"BirthDate: {result.BirthDate:yyyy-MM-dd}, " +
            $"IsActive: {result.IsActive}"
        );
        return;
    }
    catch (InvalidLLMOutputException ex)
    {
        Console.WriteLine($"LLM output is invalid: {ex.Message}");
        return;
    }
}

// Expected output:
// Name: Mark, Age: 30, Salary: 3000.50, BirthDate: 1990-05-20, IsActive: True

Usage with Free-form Text


Person.cs:

public class Person
{
    public string Name { get; set; }          // Simple text
    public int Age { get; set; }              // Integer
    public decimal Salary { get; set; }       // Decimal
    public DateTime BirthDate { get; set; }   // Date
    public bool IsActive { get; set; }        // Boolean
}

Program.cs:

using LLMParser.Core;
using LLMParser.Text;
using LLMParser.Json;
using LLMParser.Schema;

string llmResponse = "Name: Mark\nAge: 30\nSalary: 3000.50\nBirthDate: 1990-05-20\nIsActive: true";

// Check if the response matches a recognized format (JSON or Key:Value text)
if (!ParserUtils.IsRecognizedFormat(llmResponse))
{
    Console.WriteLine("LLM output format not recognized. Cannot parse.");
    return;
}

// First check if the response matches Key:Value format
if (ParserUtils.IsKeyValueText(llmResponse))
{
    try
    {
        // Convert free-form text to JSON
        string json = TextToJsonConverter.ConvertKeyValueText(llmResponse);

        // Validate and deserialize
        var schema = SchemaFactory.For<Person>();
        var result = JsonParser.EnsureValidJson<Person>(json, schema);

        Console.WriteLine(
            $"Name: {result.Name}, " +
            $"Age: {result.Age}, " +
            $"Salary: {result.Salary}, " +
            $"BirthDate: {result.BirthDate:yyyy-MM-dd}, " +
            $"IsActive: {result.IsActive}"
        );
    }
    catch (InvalidLLMOutputException ex)
    {
        Console.WriteLine($"LLM output could not be parsed: {ex.Message}");
    }
}

// Expected output:
// Name: Mark, Age: 30, Salary: 3000.50, BirthDate: 1990-05-20, IsActive: True

Key Features

  • Schema generation: Automatically builds JSON schemas from DTOs.
  • JSON validation: Strictly validates and deserializes JSON against schemas.
  • Text‑to‑JSON conversion: Transforms free‑form text into structured JSON.
  • Unified format recognition: Detects JSON or Key‑Value text before validation.
  • Custom exceptions: InvalidLLMOutputException for clearer debugging.

Quick Reference methods

Method / Utility Description
SchemaFactory Generates JSON schemas automatically from DTOs.
JsonParser Validates and deserializes JSON against schemas.
TextToJsonConverter Converts free‑form text into JSON for validation.
InvalidLLMOutputException Exception thrown when LLM output doesn’t match schema or cannot be parsed.
IsJson Checks if a string is valid JSON.
IsKeyValueText Checks if text follows the Key: Value`` format.
IsRecognizedFormat Determines if output is JSON or Key:Value text before validation.

Support the Project

If you find this library useful, consider supporting its development:

Buy Me a Coffee

⚖️ License

This project is freely available under the MIT license.
You may use it without restrictions, as long as you retain the reference to the original license.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.6 154 6/12/2026
2.0.5 119 6/12/2026
2.0.4 113 6/12/2026
2.0.3 106 6/12/2026
2.0.2 114 6/12/2026
2.0.1 119 6/11/2026
2.0.0 117 6/11/2026
1.0.9 116 6/11/2026
1.0.8 116 6/11/2026
1.0.7 120 6/11/2026
1.0.6 117 6/11/2026
1.0.5 106 6/10/2026
1.0.0 119 6/10/2026