freshcrate
Skin:/
Home > Developer Tools > xemantic-ai-tool-schema

xemantic-ai-tool-schema

AI/LLM tool use (function calling) JSON Schema generator - a Kotlin multiplatform library

Why this rank:Strong adoptionHealthy release cadenceRelease freshness

Description

AI/LLM tool use (function calling) JSON Schema generator - a Kotlin multiplatform library

README

xemantic-ai-tool-schema

AI/LLM tool use (function calling) JSON Schema generator - a Kotlin multiplatform library which generates JSON Schema for Kotlin @Serializable classes.

Maven Central Version GitHub Release Date license

GitHub Actions Workflow Status GitHub branch check runs GitHub commits since latest release GitHub last commit

GitHub contributors GitHub commit activity GitHub code size in bytes GitHub Created At kotlin version discord users online Bluesky

Why?

This library was created to fulfill the need of agentic AI projects created by xemantic. In particular:

These projects are heavily dependent on tool use (function calling) functionality provided by many Large Language Models. Thanks to xemantic-ai-tool-schema, a Kotlin class, with possible additional constraints, can be automatically instantiated from the JSON tool use input provided by the LLM. This way any manual steps of defining JSON schema for the model are avoided, which reduce a chance for errors in the process, and allows to rapidly develop even complex data structures passed to an AI agent.

In short the xemantic-ai-tool-schema library can generate a JSON Schema from any Kotlin class marked as @Serializable, according to kotlinx.serialization.

Tip

You might be familiar with similar functionality of the Pydantic Python library, however, the standard Kotlin serialization is already fulfilling model metadata provisioning, so this analogy might be misleading.

Usage

In build.gradle.kts add:

plugins {
    kotlin("multiplatform") version "2.2.20" // (or jvm for jvm-only project)
    kotlin("plugin.serialization") version "2.2.20"
}

// ...
dependencies {
    implementation("com.xemantic.ai:xemantic-ai-tool-schema:1.2.0")
}

Then in your code you can define entities like this:

@Serializable
@SerialName("address")
@Title("The full address")
@Description("An address of a person or an organization")
data class Address(
    val street: String,
    val city: String,
    @Description("A postal code not limited to particular country")
    @MinLength(3)
    @MaxLength(10)
    val postalCode: String,
    @Pattern("[a-z]{2}")
    val countryCode: String,
    @Format(StringFormat.EMAIL)
    val email: String? = null,
    @OptIn(ExperimentalTime::class)
    val registeredAt: Instant,
    val status: AddressStatus
)

@Title("Address status")
@Description("The verification status of an address")
enum class AddressStatus {
    PENDING_VERIFICATION,
    VERIFIED,
    INVALID
}

And when jsonSchemaOf() function is invoked:

val schema = jsonSchemaOf<Address>()

It will produce a JsonSchema instance, which serializes to:

