Skip to content

Commit 72a4952

Browse files
Merge pull request #189 from mbellade/json-schema-format
Try to enforce correct response format with `ChappieEnvelope` JSON Schema
2 parents a2aef91 + 47ad260 commit 72a4952

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package io.quarkiverse.chappie.runtime.dev;
22

3-
public record ChappieEnvelope<T>(String niceName, T answer) {
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public record ChappieEnvelope<T>(@JsonProperty(required = true) String niceName, @JsonProperty(required = true) T answer) {
46
}

runtime-dev/src/main/java/io/quarkiverse/chappie/runtime/dev/JsonObjectCreator.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkiverse.chappie.runtime.dev;
22

3+
import static com.github.victools.jsonschema.module.jackson.JacksonOption.RESPECT_JSONPROPERTY_REQUIRED;
4+
35
import java.lang.reflect.Type;
46
import java.nio.file.Path;
57
import java.util.List;
@@ -128,7 +130,7 @@ public static String toJsonString(Map<String, Object> data) {
128130
* TODO: We can cache this
129131
*/
130132
private static String buildResponsePrompt(Type answerType) {
131-
JsonNode jsonSchema = SCHEMA_GENERATOR.generateSchema(answerType);
133+
JsonNode jsonSchema = SCHEMA_GENERATOR.generateSchema(ChappieEnvelope.class, answerType);
132134

133135
return """
134136
RESPONSE FORMAT (strict):
@@ -137,16 +139,18 @@ RESPONSE FORMAT (strict):
137139
- Only use the contents in the [USER PROMPT]
138140
- Constraints: ≤ 60 chars, 4–9 words, Title Case, no quotes/backticks, no trailing punctuation.
139141
- Make it specific (action + topic), e.g., "Fix WebSocket Reconnect in Quarkus".
140-
- "answer": Must match and validates against the following JSON Schema (Draft 2020-12). Do not include extra fields.
141-
- JSON Scheme:
142-
%s
142+
- "answer": answer to the user request
143+
144+
The response must match and validate against the following JSON Schema (Draft 2020-12). Do not include extra fields.
145+
- JSON Schema:
146+
%s
143147
"""
144148
.formatted(jsonSchema.toString());
145149
}
146150

147151
private static final SchemaGenerator SCHEMA_GENERATOR = new SchemaGenerator(
148152
new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12)
149-
.with(new JacksonModule())
153+
.with(new JacksonModule(RESPECT_JSONPROPERTY_REQUIRED))
150154
.without(Option.FIELDS_DERIVED_FROM_ARGUMENTFREE_METHODS)
151155
.without(Option.GETTER_METHODS)
152156
.without(Option.NONSTATIC_NONVOID_NONGETTER_METHODS)

0 commit comments

Comments
 (0)