@@ -33,15 +33,13 @@ object ChatRequestBody {
33
33
34
34
/** OpenAI's JSON schema support imposes two requirements:
35
35
*
36
- * 1. All fields must be `required`:
37
- * https://platform.openai.com/docs/guides/structured-outputs/all-fields-must-be-required
36
+ * 1. All fields must be `required`: https://platform.openai.com/docs/guides/structured-outputs/all-fields-must-be-required
38
37
*
39
- * 2. `additionalProperties: false` must always be set in objects:
40
- * https://platform.openai.com/docs/guides/structured-outputs/additionalproperties-false-must-always-be-set-in-objects
38
+ * 2. `additionalProperties: false` must always be set in objects:
39
+ * https://platform.openai.com/docs/guides/structured-outputs/additionalproperties-false-must-always-be-set-in-objects
41
40
*
42
- * We implement these by folding over the JSON structure.
43
- * However, if a schema uses discriminated unions (indicated by a `discriminator` property),
44
- * we skip forcing `additionalProperties: false` to preserve flexibility in selecting sub-schemas.
41
+ * We implement these by folding over the JSON structure. However, if a schema uses discriminated unions (indicated by a
42
+ * `discriminator` property), we skip forcing `additionalProperties: false` to preserve flexibility in selecting sub-schemas.
45
43
*/
46
44
private val schemaFolder : Json .Folder [Json ] = new Json .Folder [Json ] {
47
45
lazy val onNull = Json .Null
@@ -50,21 +48,20 @@ object ChatRequestBody {
50
48
def onString (value : String ): Json = Json .fromString(value)
51
49
def onArray (value : Vector [Json ]): Json = Json .fromValues(value.map(_.foldWith(this )))
52
50
def onObject (value : JsonObject ): Json = {
53
- val state = value.toList.foldRight(FolderState (Nil , false , Nil )) {
54
- case ((k, v), acc) =>
55
- if (k == " properties" )
56
- acc.copy(
57
- fields = (k, v.foldWith(this )) :: acc.fields,
58
- addAdditionalProperties = true ,
59
- requiredProperties = v.asObject.fold(List .empty[String ])(_.keys.toList)
60
- )
61
- else if (k == " type" )
62
- acc.copy(
63
- fields = (k, v.foldWith(this )) :: acc.fields,
64
- addAdditionalProperties = acc.addAdditionalProperties || v.asString.contains(" object" )
65
- )
66
- else
67
- acc.copy(fields = (k, v.foldWith(this )) :: acc.fields)
51
+ val state = value.toList.foldRight(FolderState (Nil , false , Nil )) { case ((k, v), acc) =>
52
+ if (k == " properties" )
53
+ acc.copy(
54
+ fields = (k, v.foldWith(this )) :: acc.fields,
55
+ addAdditionalProperties = true ,
56
+ requiredProperties = v.asObject.fold(List .empty[String ])(_.keys.toList)
57
+ )
58
+ else if (k == " type" )
59
+ acc.copy(
60
+ fields = (k, v.foldWith(this )) :: acc.fields,
61
+ addAdditionalProperties = acc.addAdditionalProperties || v.asString.contains(" object" )
62
+ )
63
+ else
64
+ acc.copy(fields = (k, v.foldWith(this )) :: acc.fields)
68
65
}
69
66
70
67
// Detect if this object is part of a discriminated union by checking for a "discriminator" property.
0 commit comments