2
2
3
3
<!-- x-release-please-start-version -->
4
4
5
- [ ![ Maven Central] ( https://img.shields.io/maven-central/v/com.openai/openai-java )] ( https://central.sonatype.com/artifact/com.openai/openai-java/2.9.0 )
6
- [ ![ javadoc] ( https://javadoc.io/badge2/com.openai/openai-java/2.9.0 /javadoc.svg )] ( https://javadoc.io/doc/com.openai/openai-java/2.9.0 )
5
+ [ ![ Maven Central] ( https://img.shields.io/maven-central/v/com.openai/openai-java )] ( https://central.sonatype.com/artifact/com.openai/openai-java/2.9.1 )
6
+ [ ![ javadoc] ( https://javadoc.io/badge2/com.openai/openai-java/2.9.1 /javadoc.svg )] ( https://javadoc.io/doc/com.openai/openai-java/2.9.1 )
7
7
8
8
<!-- x-release-please-end -->
9
9
10
10
The OpenAI Java SDK provides convenient access to the [ OpenAI REST API] ( https://platform.openai.com/docs ) from applications written in Java.
11
11
12
12
<!-- x-release-please-start-version -->
13
13
14
- The REST API documentation can be found on [ platform.openai.com] ( https://platform.openai.com/docs ) . Javadocs are available on [ javadoc.io] ( https://javadoc.io/doc/com.openai/openai-java/2.9.0 ) .
14
+ The REST API documentation can be found on [ platform.openai.com] ( https://platform.openai.com/docs ) . Javadocs are available on [ javadoc.io] ( https://javadoc.io/doc/com.openai/openai-java/2.9.1 ) .
15
15
16
16
<!-- x-release-please-end -->
17
17
@@ -22,7 +22,7 @@ The REST API documentation can be found on [platform.openai.com](https://platfor
22
22
### Gradle
23
23
24
24
``` kotlin
25
- implementation(" com.openai:openai-java:2.9.0 " )
25
+ implementation(" com.openai:openai-java:2.9.1 " )
26
26
```
27
27
28
28
### Maven
@@ -31,7 +31,7 @@ implementation("com.openai:openai-java:2.9.0")
31
31
<dependency >
32
32
<groupId >com.openai</groupId >
33
33
<artifactId >openai-java</artifactId >
34
- <version >2.9.0 </version >
34
+ <version >2.9.1 </version >
35
35
</dependency >
36
36
```
37
37
@@ -363,7 +363,8 @@ response will then be converted automatically to an instance of that Java class.
363
363
example of the use of Structured Outputs with arbitrary Java classes can be seen in
364
364
[ ` StructuredOutputsExample ` ] ( openai-java-example/src/main/java/com/openai/example/StructuredOutputsExample.java ) .
365
365
366
- Java classes can contain fields declared to be instances of other classes and can use collections:
366
+ Java classes can contain fields declared to be instances of other classes and can use collections
367
+ (see [ Defining JSON schema properties] ( #defining-json-schema-properties ) for more details):
367
368
368
369
``` java
369
370
class Person {
@@ -506,12 +507,38 @@ the latter when `ResponseCreateParams.Builder.text(Class<T>)` is called.
506
507
For a full example of the usage of _ Structured Outputs_ with the Responses API, see
507
508
[ ` ResponsesStructuredOutputsExample ` ] ( openai-java-example/src/main/java/com/openai/example/ResponsesStructuredOutputsExample.java ) .
508
509
510
+ ### Defining JSON schema properties
511
+
512
+ When a JSON schema is derived from your Java classes, all properties represented by ` public ` fields
513
+ or ` public ` getter methods are included in the schema by default. Non-` public ` fields and getter
514
+ methods are _ not_ included by default. You can exclude ` public ` , or include non-` public ` fields or
515
+ getter methods, by using the ` @JsonIgnore ` or ` @JsonProperty ` annotations respectively (see
516
+ [ Annotating classes and JSON schemas] ( #annotating-classes-and-json-schemas ) for details).
517
+
518
+ If you do not want to define ` public ` fields, you can define ` private ` fields and corresponding
519
+ ` public ` getter methods. For example, a ` private ` field ` myValue ` with a ` public ` getter method
520
+ ` getMyValue() ` will result in a ` "myValue" ` property being included in the JSON schema. If you
521
+ prefer not to use the conventional Java "get" prefix for the name of the getter method, then you
522
+ _ must_ annotate the getter method with the ` @JsonProperty ` annotation and the full method name will
523
+ be used as the property name. You do not have to define any corresponding setter methods if you do
524
+ not need them.
525
+
526
+ Each of your classes _ must_ define at least one property to be included in the JSON schema. A
527
+ validation error will occur if any class contains no fields or getter methods from which schema
528
+ properties can be derived. This may occur if, for example:
529
+
530
+ - There are no fields or getter methods in the class.
531
+ - All fields and getter methods are ` public ` , but all are annotated with ` @JsonIgnore ` .
532
+ - All fields and getter methods are non-` public ` , but none are annotated with ` @JsonProperty ` .
533
+ - A field or getter method is declared with a ` Map ` type. A ` Map ` is treated like a separate class
534
+ with no named properties, so it will result in an empty ` "properties" ` field in the JSON schema.
535
+
509
536
### Annotating classes and JSON schemas
510
537
511
538
You can use annotations to add further information to the JSON schema derived from your Java
512
- classes, or to exclude individual fields from the schema. Details from annotations captured in the
513
- JSON schema may be used by the AI model to improve its response. The SDK supports the use of
514
- [ Jackson Databind] ( https://github.com/FasterXML/jackson-databind ) annotations.
539
+ classes, or to control which fields or getter methods will be included in the schema. Details from
540
+ annotations captured in the JSON schema may be used by the AI model to improve its response. The SDK
541
+ supports the use of [ Jackson Databind] ( https://github.com/FasterXML/jackson-databind ) annotations.
515
542
516
543
``` java
517
544
import com.fasterxml.jackson.annotation.JsonClassDescription ;
@@ -541,8 +568,12 @@ class BookList {
541
568
```
542
569
543
570
- Use ` @JsonClassDescription ` to add a detailed description to a class.
544
- - Use ` @JsonPropertyDescription ` to add a detailed description to a field of a class.
545
- - Use ` @JsonIgnore ` to omit a field of a class from the generated JSON schema.
571
+ - Use ` @JsonPropertyDescription ` to add a detailed description to a field or getter method of a
572
+ class.
573
+ - Use ` @JsonIgnore ` to exclude a ` public ` field or getter method of a class from the generated JSON
574
+ schema.
575
+ - Use ` @JsonProperty ` to include a non-` public ` field or getter method of a class in the generated
576
+ JSON schema.
546
577
547
578
If you use ` @JsonProperty(required = false) ` , the ` false ` value will be ignored. OpenAI JSON schemas
548
579
must mark all properties as _ required_ , so the schema generated from your Java classes will respect
@@ -577,9 +608,11 @@ _Function Calling_ with Java classes to define function parameters can be seen i
577
608
[ ` FunctionCallingExample ` ] ( openai-java-example/src/main/java/com/openai/example/FunctionCallingExample.java ) .
578
609
579
610
Like for [ Structured Outputs] ( #structured-outputs-with-json-schemas ) , Java classes can contain
580
- fields declared to be instances of other classes and can use collections. Optionally, annotations
581
- can be used to set the descriptions of the function (class) and its parameters (fields) to assist
582
- the AI model in understanding the purpose of the function and the possible values of its parameters.
611
+ fields declared to be instances of other classes and can use collections (see
612
+ [ Defining JSON schema properties] ( #defining-json-schema-properties ) for more details). Optionally,
613
+ annotations can be used to set the descriptions of the function (class) and its parameters (fields)
614
+ to assist the AI model in understanding the purpose of the function and the possible values of its
615
+ parameters.
583
616
584
617
``` java
585
618
import com.fasterxml.jackson.annotation.JsonClassDescription ;
@@ -724,24 +757,31 @@ validation and under what circumstances you might want to disable it.
724
757
### Annotating function classes
725
758
726
759
You can use annotations to add further information about functions to the JSON schemas that are
727
- derived from your function classes, or to exclude individual fields from the parameters to the
728
- function. Details from annotations captured in the JSON schema may be used by the AI model to
729
- improve its response. The SDK supports the use of
760
+ derived from your function classes, or to control which fields or getter methods will be used as
761
+ parameters to the function. Details from annotations captured in the JSON schema may be used by the
762
+ AI model to improve its response. The SDK supports the use of
730
763
[ Jackson Databind] ( https://github.com/FasterXML/jackson-databind ) annotations.
731
764
732
765
- Use ` @JsonClassDescription ` to add a description to a function class detailing when and how to use
733
766
that function.
734
767
- Use ` @JsonTypeName ` to set the function name to something other than the simple name of the class,
735
768
which is used by default.
736
- - Use ` @JsonPropertyDescription ` to add a detailed description to function parameter (a field of
737
- a function class).
738
- - Use ` @JsonIgnore ` to omit a field of a class from the generated JSON schema for a function's
739
- parameters.
769
+ - Use ` @JsonPropertyDescription ` to add a detailed description to function parameter (a field or
770
+ getter method of a function class).
771
+ - Use ` @JsonIgnore ` to exclude a ` public ` field or getter method of a class from the generated JSON
772
+ schema for a function's parameters.
773
+ - Use ` @JsonProperty ` to include a non-` public ` field or getter method of a class in the generated
774
+ JSON schema for a function's parameters.
740
775
741
776
OpenAI provides some
742
777
[ Best practices for defining functions] ( https://platform.openai.com/docs/guides/function-calling#best-practices-for-defining-functions )
743
778
that may help you to understand how to use the above annotations effectively for your functions.
744
779
780
+ See also [ Defining JSON schema properties] ( #defining-json-schema-properties ) for more details on how
781
+ to use fields and getter methods and combine access modifiers and annotations to define the
782
+ parameters of your functions. The same rules apply to function classes and to the structured output
783
+ classes described in that section.
784
+
745
785
## File uploads
746
786
747
787
The SDK defines methods that accept files.
0 commit comments