kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Generate JSON schema from serializable type

Open rnett opened this issue 5 years ago • 7 comments

What is your use-case and why do you need this feature?

I'd like a way to generate a JSON schema from a (serializable) type. It would likely share an implementation with #34 and more general serializer traversal.

The biggest use case, imo, is for documentation, in particular of api endpoints that receive JSON data, especially for something like OpenAPI/Swagger. There's also tools out there (like json-schema-to-typescript that take JSON schema as input to generate data classes for other languages.

Describe the solution you'd like

Ideally, I'd like some kind of generic serializer traversal in serialization with format specific uses like this or #34 in their respective format artifacts,

rnett avatar Oct 04 '20 23:10 rnett

Schema builders are neither the goal for 1.0 neither for particular future release. However, we provide (experimental) functionality for introspecting serializable classes that can be used to build the schema. Using this data, a json schema can be easily built. Introspection mechanism is available via SerialDescriptor. You can find an example of usage in tests: https://github.com/Kotlin/kotlinx.serialization/blob/master/core/commonTest/src/kotlinx/serialization/SerialDescriptorSpecificationTest.kt#L39

sandwwraith avatar Oct 06 '20 19:10 sandwwraith

FYI, there is https://github.com/Ricky12Awesome/json-schema-serialization which might become handy.

sschuberth avatar Oct 29 '21 16:10 sschuberth

See the PR I just created for Spring Doc Open API: https://github.com/springdoc/springdoc-openapi/pull/1514

This will integrate with a Spring server using swagger for generation of an Open API schema.

werner77 avatar Feb 20 '22 15:02 werner77

I've written a library that will generate TypeScript from Kotlinx Serialization classes.

https://github.com/adamko-dev/kotlinx-serialization-typescript-generator

I think the generated TypeScript could be used to generate a JsonSchema using another library https://github.com/YousefED/typescript-json-schema https://github.com/vega/ts-json-schema-generator

aSemy avatar Oct 15 '22 10:10 aSemy

I believe this feature is more useful than ever thanks to AI APIs. Imagine you’re building an app that leverages generative AI and need to invoke the API. To ensure consistent JSON responses, you must supply the API with a JSON schema. Currently, we rely on https://github.com/Ricky12Awesome/json-schema-serialization, but since it’s hosted on JCenter, it is now read-only. This means it cannot be updated, and there is also a risk that the server could become unavailable in the future. (CC: @sandwwraith)

takahirom avatar Apr 28 '25 08:04 takahirom

@takahirom, you might want to give https://github.com/SMILEY4/schema-kenerator a try.

sschuberth avatar Apr 30 '25 09:04 sschuberth

https://github.com/Kotlin/kotlinx-schema/

This library solves three key challenges:

  • LLM Function Calling Integration: Generate OpenAI/Anthropic-compatible function schemas directly from Kotlin functions with proper type definitions and descriptions
  • Third-Party Class Support: Create schemas for library classes without modifying their source code (Spring entities, Ktor models, etc.)
  • Multi-Framework Compatibility: Works with existing annotations from Jackson, LangChain4j, Koog, and more — no code changes needed

When to Use

  • Building LLM-powered applications with structured function calling (OpenAI, Anthropic, Claude, MCP)
  • Need schemas for third-party library classes you cannot modify
  • Already using @Description-like annotations from other frameworks
  • Want zero runtime overhead with compile-time generation (Multiplatform!!!)
  • Need dynamic schema generation at runtime via reflection (JVM)

kpavlov avatar Nov 27 '25 14:11 kpavlov