Repository navigation
Replies: 2 comments
|
@cberes If you only target one model/provider that supports native structured output, setting |
|
Yes, you can pass a dynamically generated JSON Schema directly to Spring AI and you don't need to subclass Spring AI exposes the schema directly through String jsonSchema = objectMapper.writeValueAsString(schema);
var options = StructuredOutputChatOptions.builder()
.outputSchema(jsonSchema)
.build();
var response = chatClient.prompt()
.options(options)
.user(prompt)
.call()
.content();
JsonNode result = objectMapper.readTree(response);Provider-specific options can also expose the same The For portability, |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm currently using OpenAI's API with a json_schema response format. My understanding is that Spring AI's native structured outputs is the equivalent, so when I switch to Spring AI I'd like to use native structured outputs. However I make the schema dynamically. Is that something I can do with Spring AI?
The schema looks something like this, but as I said it's dynamic. The fields in AiResponse may be different each time.
ValueTypeis one of the 4 subtypes below, except we don't actually have aMyEnum; we build the schema'senumproperty dynamically.I found a way to do this where I build the schema by hand using Jackson. And also I use Jackson's readTree to get a JsonNode, then extract the fields manually. Below is an example.
My understanding is that I have to use a BeanOutputConverter if I want to use native structured outputs. Is that correct?
Is there a better or simpler way to do this? I worry that this won't be portable to other LLMs if I have to build the schema by hand.
All reactions