LLMParser 2.0.6
dotnet add package LLMParser --version 2.0.6
NuGet\Install-Package LLMParser -Version 2.0.6
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.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LLMParser" Version="2.0.6" />
<PackageReference Include="LLMParser" />
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.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LLMParser, 2.0.6"
#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.6
#: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.6
#tool nuget:?package=LLMParser&version=2.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
LLMParser
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: 30into 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:
InvalidLLMOutputExceptionfor 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:
⚖️ 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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- JsonSchema.Net (>= 9.2.1)
- NJsonSchema (>= 11.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
