SchemaDoctor 0.4.1
dotnet add package SchemaDoctor --version 0.4.1
NuGet\Install-Package SchemaDoctor -Version 0.4.1
<PackageReference Include="SchemaDoctor" Version="0.4.1" />
<PackageVersion Include="SchemaDoctor" Version="0.4.1" />
<PackageReference Include="SchemaDoctor" />
paket add SchemaDoctor --version 0.4.1
#r "nuget: SchemaDoctor, 0.4.1"
#addin nuget:?package=SchemaDoctor&version=0.4.1
#tool nuget:?package=SchemaDoctor&version=0.4.1
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 viaChatResponse<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 | 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 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. |
-
net8.0
- NJsonSchema (>= 11.3.2)
-
net9.0
- NJsonSchema (>= 11.3.2)
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.