SchemaDoctor 0.4.1

dotnet add package SchemaDoctor --version 0.4.1
                    
NuGet\Install-Package SchemaDoctor -Version 0.4.1
                    
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="SchemaDoctor" Version="0.4.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SchemaDoctor" Version="0.4.1" />
                    
Directory.Packages.props
<PackageReference Include="SchemaDoctor" />
                    
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 SchemaDoctor --version 0.4.1
                    
#r "nuget: SchemaDoctor, 0.4.1"
                    
#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.
#addin nuget:?package=SchemaDoctor&version=0.4.1
                    
Install SchemaDoctor as a Cake Addin
#tool nuget:?package=SchemaDoctor&version=0.4.1
                    
Install SchemaDoctor as a Cake Tool

SchemaDoctor

<img src="logo.png" alt="SchemaDoctor Logo" width="200px;">

A .NET helper library to fix schema issues when working with Large Language Models (LLMs). It helps handle common hallucination problems by attempting to map potentially incorrect responses to your expected schema.

It is also able to pick out json responses returned after chain of thought or reasoning tokens, as long as it matches a given json schema.

Installation

dotnet add package SchemaDoctor

For Microsoft.Extensions.AI integration:

dotnet add package SchemaDoctor.Microsoft.Extensions.AI

Key Features

  • Fixes common LLM response issues like incorrect data types and malformed JSON
  • Maps string representations to proper numeric/boolean values
  • Handles arrays that are incorrectly returned as strings
  • Supports case-insensitive property mapping
  • Integrates with Microsoft.Extensions.AI, both for AIFunction and structured responses via ChatResponse<T>.

Basic Usage

public class MyResponse
{
    public int Number { get; set; }
    public bool Flag { get; set; }
    public string[] Tags { get; set; }
}

// Try to map potentially malformed JSON to your schema
if (SchemaTherapist.TryMapToSchema<MyResponse>(llmResponse, out var parsed))
{
    // Successfully mapped to schema
    Console.WriteLine($"Number: {parsed.Number}");
}
else
{
    Console.WriteLine($"Failed to parse response");
}

Microsoft.Extensions.AI Integration

Schema Doctor provides extensions for Microsoft.Extensions.AI to make function calling more robust:

// Create a function with hallucination therapy
var function = AIFunctionFactoryWithTherapy.CreateFunction(
    (int number, string text) => $"Received {number} and {text}",
    name: "example",
    description: "An example function"
);

// Wrap a function with hallucination therapy
AIFunction function = ...
var wrappedFunction = function.WithTherapy();

public static AIFunction WithTherapy(this AIFunction function)

// Use the completion extension to handle potential hallucinations
if (completion.TryToGetResultWithTherapy(out var result))
{
    Console.WriteLine($"Got result: {result}");
}

What It Fixes

  • String representations of numbers: "42"42
  • String representations of booleans: "true"true
  • String arrays represented as single strings: "[1,2,3]"[1,2,3]
  • Comma-separated strings to arrays: "red,green,blue"["red","green","blue"]
  • Case-insensitive property mapping
  • Schema definition responses instead of actual data
  • Malformed JSON structures

Contributing

Issues and pull requests are welcome on GitHub at https://github.com/mhelleborg/schema-doctor

License

This project is available under the MIT License.

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

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SchemaDoctor:

Package Downloads
SchemaDoctor.Microsoft.Extensions.AI

.NET helper library to fix schema issues when working with LLM's

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.1 182 5/22/2025
0.4.0 186 5/8/2025
0.3.1 558 3/12/2025
0.3.0 462 2/23/2025
0.2.2 228 2/11/2025
0.2.1 108 2/10/2025
0.2.0 125 2/10/2025
0.1.0 266 1/21/2025