Skip to content

Commit 162a613

Browse files
committed
Update project metadata and add new JSON Schema properties: support array-like type of property to support optional props https://platform.openai.com/docs/guides/structured-outputs#all-fields-must-be-required
1 parent 9936f29 commit 162a613

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

OpenAI.SDK/ObjectModels/Converters.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,51 @@ public override void Write(Utf8JsonWriter writer, AssistantsApiToolChoiceOneOfTy
8989
writer.WriteNullValue();
9090
}
9191
}
92-
}
92+
}
93+
94+
public class SingleOrArrayToListConverter : JsonConverter<List<string?>>
95+
{
96+
public override List<string?>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
97+
switch (reader.TokenType) {
98+
case JsonTokenType.String:
99+
return [reader.GetString()];
100+
case JsonTokenType.StartArray:
101+
{
102+
var list = new List<string?>();
103+
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) {
104+
switch (reader.TokenType) {
105+
case JsonTokenType.String:
106+
list.Add(reader.GetString());
107+
break;
108+
case JsonTokenType.Null:
109+
list.Add(null);
110+
break;
111+
default:
112+
throw new JsonException($"Unexpected token in type array: {reader.TokenType}");
113+
}
114+
}
115+
return list;
116+
}
117+
default:
118+
throw new JsonException($"Unexpected token parsing type: {reader.TokenType}");
119+
}
120+
}
121+
122+
public override void Write(Utf8JsonWriter writer, List<string?>? value, JsonSerializerOptions options) {
123+
if (value == null || value.Count == 0) {
124+
writer.WriteNullValue();
125+
} else if (value.Count == 1) {
126+
writer.WriteStringValue(value[0]);
127+
} else {
128+
writer.WriteStartArray();
129+
foreach (var item in value) {
130+
if (item == null) {
131+
writer.WriteNullValue();
132+
} else {
133+
writer.WriteStringValue(item);
134+
}
135+
}
136+
writer.WriteEndArray();
137+
}
138+
}
139+
}

OpenAI.SDK/ObjectModels/SharedModels/FunctionParameters.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,26 @@ public enum FunctionObjectTypes
2020
}
2121

2222
/// <summary>
23-
/// Function parameter object type. Default value is "object".
23+
/// Property's type as a string. Only one: <see cref="TypeList"/> or <see cref="Type"/> should be set.
24+
/// </summary>
25+
[JsonIgnore]
26+
public string? Type {
27+
get => TypeList?.Count != 1 ? null : TypeList[0];
28+
set {
29+
if (value == null) {
30+
TypeList = null;
31+
return;
32+
}
33+
TypeList = [value];
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Property's type as a list of strings. Only one: <see cref="TypeList"/> or <see cref="Type"/> should be set.
2439
/// </summary>
2540
[JsonPropertyName("type")]
26-
public string? Type { get; set; }
41+
[JsonConverter(typeof(SingleOrArrayToListConverter))]
42+
public IList<string?>? TypeList { get; set; }
2743

2844
/// <summary>
2945
/// An instance validates successfully against this keyword if its value is equal to the value of the keyword

0 commit comments

Comments
 (0)