Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.40.1"
".": "0.41.0"
}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 0.41.0 (2025-03-31)

Full Changelog: [v0.40.1...v0.41.0](https://github.com/openai/openai-java/compare/v0.40.1...v0.41.0)

### Features

* **client:** improve some method names ([#379](https://github.com/openai/openai-java/issues/379)) ([895f99a](https://github.com/openai/openai-java/commit/895f99acaee6f8e38165c72da1de18e3d7682321))


### Bug Fixes

* **client:** limit json deserialization coercion ([#377](https://github.com/openai/openai-java/issues/377)) ([5234cf9](https://github.com/openai/openai-java/commit/5234cf94a540191ae051e3123b7d1be088655100))

## 0.40.1 (2025-03-28)

Full Changelog: [v0.40.0...v0.40.1](https://github.com/openai/openai-java/compare/v0.40.0...v0.40.1)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

<!-- x-release-please-start-version -->

[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/0.40.1)
[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/0.40.1/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/0.40.1)
[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/0.41.0)
[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/0.41.0/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/0.41.0)

<!-- x-release-please-end -->

The OpenAI Java SDK provides convenient access to the [OpenAI REST API](https://platform.openai.com/docs) from applications written in Java.

<!-- x-release-please-start-version -->

The REST API documentation can be found on [platform.openai.com](https://platform.openai.com/docs). Javadocs are also available on [javadoc.io](https://javadoc.io/doc/com.openai/openai-java/0.40.1).
The REST API documentation can be found on [platform.openai.com](https://platform.openai.com/docs). Javadocs are also available on [javadoc.io](https://javadoc.io/doc/com.openai/openai-java/0.41.0).

<!-- x-release-please-end -->

Expand All @@ -29,7 +29,7 @@ The REST API documentation can be found on [platform.openai.com](https://platfor
### Gradle

```kotlin
implementation("com.openai:openai-java:0.40.1")
implementation("com.openai:openai-java:0.41.0")
```

### Maven
Expand All @@ -38,7 +38,7 @@ implementation("com.openai:openai-java:0.40.1")
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>0.40.1</version>
<version>0.41.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {

allprojects {
group = "com.openai"
version = "0.40.1" // x-release-please-version
version = "0.41.0" // x-release-please-version
}

subprojects {
Expand Down
1 change: 1 addition & 0 deletions openai-java-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
testImplementation("org.assertj:assertj-core:3.25.3")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.3")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.3")
testImplementation("org.junit-pioneer:junit-pioneer:1.9.1")
testImplementation("org.mockito:mockito-core:5.14.2")
testImplementation("org.mockito:mockito-junit-jupiter:5.14.2")
testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.cfg.CoercionAction
import com.fasterxml.jackson.databind.cfg.CoercionInputShape
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.type.LogicalType
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.kotlinModule
Expand All @@ -21,6 +24,60 @@ fun jsonMapper(): JsonMapper =
.addModule(Jdk8Module())
.addModule(JavaTimeModule())
.addModule(SimpleModule().addSerializer(InputStreamJsonSerializer))
.withCoercionConfig(LogicalType.Boolean) {
it.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Array, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.Integer) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Array, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.Float) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Array, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.Textual) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Array, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.Array) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.Collection) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.Map) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail)
}
.withCoercionConfig(LogicalType.POJO) {
it.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Array, CoercionAction.Fail)
}
.serializationInclusion(JsonInclude.Include.NON_ABSENT)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,8 @@ private constructor(
this.responseFormat = responseFormat
}

/**
* Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() = responseFormat(AssistantResponseFormatOption.ofJsonValue())
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = responseFormat(AssistantResponseFormatOption.ofAuto())

/**
* Alias for calling [responseFormat] with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,8 @@ private constructor(
body.responseFormat(responseFormat)
}

/**
* Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() = apply { body.responseFormatJsonValue() }
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = apply { body.responseFormatAuto() }

/**
* Alias for calling [responseFormat] with
Expand Down Expand Up @@ -1247,12 +1245,8 @@ private constructor(
this.responseFormat = responseFormat
}

/**
* Alias for calling [responseFormat] with
* `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() =
responseFormat(AssistantResponseFormatOption.ofJsonValue())
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = responseFormat(AssistantResponseFormatOption.ofAuto())

/**
* Alias for calling [responseFormat] with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,8 @@ private constructor(
body.responseFormat(responseFormat)
}

/**
* Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() = apply { body.responseFormatJsonValue() }
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = apply { body.responseFormatAuto() }

/**
* Alias for calling [responseFormat] with
Expand Down Expand Up @@ -1244,12 +1242,8 @@ private constructor(
this.responseFormat = responseFormat
}

/**
* Alias for calling [responseFormat] with
* `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() =
responseFormat(AssistantResponseFormatOption.ofJsonValue())
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = responseFormat(AssistantResponseFormatOption.ofAuto())

/**
* Alias for calling [responseFormat] with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ import java.util.Optional
@JsonSerialize(using = AssistantResponseFormatOption.Serializer::class)
class AssistantResponseFormatOption
private constructor(
private val jsonValue: JsonValue? = null,
private val auto: JsonValue? = null,
private val responseFormatText: ResponseFormatText? = null,
private val responseFormatJsonObject: ResponseFormatJsonObject? = null,
private val responseFormatJsonSchema: ResponseFormatJsonSchema? = null,
private val _json: JsonValue? = null,
) {

/** `auto` is the default value */
fun jsonValue(): Optional<JsonValue> = Optional.ofNullable(jsonValue)
fun auto(): Optional<JsonValue> = Optional.ofNullable(auto)

/** Default response format. Used to generate text responses. */
fun responseFormatText(): Optional<ResponseFormatText> = Optional.ofNullable(responseFormatText)
Expand All @@ -72,7 +72,7 @@ private constructor(
fun responseFormatJsonSchema(): Optional<ResponseFormatJsonSchema> =
Optional.ofNullable(responseFormatJsonSchema)

fun isJsonValue(): Boolean = jsonValue != null
fun isAuto(): Boolean = auto != null

fun isResponseFormatText(): Boolean = responseFormatText != null

Expand All @@ -81,7 +81,7 @@ private constructor(
fun isResponseFormatJsonSchema(): Boolean = responseFormatJsonSchema != null

/** `auto` is the default value */
fun asJsonValue(): JsonValue = jsonValue.getOrThrow("jsonValue")
fun asAuto(): JsonValue = auto.getOrThrow("auto")

/** Default response format. Used to generate text responses. */
fun asResponseFormatText(): ResponseFormatText =
Expand All @@ -106,7 +106,7 @@ private constructor(

fun <T> accept(visitor: Visitor<T>): T {
return when {
jsonValue != null -> visitor.visitJsonValue(jsonValue)
auto != null -> visitor.visitAuto(auto)
responseFormatText != null -> visitor.visitResponseFormatText(responseFormatText)
responseFormatJsonObject != null ->
visitor.visitResponseFormatJsonObject(responseFormatJsonObject)
Expand All @@ -125,10 +125,10 @@ private constructor(

accept(
object : Visitor<Unit> {
override fun visitJsonValue(jsonValue: JsonValue) {
jsonValue.let {
override fun visitAuto(auto: JsonValue) {
auto.let {
if (it != JsonValue.from("auto")) {
throw OpenAIInvalidDataException("'jsonValue' is invalid, received $it")
throw OpenAIInvalidDataException("'auto' is invalid, received $it")
}
}
}
Expand Down Expand Up @@ -158,14 +158,14 @@ private constructor(
return true
}

return /* spotless:off */ other is AssistantResponseFormatOption && jsonValue == other.jsonValue && responseFormatText == other.responseFormatText && responseFormatJsonObject == other.responseFormatJsonObject && responseFormatJsonSchema == other.responseFormatJsonSchema /* spotless:on */
return /* spotless:off */ other is AssistantResponseFormatOption && auto == other.auto && responseFormatText == other.responseFormatText && responseFormatJsonObject == other.responseFormatJsonObject && responseFormatJsonSchema == other.responseFormatJsonSchema /* spotless:on */
}

override fun hashCode(): Int = /* spotless:off */ Objects.hash(jsonValue, responseFormatText, responseFormatJsonObject, responseFormatJsonSchema) /* spotless:on */
override fun hashCode(): Int = /* spotless:off */ Objects.hash(auto, responseFormatText, responseFormatJsonObject, responseFormatJsonSchema) /* spotless:on */

override fun toString(): String =
when {
jsonValue != null -> "AssistantResponseFormatOption{jsonValue=$jsonValue}"
auto != null -> "AssistantResponseFormatOption{auto=$auto}"
responseFormatText != null ->
"AssistantResponseFormatOption{responseFormatText=$responseFormatText}"
responseFormatJsonObject != null ->
Expand All @@ -179,8 +179,7 @@ private constructor(
companion object {

/** `auto` is the default value */
@JvmStatic
fun ofJsonValue() = AssistantResponseFormatOption(jsonValue = JsonValue.from("auto"))
@JvmStatic fun ofAuto() = AssistantResponseFormatOption(auto = JsonValue.from("auto"))

/** Default response format. Used to generate text responses. */
@JvmStatic
Expand Down Expand Up @@ -212,7 +211,7 @@ private constructor(
interface Visitor<out T> {

/** `auto` is the default value */
fun visitJsonValue(jsonValue: JsonValue): T
fun visitAuto(auto: JsonValue): T

/** Default response format. Used to generate text responses. */
fun visitResponseFormatText(responseFormatText: ResponseFormatText): T
Expand Down Expand Up @@ -254,12 +253,12 @@ private constructor(
tryDeserialize(node, jacksonTypeRef<JsonValue>()) {
it.let {
if (it != JsonValue.from("auto")) {
throw OpenAIInvalidDataException("'jsonValue' is invalid, received $it")
throw OpenAIInvalidDataException("'auto' is invalid, received $it")
}
}
}
?.let {
return AssistantResponseFormatOption(jsonValue = it, _json = json)
return AssistantResponseFormatOption(auto = it, _json = json)
}
tryDeserialize(node, jacksonTypeRef<ResponseFormatText>()) { it.validate() }
?.let {
Expand Down Expand Up @@ -293,7 +292,7 @@ private constructor(
provider: SerializerProvider,
) {
when {
value.jsonValue != null -> generator.writeObject(value.jsonValue)
value.auto != null -> generator.writeObject(value.auto)
value.responseFormatText != null -> generator.writeObject(value.responseFormatText)
value.responseFormatJsonObject != null ->
generator.writeObject(value.responseFormatJsonObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,8 @@ private constructor(
body.responseFormat(responseFormat)
}

/**
* Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() = apply { body.responseFormatJsonValue() }
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = apply { body.responseFormatAuto() }

/**
* Alias for calling [responseFormat] with
Expand Down Expand Up @@ -1599,12 +1597,8 @@ private constructor(
this.responseFormat = responseFormat
}

/**
* Alias for calling [responseFormat] with
* `AssistantResponseFormatOption.ofJsonValue()`.
*/
fun responseFormatJsonValue() =
responseFormat(AssistantResponseFormatOption.ofJsonValue())
/** Alias for calling [responseFormat] with `AssistantResponseFormatOption.ofAuto()`. */
fun responseFormatAuto() = responseFormat(AssistantResponseFormatOption.ofAuto())

/**
* Alias for calling [responseFormat] with
Expand Down
Loading
Loading