---
title: "Generate tool JSON Schema in C#"
url: https://recursiveai.org/onellm/cookbook/generate-tool-json-schema-offline/
description: "Generate input and output JSON Schema from a local C# delegate. See how OneLLM handles annotations, defaults, and cancellation without an API key."
version: "1.0.1"
---

# Generate tool JSON Schema in C#

**OneLLM** is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/generate-tool-json-schema-offline/generate-tool-json-schema-offline.zip)

Use `Tool.Create` to describe a method’s arguments and return value. No model call or API key.

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneLlm.Tools;

var spec = Tool.Create("in_stock",
    (string sku, CancellationToken ct) =>
        sku == "A1").Definition;
Console.WriteLine(spec.InputSchema);
Console.WriteLine(spec.OutputSchema);
```

Unzip [the example](https://recursiveai.org/onellm/cookbook/generate-tool-json-schema-offline/generate-tool-json-schema-offline.zip), then `dotnet run`.

Prints the input schema for `sku`, then the Boolean output schema. The cancellation parameter isn’t a JSON property:

```text
{
  "type": "object",
  "properties": {
    "sku": {
      "type": "string"
    }
  },
  "required": [
    "sku"
  ],
  "additionalProperties": false
}
{
  "type": "boolean"
}
```

## The full program

The download builds a tool from the `Catalog.Reserve` method. The method’s signature and attributes are the only inputs. Generating the schemas doesn’t invoke the method.

```csharp
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using OneLlm.Tools;

var tool = Tool.Create(Catalog.Reserve).Definition;
var pretty = new JsonSerializerOptions { WriteIndented = true };
Console.WriteLine($"{tool.Name}: {tool.Description}");
Console.WriteLine("Input:");
Console.WriteLine(tool.InputSchema.ToJsonString(pretty));
Console.WriteLine("Output:");
Console.WriteLine(tool.OutputSchema?.ToJsonString(pretty));

record Reservation(string Sku, int Quantity);

static class Catalog
{
    [Description("Describe a stock reservation.")]
    public static Reservation Reserve(
        [Description("Stock code")] string sku,
        CancellationToken cancellationToken,
        [Range(1, 20)] int quantity = 1)
    {
        cancellationToken.ThrowIfCancellationRequested();
        return new Reservation(sku, quantity);
    }
}
```

`dotnet run` prints:

```text
reserve: Describe a stock reservation.
Input:
{
  "type": "object",
  "properties": {
    "sku": {
      "type": "string",
      "description": "Stock code"
    },
    "quantity": {
      "type": "integer",
      "minimum": 1,
      "maximum": 20,
      "default": 1
    }
  },
  "required": [
    "sku"
  ],
  "additionalProperties": false
}
Output:
{
  "type": "object",
  "properties": {
    "sku": {
      "type": "string"
    },
    "quantity": {
      "type": "integer",
      "minimum": -2147483648,
      "maximum": 2147483647
    }
  },
  "required": [
    "sku",
    "quantity"
  ],
  "additionalProperties": false
}
```

`sku` is required, while `quantity` is optional with a default of `1` and a range of `1` to `20`. The record returned by `Reserve` has its own schema. If the signature uses an unsupported type such as a multidimensional array, `Tool.Create` throws `NotSupportedException`.

## Variations

**Write the schema to a file.** After creating `spec` in the snippet at the top:

```csharp
File.WriteAllText("tool-input.json",
    spec.InputSchema.ToString());
```

## Related recipes

- [Test LLM code without an API key](https://recursiveai.org/onellm/cookbook/test-llm-code-without-api-key/)
- [Parse structured model output into a C# record](https://recursiveai.org/onellm/cookbook/structured-output-to-a-csharp-record/)

## The short answer

Pass a delegate to `Tool.Create` and inspect its `Definition.InputSchema` and `Definition.OutputSchema`. This is local reflection over a C# signature, not a prompt asking a model to write JSON. The input schema is a closed JSON object: it names the arguments the caller can supply, their types, and which are required. The output schema describes a supported typed return, such as a Boolean or record. The two schemas answer different questions; OneLLM sends the input schema to the model, not the return schema shown here.

## How do C# parameters become a tool’s JSON Schema?

OneLLM uses the parameter’s C# name as the JSON property name. A default makes the argument optional. A nullable parameter without a default is still required: the caller must send its property, but the value can be JSON `null`. `CancellationToken` and `ToolContext` are different: the runtime supplies them when it invokes the tool, so they don’t belong in the JSON at all. The full example declares `cancellationToken` but shows only `sku` and `quantity` under `properties`.

## Can you add descriptions and validation rules to generated JSON Schema?

For this reflection path, add `[Description]` to a method or parameter. The method attribute fills the tool’s description; the parameter attribute becomes a `description` inside the input schema. `[Range]` on a numeric parameter replaces the normal bounds for the C# type. Here the input `quantity` is constrained to `1` through `20`, while the output record’s `quantity` is a separate property with the usual `int` bounds. The annotation doesn’t follow the value from input to output. To constrain the output property, annotate it on the record itself. The [tool schema reference](https://recursiveai.org/onellm/docs/tool-schemas/) lists other supported annotations and explains how provider clients adapt schemas to the keywords their APIs support.

## Is a tool schema the same as a JSON Schema for a C# class?

No. A type schema, such as the one produced by the [.NET JSON schema exporter](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/extract-schema), describes a serialization contract for a type. A tool’s input schema describes a method call: which arguments have defaults, what to name them, and which parameters the runtime injects instead of accepting from JSON. The return can be a type schema but isn’t always one. A `string` return is a text result and has no `OutputSchema`; `Task<T>` and `ValueTask<T>` are unwrapped to inspect `T`.
