@@ -343,21 +343,23 @@ is a feature that ensures that the model will always generate responses that adh
343
343
A JSON schema can be defined by creating a
344
344
[ ` ResponseFormatJsonSchema ` ] ( openai-java-core/src/main/kotlin/com/openai/models/ResponseFormatJsonSchema.kt )
345
345
and setting it on the input parameters. However, for greater convenience, a JSON schema can instead
346
- be derived automatically from the structure of an arbitrary Java class. The response will then
347
- automatically convert the generated JSON content to an instance of that Java class.
346
+ be derived automatically from the structure of an arbitrary Java class. The JSON content from the
347
+ response will then be converted automatically to an instance of that Java class. A full, working
348
+ example of the use of Structured Outputs with arbitrary Java classes can be seen in
349
+ [ ` StructuredOutputsClassExample ` ] ( openai-java-example/src/main/java/com/openai/example/StructuredOutputsClassExample.java ) .
348
350
349
351
Java classes can contain fields declared to be instances of other classes and can use collections:
350
352
351
353
``` java
352
354
class Person {
353
355
public String name;
354
- public int yearOfBirth ;
356
+ public int birthYear ;
355
357
}
356
358
357
359
class Book {
358
360
public String title;
359
361
public Person author;
360
- public int yearPublished ;
362
+ public int publicationYear ;
361
363
}
362
364
363
365
class BookList {
@@ -375,7 +377,7 @@ import com.openai.models.chat.completions.ChatCompletionCreateParams;
375
377
import com.openai.models.chat.completions.StructuredChatCompletionCreateParams ;
376
378
377
379
StructuredChatCompletionCreateParams<BookList > params = ChatCompletionCreateParams . builder()
378
- .addUserMessage(" List six famous nineteenth century novels." )
380
+ .addUserMessage(" List some famous late twentieth century novels." )
379
381
.model(ChatModel . GPT_4_1 )
380
382
.responseFormat(BookList . class)
381
383
.build();
@@ -403,11 +405,25 @@ import java.util.Optional;
403
405
class Book {
404
406
public String title;
405
407
public Person author;
406
- public int yearPublished ;
408
+ public int publicationYear ;
407
409
public Optional<String > isbn;
408
410
}
409
411
```
410
412
413
+ Generic type information for fields is retained in the class's metadata, but _ generic type erasure_
414
+ applies in other scopes. While, for example, a JSON schema defining an array of strings can be
415
+ derived from the ` BoolList.books ` field with type ` List<String> ` , a valid JSON schema cannot be
416
+ derived from a local variable of that same type, so the following will _ not_ work:
417
+
418
+ ``` java
419
+ List<String > books = new ArrayList<> ();
420
+
421
+ StructuredChatCompletionCreateParams<BookList > params = ChatCompletionCreateParams . builder()
422
+ .responseFormat(books. class)
423
+ // ...
424
+ .build();
425
+ ```
426
+
411
427
If an error occurs while converting a JSON response to an instance of a Java class, the error
412
428
message will include the JSON response to assist in diagnosis. For instance, if the response is
413
429
truncated, the JSON data will be incomplete and cannot be converted to a class instance. If your
@@ -435,20 +451,23 @@ well.
435
451
schema in the request.
436
452
- ** Version Compatibility** : There may be instances where local validation fails while remote
437
453
validation succeeds. This can occur if the SDK version is outdated compared to the restrictions
438
- enforced by the remote model.
454
+ enforced by the remote AI model.
439
455
- ** Disabling Local Validation** : If you encounter compatibility issues and wish to bypass local
440
- validation, you can disable it by passing ` false ` to the ` responseFormat(Class<T>, boolean) ` method
441
- when building the parameters. (The default value for this parameter is ` true ` .)
456
+ validation, you can disable it by passing
457
+ [ ` JsonSchemaLocalValidation.NO ` ] ( openai-java-core/src/main/kotlin/com/openai/core/JsonSchemaLocalValidation.kt )
458
+ to the ` responseFormat(Class<T>, JsonSchemaLocalValidation) ` method when building the parameters.
459
+ (The default value for this parameter is ` JsonSchemaLocalValidation.YES ` .)
442
460
443
461
``` java
462
+ import com.openai.core.JsonSchemaLocalValidation ;
444
463
import com.openai.models.ChatModel ;
445
464
import com.openai.models.chat.completions.ChatCompletionCreateParams ;
446
465
import com.openai.models.chat.completions.StructuredChatCompletionCreateParams ;
447
466
448
467
StructuredChatCompletionCreateParams<BookList > params = ChatCompletionCreateParams . builder()
449
- .addUserMessage(" List six famous nineteenth century novels." )
468
+ .addUserMessage(" List some famous late twentieth century novels." )
450
469
.model(ChatModel . GPT_4_1 )
451
- .responseFormat(BookList . class, false ) // Disable local validation.
470
+ .responseFormat(BookList . class, JsonSchemaLocalValidation . NO )
452
471
.build();
453
472
```
454
473
@@ -470,14 +489,17 @@ import com.fasterxml.jackson.annotation.JsonPropertyDescription;
470
489
class Person {
471
490
@JsonPropertyDescription (" The first name and surname of the person" )
472
491
public String name;
473
- public int yearOfBirth;
492
+ public int birthYear;
493
+ @JsonPropertyDescription (" The year the person died, or 'present' if the person is living." )
494
+ public String deathYear;
474
495
}
475
496
476
497
@JsonClassDescription (" The details of one published book" )
477
498
class Book {
478
499
public String title;
479
500
public Person author;
480
- public int yearPublished;
501
+ @JsonPropertyDescription (" The year in which the book was first published." )
502
+ public int publicationYear;
481
503
@JsonIgnore public String genre;
482
504
}
483
505
0 commit comments