LarchSys.OpenAi.JsonSchema 0.1.0

dotnet add package LarchSys.OpenAi.JsonSchema --version 0.1.0                
NuGet\Install-Package LarchSys.OpenAi.JsonSchema -Version 0.1.0                
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="LarchSys.OpenAi.JsonSchema" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LarchSys.OpenAi.JsonSchema --version 0.1.0                
#r "nuget: LarchSys.OpenAi.JsonSchema, 0.1.0"                
#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.
// Install LarchSys.OpenAi.JsonSchema as a Cake Addin
#addin nuget:?package=LarchSys.OpenAi.JsonSchema&version=0.1.0

// Install LarchSys.OpenAi.JsonSchema as a Cake Tool
#tool nuget:?package=LarchSys.OpenAi.JsonSchema&version=0.1.0                

OpenAi JsonSchema

GitHub Actions Workflow Status NuGet Version

OpenAi-JsonSchema is a lightweight library for generating valid JSON Schema for OpenAI's Structured Outputs feature, ensuring compatibility with OpenAI's JSON Schema subset. It simplifies the creation of structured outputs for OpenAI models, following the schema generation guidelines provided by OpenAI.

Features

  • Supports System.ComponentModel.DescriptionAttribute for descriptions.
  • Handles nullable reference types.
  • Supports a wide range of types, including primitives (e.g., bool, int, double, DateTime).
  • Ensures compatibility with OpenAI's JSON Schema format.

Installation

Install via NuGet:

Install-Package LarchSys.OpenAi.JsonSchema

Quick Start

The following example demonstrates how to generate a JSON Schema using the LarchSys.OpenAi.JsonSchema library.

var options = new JsonSchemaOptions(SchemaDefaults.OpenAi);

var resolver = new DefaultSchemaGenerator();
var schema = resolver.Generate<Document>(options);

var json = schema.ToJsonNode().ToJsonString(new JsonSerializerOptions() { WriteIndented = true });
output.WriteLine(json);
Assert.NotNull(json);

[Description("A document")]
public record Document(
    [property: Description("Id of the document")] int Id,
    [property: Description("Document name")] string Name,
    [property: Description("Text lines of the document")] Line[] Lines,
    [property: Description("Next document in order")] Document? Next,
    [property: Description("Prev document in order")] Document? Prev
);

[Description("A line of text in a document")]
public record Line(
    [property: Description("Line number")] int Number,
    [property: Description("Line text")] string Text
);

Example Output

{
  "type": "object",
  "description": "A document",
  "properties": {
    "Id": {
      "type": "integer",
      "description": "Id of the document"
    },
    "Name": {
      "type": "string",
      "description": "Document name"
    },
    "Lines": {
      "type": "array",
      "description": "Text lines of the document",
      "items": {
        "type": "object",
        "description": "A line of text in a document",
        "properties": {
          "Number": {
            "type": "integer",
            "description": "Line number"
          },
          "Text": {
            "type": "string",
            "description": "Line text"
          }
        },
        "required": [
          "Number",
          "Text"
        ],
        "additionalProperties": false
      }
    },
    "Next": {
      "description": "Next document in order",
      "anyOf": [
        { "type": "null" },
        { "$ref": "#" }
      ]
    },
    "Prev": {
      "description": "Prev document in order",
      "anyOf": [
        { "type": "null" },
        { "$ref": "#" }
      ]
    }
  },
  "required": [
    "Id",
    "Name",
    "Lines",
    "Next",
    "Prev"
  ],
  "additionalProperties": false
}

How It Works

OpenAi JsonSchema simplifies the generation of JSON Schema for structured outputs using C# classes and attributes. It leverages the System.ComponentModel.DescriptionAttribute for field descriptions and supports nullable reference types, ensuring full compatibility with the JSON Schema language supported by OpenAI models.

For more details on the OpenAI Structured Outputs feature, check out:

Contributing

Contributions are welcome! Please fork this repository and submit a pull request with any improvements or feature additions. All contributions should follow the repository's guidelines.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Contact

For any questions or issues, feel free to reach out via GitHub or email.

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. 
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
0.1.0 27 10/3/2024
0.1.0-preview-3 31 10/3/2024