JsonSchemaGenerator

class JsonSchemaGenerator(json: Json, schemaFormat: JsonSchemaGenerator.SchemaFormat, maxDepth: Int)(source)

JSON schema generator from Kotlin serializable classes, to be used with LLM structured output functionality.

This generator creates JSON schemas that can be included in LLM prompts to encourage structured outputs that match your Kotlin data models. It supports:

  • Different schema formats (Simple or JsonSchema)

  • Automatic schema generation from Kotlin serializable classes

  • Custom property descriptions for better LLM understanding

  • Nested object structures with configurable depth limits

  • Polymorphic types via sealed classes

The generated schemas help LLMs understand the expected response format, improving parsing reliability and reducing the need for output coercion or error handling.

Constructors

Link copied to clipboard
constructor(json: Json, schemaFormat: JsonSchemaGenerator.SchemaFormat, maxDepth: Int)

Types

Functions

Link copied to clipboard
fun generate(id: String, serializer: KSerializer<*>, descriptions: Map<String, String> = emptyMap()): JsonObject

Generate a JSON schema for a serializable class.