Skip to content

Commit 02172bc

Browse files
Refactor ChatResponseFormat -> TextResponseFormat
first pass responses unit test
1 parent 90cc841 commit 02172bc

19 files changed

+603
-88
lines changed

OpenAI/Packages/com.openai.unity/Runtime/Assistants/AssistantResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ internal AssistantResponse(
163163

164164
/// <summary>
165165
/// Specifies the format that the model must output.
166-
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
166+
/// Setting to <see cref="TextResponseFormat.Json"/> or <see cref="TextResponseFormat.JsonSchema"/> enables JSON mode,
167167
/// which guarantees the message the model generates is valid JSON.
168168
/// </summary>
169169
/// <remarks>
@@ -180,7 +180,7 @@ internal AssistantResponse(
180180

181181
[Preserve]
182182
[JsonIgnore]
183-
public ChatResponseFormat ResponseFormat => ResponseFormatObject ?? ChatResponseFormat.Auto;
183+
public TextResponseFormat ResponseFormat => ResponseFormatObject ?? TextResponseFormat.Auto;
184184

185185
[Preserve]
186186
public static implicit operator string(AssistantResponse assistant) => assistant?.Id;

OpenAI/Packages/com.openai.unity/Runtime/Assistants/CreateAssistantRequest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public CreateAssistantRequest(
2525
double? temperature,
2626
double? topP,
2727
JsonSchema jsonSchema,
28-
ChatResponseFormat? responseFormat = null)
28+
TextResponseFormat? responseFormat = null)
2929
: this(
3030
string.IsNullOrWhiteSpace(model) ? assistant.Model : model,
3131
string.IsNullOrWhiteSpace(name) ? assistant.Name : name,
@@ -100,7 +100,7 @@ public CreateAssistantRequest(
100100
/// </param>
101101
/// <param name="responseFormat">
102102
/// Specifies the format that the model must output.
103-
/// Setting to <see cref="ChatResponseFormat.Json"/> enables JSON mode,
103+
/// Setting to <see cref="TextResponseFormat.Json"/> enables JSON mode,
104104
/// which guarantees the message the model generates is valid JSON.<br/>
105105
/// Important: When using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.
106106
/// Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit,
@@ -121,7 +121,7 @@ public CreateAssistantRequest(
121121
double? topP = null,
122122
ReasoningEffort reasoningEffort = 0,
123123
JsonSchema jsonSchema = null,
124-
ChatResponseFormat? responseFormat = null)
124+
TextResponseFormat? responseFormat = null)
125125
: this(
126126
string.IsNullOrWhiteSpace(model) ? assistant.Model : model,
127127
string.IsNullOrWhiteSpace(name) ? assistant.Name : name,
@@ -195,7 +195,7 @@ public CreateAssistantRequest(
195195
/// </param>
196196
/// <param name="responseFormat">
197197
/// Specifies the format that the model must output.
198-
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
198+
/// Setting to <see cref="TextResponseFormat.Json"/> or <see cref="TextResponseFormat.JsonSchema"/> enables JSON mode,
199199
/// which guarantees the message the model generates is valid JSON.<br/>
200200
/// Important: When using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.
201201
/// Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit,
@@ -215,7 +215,7 @@ public CreateAssistantRequest(
215215
double? topP = null,
216216
ReasoningEffort reasoningEffort = 0,
217217
JsonSchema jsonSchema = null,
218-
ChatResponseFormat responseFormat = ChatResponseFormat.Text)
218+
TextResponseFormat responseFormat = TextResponseFormat.Text)
219219
{
220220
Model = string.IsNullOrWhiteSpace(model) ? Models.Model.GPT4o : model;
221221
Name = name;
@@ -318,7 +318,7 @@ public CreateAssistantRequest(
318318

319319
/// <summary>
320320
/// Specifies the format that the model must output.
321-
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
321+
/// Setting to <see cref="TextResponseFormat.Json"/> or <see cref="TextResponseFormat.JsonSchema"/> enables JSON mode,
322322
/// which guarantees the message the model generates is valid JSON.
323323
/// </summary>
324324
/// <remarks>
@@ -335,7 +335,7 @@ public CreateAssistantRequest(
335335

336336
[Preserve]
337337
[JsonIgnore]
338-
public ChatResponseFormat ResponseFormat => ResponseFormatObject ?? ChatResponseFormat.Auto;
338+
public TextResponseFormat ResponseFormat => ResponseFormatObject ?? TextResponseFormat.Auto;
339339

340340
/// <summary>
341341
/// Set of 16 key-value pairs that can be attached to an object.

OpenAI/Packages/com.openai.unity/Runtime/Chat/ChatRequest.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ChatRequest(
2424
int? maxTokens = null,
2525
int? number = null,
2626
double? presencePenalty = null,
27-
ChatResponseFormat responseFormat = ChatResponseFormat.Auto,
27+
TextResponseFormat responseFormat = TextResponseFormat.Auto,
2828
int? seed = null,
2929
string[] stops = null,
3030
double? temperature = null,
@@ -38,28 +38,26 @@ public ChatRequest(
3838
: this(messages, model, frequencyPenalty, logitBias, maxTokens, number, presencePenalty,
3939
responseFormat, seed, stops, temperature, topP, topLogProbs, parallelToolCalls, jsonSchema, audioConfig, reasoningEffort, user)
4040
{
41+
if (string.IsNullOrWhiteSpace(toolChoice))
42+
{
43+
ToolChoice = "auto";
44+
}
45+
4146
var toolList = tools?.ToList();
4247

4348
if (toolList is { Count: > 0 })
4449
{
45-
if (string.IsNullOrWhiteSpace(toolChoice))
50+
if (!toolChoice.Equals("none") &&
51+
!toolChoice.Equals("required") &&
52+
!toolChoice.Equals("auto"))
4653
{
47-
ToolChoice = "auto";
54+
var tool = toolList.FirstOrDefault(t => t.Function.Name.Contains(toolChoice)) ??
55+
throw new ArgumentException($"The specified tool choice '{toolChoice}' was not found in the list of tools");
56+
ToolChoice = new { type = "function", function = new { name = tool.Function.Name } };
4857
}
4958
else
5059
{
51-
if (!toolChoice.Equals("none") &&
52-
!toolChoice.Equals("required") &&
53-
!toolChoice.Equals("auto"))
54-
{
55-
var tool = toolList.FirstOrDefault(t => t.Function.Name.Contains(toolChoice)) ??
56-
throw new ArgumentException($"The specified tool choice '{toolChoice}' was not found in the list of tools");
57-
ToolChoice = new { type = "function", function = new { name = tool.Function.Name } };
58-
}
59-
else
60-
{
61-
ToolChoice = toolChoice;
62-
}
60+
ToolChoice = toolChoice;
6361
}
6462

6563
foreach (var tool in toolList)
@@ -117,7 +115,7 @@ public ChatRequest(
117115
/// </param>
118116
/// <param name="responseFormat">
119117
/// An object specifying the format that the model must output.
120-
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
118+
/// Setting to <see cref="TextResponseFormat.Json"/> or <see cref="TextResponseFormat.JsonSchema"/> enables JSON mode,
121119
/// which guarantees the message the model generates is valid JSON.
122120
/// </param>
123121
/// <param name="frequencyPenalty">
@@ -167,7 +165,7 @@ public ChatRequest(
167165
int? maxTokens = null,
168166
int? number = null,
169167
double? presencePenalty = null,
170-
ChatResponseFormat responseFormat = ChatResponseFormat.Auto,
168+
TextResponseFormat responseFormat = TextResponseFormat.Auto,
171169
int? seed = null,
172170
string[] stops = null,
173171
double? temperature = null,
@@ -222,8 +220,8 @@ public ChatRequest(
222220
{
223221
ResponseFormatObject = responseFormat switch
224222
{
225-
ChatResponseFormat.Text or ChatResponseFormat.Json or ChatResponseFormat.JsonSchema => responseFormat,
226-
_ => null
223+
TextResponseFormat.Auto => null,
224+
_ => responseFormat
227225
};
228226
}
229227

@@ -380,7 +378,7 @@ public ChatRequest(
380378

381379
/// <summary>
382380
/// An object specifying the format that the model must output.
383-
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
381+
/// Setting to <see cref="TextResponseFormat.Json"/> or <see cref="TextResponseFormat.JsonSchema"/> enables JSON mode,
384382
/// which guarantees the message the model generates is valid JSON.
385383
/// </summary>
386384
/// <remarks>
@@ -396,7 +394,7 @@ public ChatRequest(
396394

397395
/// <summary>
398396
/// An object specifying the format that the model must output.
399-
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
397+
/// Setting to <see cref="TextResponseFormat.Json"/> or <see cref="TextResponseFormat.JsonSchema"/> enables JSON mode,
400398
/// which guarantees the message the model generates is valid JSON.
401399
/// </summary>
402400
/// <remarks>
@@ -407,7 +405,7 @@ public ChatRequest(
407405
/// </remarks>
408406
[Preserve]
409407
[JsonIgnore]
410-
public ChatResponseFormat ResponseFormat => ResponseFormatObject ?? ChatResponseFormat.Auto;
408+
public TextResponseFormat ResponseFormat => ResponseFormatObject ?? TextResponseFormat.Auto;
411409

412410
/// <summary>
413411
/// This feature is in Beta. If specified, our system will make a best effort to sample deterministically,

OpenAI/Packages/com.openai.unity/Runtime/Common/ChatResponseFormat.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
// Licensed under the MIT License. See LICENSE in the project root for license information.
22

3+
using System;
34
using System.Runtime.Serialization;
45

56
namespace OpenAI
67
{
7-
/// <summary>
8-
/// TODO rename to TextResponseFormat
9-
/// </summary>
8+
[Obsolete("use TextResponseFormat instead")]
109
public enum ChatResponseFormat
1110
{
1211
Auto = 0,
12+
/// <summary>
13+
/// Default response format. Used to generate text responses.
14+
/// </summary>
1315
[EnumMember(Value = "text")]
1416
Text,
17+
/// <summary>
18+
/// JSON object response format.
19+
/// An older method of generating JSON responses.
20+
/// Using `json_schema` is recommended for models that support it.
21+
/// Note that the model will not generate JSON without a system or user message
22+
/// instructing it to do so.
23+
/// </summary>
24+
/// <remarks>
25+
/// Not recommended for gpt-4o and newer models!
26+
/// </remarks>
1527
[EnumMember(Value = "json_object")]
1628
Json,
29+
/// <summary>
30+
/// JSON Schema response format. Used to generate structured JSON responses.
31+
/// </summary>
1732
[EnumMember(Value = "json_schema")]
1833
JsonSchema
1934
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
namespace OpenAI
7+
{
8+
public enum TextResponseFormat
9+
{
10+
Auto = 0,
11+
/// <summary>
12+
/// Default response format. Used to generate text responses.
13+
/// </summary>
14+
[EnumMember(Value = "text")]
15+
Text,
16+
/// <summary>
17+
/// JSON object response format.
18+
/// An older method of generating JSON responses.
19+
/// Using `json_schema` is recommended for models that support it.
20+
/// Note that the model will not generate JSON without a system or user message
21+
/// instructing it to do so.
22+
/// </summary>
23+
/// <remarks>
24+
/// Not recommended for gpt-4o and newer models!
25+
/// </remarks>
26+
[Obsolete("use JsonSchema instead")]
27+
[EnumMember(Value = "json_object")]
28+
Json,
29+
/// <summary>
30+
/// JSON Schema response format. Used to generate structured JSON responses.
31+
/// </summary>
32+
[EnumMember(Value = "json_schema")]
33+
JsonSchema
34+
}
35+
}

OpenAI/Packages/com.openai.unity/Runtime/Common/TextResponseFormat.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OpenAI/Packages/com.openai.unity/Runtime/Common/TextResponseFormatConfiguration.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,44 @@ public sealed class TextResponseFormatConfiguration
1313
public TextResponseFormatConfiguration() { }
1414

1515
[Preserve]
16+
[Obsolete("Use new overload with TextResponseFormat instead")]
1617
public TextResponseFormatConfiguration(ChatResponseFormat type)
1718
{
1819
if (type == ChatResponseFormat.JsonSchema)
1920
{
2021
throw new ArgumentException("Use the constructor overload that accepts a JsonSchema object for ChatResponseFormat.JsonSchema.", nameof(type));
2122
}
23+
24+
Type = type switch
25+
{
26+
ChatResponseFormat.Text => TextResponseFormat.Text,
27+
ChatResponseFormat.Json => TextResponseFormat.Json,
28+
_ => throw new ArgumentOutOfRangeException(nameof(type), $"Unsupported response format: {type}")
29+
};
30+
}
31+
32+
[Preserve]
33+
public TextResponseFormatConfiguration(TextResponseFormat type)
34+
{
35+
if (type == TextResponseFormat.JsonSchema)
36+
{
37+
throw new ArgumentException("Use the constructor overload that accepts a JsonSchema object for ChatResponseFormat.JsonSchema.", nameof(type));
38+
}
39+
2240
Type = type;
2341
}
2442

2543
[Preserve]
2644
public TextResponseFormatConfiguration(JsonSchema schema)
2745
{
28-
Type = ChatResponseFormat.JsonSchema;
46+
Type = TextResponseFormat.JsonSchema;
2947
JsonSchema = schema;
3048
}
3149

3250
[Preserve]
3351
[JsonConstructor]
3452
internal TextResponseFormatConfiguration(
35-
[JsonProperty("type")] ChatResponseFormat type,
53+
[JsonProperty("type")] TextResponseFormat type,
3654
[JsonProperty("json_schema")] JsonSchema schema)
3755
{
3856
Type = type;
@@ -41,15 +59,35 @@ internal TextResponseFormatConfiguration(
4159

4260
[Preserve]
4361
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Include)]
44-
public ChatResponseFormat Type { get; private set; }
62+
public TextResponseFormat Type { get; private set; }
4563

4664
[Preserve]
4765
[JsonProperty("json_schema", DefaultValueHandling = DefaultValueHandling.Ignore)]
4866
public JsonSchema JsonSchema { get; private set; }
4967

68+
#pragma warning disable CS0618 // Type or member is obsolete
69+
[Preserve]
70+
[Obsolete("Use new overload with TextResponseFormat instead")]
5071
public static implicit operator TextResponseFormatConfiguration(ChatResponseFormat type) => new(type);
5172

5273
[Preserve]
53-
public static implicit operator ChatResponseFormat(TextResponseFormatConfiguration format) => format.Type;
74+
[Obsolete("Use new overload with TextResponseFormat instead")]
75+
public static implicit operator ChatResponseFormat(TextResponseFormatConfiguration format)
76+
{
77+
return format.Type switch
78+
{
79+
TextResponseFormat.Text => ChatResponseFormat.Text,
80+
TextResponseFormat.Json => ChatResponseFormat.Json,
81+
TextResponseFormat.JsonSchema => ChatResponseFormat.JsonSchema,
82+
_ => throw new ArgumentOutOfRangeException(nameof(format), $"Unsupported response format: {format.Type}")
83+
};
84+
}
85+
#pragma warning restore CS0618 // Type or member is obsolete
86+
87+
[Preserve]
88+
public static implicit operator TextResponseFormatConfiguration(TextResponseFormat type) => new(type);
89+
90+
[Preserve]
91+
public static implicit operator TextResponseFormat(TextResponseFormatConfiguration format) => format.Type;
5492
}
5593
}

OpenAI/Packages/com.openai.unity/Runtime/Extensions/ResponseContentConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2424
{
2525
"input_text" => jObject.ToObject<TextContent>(serializer),
2626
"output_text" => jObject.ToObject<TextContent>(serializer),
27+
"input_audio" => jObject.ToObject<AudioContent>(serializer),
2728
"input_image" => jObject.ToObject<ImageContent>(serializer),
2829
"input_file" => jObject.ToObject<FileContent>(serializer),
2930
"refusal" => jObject.ToObject<RefusalContent>(serializer),

OpenAI/Packages/com.openai.unity/Runtime/Extensions/TextResponseFormatConverter.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ public override TextResponseFormatConfiguration ReadJson(JsonReader reader, Type
1616
{
1717
if (reader.TokenType is JsonToken.Null or JsonToken.String)
1818
{
19-
return ChatResponseFormat.Auto;
19+
return TextResponseFormat.Auto;
2020
}
2121

2222
return serializer.Deserialize<TextResponseFormatConfiguration>(reader);
2323
}
2424
catch (Exception e)
2525
{
26-
throw new JsonSerializationException($"Error reading {nameof(ChatResponseFormat)} from JSON", e);
26+
throw new JsonSerializationException($"Error reading {nameof(TextResponseFormat)} from JSON", e);
2727
}
2828
}
2929

3030
[Preserve]
3131
public override void WriteJson(JsonWriter writer, TextResponseFormatConfiguration value, JsonSerializer serializer)
32-
{
33-
serializer.Serialize(writer, value);
34-
}
32+
=> serializer.Serialize(writer, value);
3533
}
3634
}

0 commit comments

Comments
 (0)