{
  "type": "object",
  "title": "The full address",
  "description": "An address of a person or an organization",
  "properties": {
    "street": {
      "type": "string"
    },
    "city": {
      "type": "string"
    },
    "postalCode": {
      "type": "string",
      "description": "A postal code not limited to particular country",
      "minLength": 3,
      "maxLength": 10
    },
    "countryCode": {
      "type": "string",
      "pattern": "[a-z]{2}"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "registeredAt": {
      "type": "string",
      "format": "date-time"
    },
    "status": {
      "type": "string",
      "title": "Address status",
      "description": "The verification status of an address",
      "enum": [
        "PENDING_VERIFICATION",
        "VERIFIED",
        "INVALID"
      ]
    }
  },
  "required": [
    "street",
    "city",
    "postalCode",
    "countryCode",
    "registeredAt",
    "status"
  ]
}

And this is the input accepted by Large Language Model APIs like OpenAI API and Anthropic API. When requesting a tool use, these LLMs will send a JSON payload adhering to this schema, therefore immediately deserializable as the original @Serializable Kotlin class.

More details and use cases in the JsonSchemaGeneratorTest.

Note

When calling toString() function on any instance of JsonSchema, it will also produce a pretty printed String representation of a valid JSON schema, which in turn describes the Kotlin class as a serialized JSON. This functionality is useful for testing and debugging.

Serializing Java BigDecimals

For JVM-only projects, it is possible to specify java.math.BigDecimal serialization. It will serialize decimal numbers to strings, and add description and pattern properties to generated JSON Schema of a BigDecimal property.

See JavaBigDecimalToSchemaTest for details.

Serializing BigDecimal/monetary values in multiplatform way

There is an interface called Money defined in the tests of this project. It explains how to define and serialize monetary amounts independently of the underlying decimal number and arithmetics provider.

See also xemantic-ai-money project for a ready solution packaged as a library.

Development

Clone this repo and then in the project dir:

./gradlew build

Non-recommended usage

Warning

Even though this library provides basic serializable representation of a JSON Schema, it is not meant to fully model general purpose JSON Schema. In particular, it should not be used for deserializing existing schemas from JSON.

Release History

VersionChangesUrgencyDate
v1.2.0## What's Changed * Update GitHub Action Versions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/35 * Initialize JetBrains Junie 🚀 by @jetbrains-junie[bot] in https://github.com/xemantic/xemantic-ai-tool-schema/pull/36 * Update GitHub Action Versions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/40 * Update GitHub Action Versions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/48 * Update dependencies to Kotlin 2.2.Low10/20/2025
v1.1.2**Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v1.1.1...v1.1.2Low4/13/2025
v1.1.1## What's Changed * Update GitHub Action Versions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/24 * Update: roll to java 8 jvm target by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/25 **Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v1.1.0...v1.1.1Low4/13/2025
v1.1.0## What's Changed * Add more built in fallback formats and support inlining refs by @morki in https://github.com/xemantic/xemantic-ai-tool-schema/pull/17 * Support sealed hierarchies by @morki in https://github.com/xemantic/xemantic-ai-tool-schema/pull/18 ## New Contributors * @morki made their first contribution in https://github.com/xemantic/xemantic-ai-tool-schema/pull/17 **Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v1.0.0...v1.1.0Low3/13/2025
v1.0.0## What's Changed * BigDecimalSerializer name change. Final changes before releasing version 1.0.0 by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/15 **Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.2.2...v1.0.0Low2/19/2025
v0.2.2## What's Changed * Update GitHub Action Versions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/13 * new ObjectSchema.copy() allowing to modify properties by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/14 **Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.2.1...v0.2.2Low2/16/2025
v0.2.1**Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.2.0...v0.2.1Low2/1/2025
v0.2.0## What's Changed * All the tests updated to the latest conventions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/2 * Update GitHub Action Versions by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/5 * Update/kotlin code style by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/6 * copyright update by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/7 * Update GitHub Action Versions by @morisil in https:Low2/1/2025
v0.1.4**Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.1.3...v0.1.4Low12/9/2024
v0.1.3**Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.1.2...v0.1.3Low12/9/2024
v0.1.2**Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.1.1...v0.1.2Low12/9/2024
v0.1.1## What's Changed * add suppressDescription flag to JsonSchemaGenerator by @morisil in https://github.com/xemantic/xemantic-ai-tool-schema/pull/1 ## New Contributors * @morisil made their first contribution in https://github.com/xemantic/xemantic-ai-tool-schema/pull/1 **Full Changelog**: https://github.com/xemantic/xemantic-ai-tool-schema/compare/v0.1...v0.1.1Low12/2/2024
v0.1Release v0.1Low12/1/2024

Dependencies & License Audit

Loading dependencies...

Similar Packages

adk-pythonAn open-source, code-first Python toolkit for building, evaluating, and deploying sophisticated AI agents with flexibility and control.v2.2.0
wanakuWanaku MCP Routerv0.1.3
golemGolem Cloud is the agent-native platform for building AI agents and distributed applications that never lose state, never duplicate work, and never require you to build infrastructure.v1.5.4
samplesAgent samples built using the Strands Agents SDK.main@2026-06-04
Autonomous-AgentsAutonomous Agents (LLMs) research papers. Updated Daily.main@2026-06-03

More in Developer Tools

mypyOptional static typing for Python
pipThe PyPA recommended tool for installing Python packages.
anthropicThe official Python library for the anthropic API
openinference-instrumentationOpenInference instrumentation utilities