spring-projects / spring-projects/spring-ai

Generate complex Json schemas with BeanOutputConverter

Open
#794 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted structured output
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Expected Behavior

Be able to generate more complex schemas with the BeanOutputConverter class, for instance with constrains definitions like max elements in array and max or min length for strings. This can be handled adding support to the definition of what modules should be loaded for jsonschema-generator library. Could be allowing override the private method generateSchema or adding it as a constructor parameter.

Current Behavior

Right now the BeanOutputConverter is adding just the jackson-module to the configuration of jsonschema-generator library, this is a great starting point to have a schema definition but does not allow the definition of more complex scenarios. The code to generate the value for jsonSchema field is private so we cannot override it to adding more modules to the libray.

Context

Under some circumstances we can ask to the AI model to generate content with specific rules, for instance if we want to generate specific number of answers for one question. In that case we can specify it with the prompt but probably could be a good idea add it as a constraint to the expected schema just to be sure the AI model respect it. This can be handled with the maven library jsonschema-module-jakarta-validation. I tested locally and just add it to the jsonschema-generator library configuration it's enough to add constraints to the generated schema.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the BeanOutputConverter class and its private generateSchema method, then inspect how jsonschema-generator is configured with the Jackson module. Determine how additional schema modules such as jsonschema-module-jakarta-validation can be supplied, and verify that constraints like array bounds and string lengths appear in the generated schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.