Skip to content

Commit 6df63b8

Browse files
committed
Expose CodeInterpreter tool for responses
1 parent 81c4795 commit 6df63b8

File tree

11 files changed

+213
-51
lines changed

11 files changed

+213
-51
lines changed

api/OpenAI.net8.0.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4590,6 +4590,15 @@ public class TurnDetectionOptions : IJsonModel<TurnDetectionOptions>, IPersistab
45904590
}
45914591
}
45924592
namespace OpenAI.Responses {
4593+
[Experimental("OPENAI001")]
4594+
public class CodeInterpreterTool : ResponseTool, IJsonModel<CodeInterpreterTool>, IPersistableModel<CodeInterpreterTool> {
4595+
public CodeInterpreterTool(BinaryData container);
4596+
public BinaryData Container { get; set; }
4597+
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
4598+
protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
4599+
protected override ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
4600+
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
4601+
}
45934602
[Experimental("OPENAICUA001")]
45944603
public class ComputerCallAction : IJsonModel<ComputerCallAction>, IPersistableModel<ComputerCallAction> {
45954604
public Drawing.Point? ClickCoordinates { get; }
@@ -5475,6 +5484,8 @@ public class ResponseTokenUsage : IJsonModel<ResponseTokenUsage>, IPersistableMo
54755484
}
54765485
[Experimental("OPENAI001")]
54775486
public class ResponseTool : IJsonModel<ResponseTool>, IPersistableModel<ResponseTool> {
5487+
public static CodeInterpreterTool CreateCodeInterpreterTool(IEnumerable<string> fileIds = null);
5488+
public static CodeInterpreterTool CreateCodeInterpreterTool(string containerId);
54785489
[Experimental("OPENAICUA001")]
54795490
public static ComputerTool CreateComputerTool(ComputerToolEnvironment environment, int displayWidth, int displayHeight);
54805491
public static FileSearchTool CreateFileSearchTool(IEnumerable<string> vectorStoreIds, int? maxResultCount = null, FileSearchToolRankingOptions rankingOptions = null, BinaryData filters = null);

api/OpenAI.netstandard2.0.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4023,6 +4023,14 @@ public class TurnDetectionOptions : IJsonModel<TurnDetectionOptions>, IPersistab
40234023
}
40244024
}
40254025
namespace OpenAI.Responses {
4026+
public class CodeInterpreterTool : ResponseTool, IJsonModel<CodeInterpreterTool>, IPersistableModel<CodeInterpreterTool> {
4027+
public CodeInterpreterTool(BinaryData container);
4028+
public BinaryData Container { get; set; }
4029+
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
4030+
protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
4031+
protected override ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
4032+
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
4033+
}
40264034
public class ComputerCallAction : IJsonModel<ComputerCallAction>, IPersistableModel<ComputerCallAction> {
40274035
public Drawing.Point? ClickCoordinates { get; }
40284036
public ComputerCallActionMouseButton? ClickMouseButton { get; }
@@ -4834,6 +4842,8 @@ public class ResponseTokenUsage : IJsonModel<ResponseTokenUsage>, IPersistableMo
48344842
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
48354843
}
48364844
public class ResponseTool : IJsonModel<ResponseTool>, IPersistableModel<ResponseTool> {
4845+
public static CodeInterpreterTool CreateCodeInterpreterTool(IEnumerable<string> fileIds = null);
4846+
public static CodeInterpreterTool CreateCodeInterpreterTool(string containerId);
48374847
public static ComputerTool CreateComputerTool(ComputerToolEnvironment environment, int displayWidth, int displayHeight);
48384848
public static FileSearchTool CreateFileSearchTool(IEnumerable<string> vectorStoreIds, int? maxResultCount = null, FileSearchToolRankingOptions rankingOptions = null, BinaryData filters = null);
48394849
public static FunctionTool CreateFunctionTool(string functionName, BinaryData functionParameters, bool? strictModeEnabled, string functionDescription = null);

src/Custom/Responses/Internal/GeneratorStubs.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ namespace OpenAI.Responses;
104104
[CodeGenType("CodeInterpreterToolCallItemResourceStatus")] internal readonly partial struct InternalCodeInterpreterToolCallItemResourceStatus {}
105105
[CodeGenType("LocalShellToolCallItemResourceStatus")] internal readonly partial struct InternalLocalShellToolCallItemResourceStatus {}
106106
[CodeGenType("LocalShellToolCallOutputItemResourceStatus")] internal readonly partial struct InternalLocalShellToolCallOutputItemResourceStatus {}
107-
[CodeGenType("CodeInterpreterTool")] internal partial class InternalCodeInterpreterTool {}
108107
[CodeGenType("CodeInterpreterToolAuto")] internal partial class InternalCodeInterpreterToolAuto {}
109108
[CodeGenType("ImageGenTool")] internal partial class InternalImageGenTool {}
110109
[CodeGenType("ImageGenToolInputImageMask")] internal partial class InternalImageGenToolInputImageMask {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace OpenAI.Responses;
4+
5+
// CUSTOM: correct namespace.
6+
[CodeGenType("CodeInterpreterTool")]
7+
public partial class CodeInterpreterTool
8+
{
9+
internal static BinaryData AutoContainer { get; } = BinaryData.FromString("""{"type": "auto"}""");
10+
}

src/Custom/Responses/Tools/ResponseTool.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public static FunctionTool CreateFunctionTool(string functionName, BinaryData fu
2525

2626
// CUSTOM: Added factory method a a convenience.
2727
[Experimental("OPENAICUA001")]
28-
public static ComputerTool CreateComputerTool(ComputerToolEnvironment environment, int displayWidth,int displayHeight)
28+
public static ComputerTool CreateComputerTool(ComputerToolEnvironment environment, int displayWidth, int displayHeight)
2929
{
3030
return new ComputerTool(
3131
kind: InternalToolType.ComputerUsePreview,
3232
additionalBinaryDataProperties: null,
3333
environment: environment,
3434
displayWidth: displayWidth,
35-
displayHeight:displayHeight);
35+
displayHeight: displayHeight);
3636
}
3737

3838
// CUSTOM: Added factory method a a convenience.
@@ -74,4 +74,42 @@ public static McpTool CreateMcpTool(string serverLabel, Uri serverUri, IDictiona
7474
allowedTools: allowedTools,
7575
toolCallApprovalPolicy: toolCallApprovalPolicy);
7676
}
77+
78+
// CUSTOM: Added factory method a a convenience.
79+
/// <summary>
80+
/// Creates a new instance of the <see cref="CodeInterpreterTool"/> class with an auto-generated container.
81+
/// </summary>
82+
/// <param name="fileIds">The file IDs to include in the container.</param>
83+
/// <returns></returns>
84+
public static CodeInterpreterTool CreateCodeInterpreterTool(IEnumerable<string> fileIds = null)
85+
{
86+
string containerJson = fileIds?.Any() == true ?
87+
$"{{\"type\": \"auto\", \"file_ids\": [{string.Join(", ", fileIds.Select(id => $"\"{id}\""))}]}}" :
88+
"{\"type\": \"auto\"}";
89+
90+
return new CodeInterpreterTool(
91+
kind: InternalToolType.CodeInterpreter,
92+
additionalBinaryDataProperties: null,
93+
container: new BinaryData(containerJson));
94+
}
95+
96+
// CUSTOM: Added factory method a a convenience.
97+
/// <summary>
98+
///
99+
/// </summary>
100+
/// <param name="containerId">The Id of a previously created container</param>
101+
/// <returns></returns>
102+
public static CodeInterpreterTool CreateCodeInterpreterTool(string containerId)
103+
{
104+
Argument.AssertNotNull(containerId, nameof(containerId));
105+
106+
// Since the container field can be a string or an object, when it's a string (container ID),
107+
// we need to serialize it as a JSON string value
108+
string containerJson = $"\"{containerId}\"";
109+
110+
return new CodeInterpreterTool(
111+
kind: InternalToolType.CodeInterpreter,
112+
additionalBinaryDataProperties: null,
113+
container: new BinaryData(containerJson));
114+
}
77115
}

src/Generated/Models/OpenAIContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace OpenAI
6262
[ModelReaderWriterBuildable(typeof(ChatToolCall))]
6363
[ModelReaderWriterBuildable(typeof(ChatToolChoice))]
6464
[ModelReaderWriterBuildable(typeof(ChatWebSearchOptions))]
65+
[ModelReaderWriterBuildable(typeof(CodeInterpreterTool))]
6566
[ModelReaderWriterBuildable(typeof(CodeInterpreterToolDefinition))]
6667
[ModelReaderWriterBuildable(typeof(CodeInterpreterToolResources))]
6768
[ModelReaderWriterBuildable(typeof(ComputerCallAction))]
@@ -188,7 +189,6 @@ namespace OpenAI
188189
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterFileOutput))]
189190
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterFileOutputFile))]
190191
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterTextOutput))]
191-
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterTool))]
192192
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterToolAuto))]
193193
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterToolCallItemParam))]
194194
[ModelReaderWriterBuildable(typeof(InternalCodeInterpreterToolCallItemResource))]

src/Generated/Models/Responses/InternalCodeInterpreterTool.Serialization.cs renamed to src/Generated/Models/Responses/CodeInterpreterTool.Serialization.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
namespace OpenAI.Responses
1212
{
13-
internal partial class InternalCodeInterpreterTool : IJsonModel<InternalCodeInterpreterTool>
13+
public partial class CodeInterpreterTool : IJsonModel<CodeInterpreterTool>
1414
{
15-
internal InternalCodeInterpreterTool() : this(InternalToolType.CodeInterpreter, null, null)
15+
internal CodeInterpreterTool() : this(InternalToolType.CodeInterpreter, null, null)
1616
{
1717
}
1818

19-
void IJsonModel<InternalCodeInterpreterTool>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
19+
void IJsonModel<CodeInterpreterTool>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
2020
{
2121
writer.WriteStartObject();
2222
JsonModelWriteCore(writer, options);
@@ -25,10 +25,10 @@ void IJsonModel<InternalCodeInterpreterTool>.Write(Utf8JsonWriter writer, ModelR
2525

2626
protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
2727
{
28-
string format = options.Format == "W" ? ((IPersistableModel<InternalCodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
28+
string format = options.Format == "W" ? ((IPersistableModel<CodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
2929
if (format != "J")
3030
{
31-
throw new FormatException($"The model {nameof(InternalCodeInterpreterTool)} does not support writing '{format}' format.");
31+
throw new FormatException($"The model {nameof(CodeInterpreterTool)} does not support writing '{format}' format.");
3232
}
3333
base.JsonModelWriteCore(writer, options);
3434
if (_additionalBinaryDataProperties?.ContainsKey("container") != true)
@@ -45,20 +45,20 @@ protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWri
4545
}
4646
}
4747

48-
InternalCodeInterpreterTool IJsonModel<InternalCodeInterpreterTool>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (InternalCodeInterpreterTool)JsonModelCreateCore(ref reader, options);
48+
CodeInterpreterTool IJsonModel<CodeInterpreterTool>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (CodeInterpreterTool)JsonModelCreateCore(ref reader, options);
4949

5050
protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
5151
{
52-
string format = options.Format == "W" ? ((IPersistableModel<InternalCodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
52+
string format = options.Format == "W" ? ((IPersistableModel<CodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
5353
if (format != "J")
5454
{
55-
throw new FormatException($"The model {nameof(InternalCodeInterpreterTool)} does not support reading '{format}' format.");
55+
throw new FormatException($"The model {nameof(CodeInterpreterTool)} does not support reading '{format}' format.");
5656
}
5757
using JsonDocument document = JsonDocument.ParseValue(ref reader);
58-
return DeserializeInternalCodeInterpreterTool(document.RootElement, options);
58+
return DeserializeCodeInterpreterTool(document.RootElement, options);
5959
}
6060

61-
internal static InternalCodeInterpreterTool DeserializeInternalCodeInterpreterTool(JsonElement element, ModelReaderWriterOptions options)
61+
internal static CodeInterpreterTool DeserializeCodeInterpreterTool(JsonElement element, ModelReaderWriterOptions options)
6262
{
6363
if (element.ValueKind == JsonValueKind.Null)
6464
{
@@ -82,40 +82,40 @@ internal static InternalCodeInterpreterTool DeserializeInternalCodeInterpreterTo
8282
// Plugin customization: remove options.Format != "W" check
8383
additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
8484
}
85-
return new InternalCodeInterpreterTool(kind, additionalBinaryDataProperties, container);
85+
return new CodeInterpreterTool(kind, additionalBinaryDataProperties, container);
8686
}
8787

88-
BinaryData IPersistableModel<InternalCodeInterpreterTool>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
88+
BinaryData IPersistableModel<CodeInterpreterTool>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
8989

9090
protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
9191
{
92-
string format = options.Format == "W" ? ((IPersistableModel<InternalCodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
92+
string format = options.Format == "W" ? ((IPersistableModel<CodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
9393
switch (format)
9494
{
9595
case "J":
9696
return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
9797
default:
98-
throw new FormatException($"The model {nameof(InternalCodeInterpreterTool)} does not support writing '{options.Format}' format.");
98+
throw new FormatException($"The model {nameof(CodeInterpreterTool)} does not support writing '{options.Format}' format.");
9999
}
100100
}
101101

102-
InternalCodeInterpreterTool IPersistableModel<InternalCodeInterpreterTool>.Create(BinaryData data, ModelReaderWriterOptions options) => (InternalCodeInterpreterTool)PersistableModelCreateCore(data, options);
102+
CodeInterpreterTool IPersistableModel<CodeInterpreterTool>.Create(BinaryData data, ModelReaderWriterOptions options) => (CodeInterpreterTool)PersistableModelCreateCore(data, options);
103103

104104
protected override ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
105105
{
106-
string format = options.Format == "W" ? ((IPersistableModel<InternalCodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
106+
string format = options.Format == "W" ? ((IPersistableModel<CodeInterpreterTool>)this).GetFormatFromOptions(options) : options.Format;
107107
switch (format)
108108
{
109109
case "J":
110110
using (JsonDocument document = JsonDocument.Parse(data))
111111
{
112-
return DeserializeInternalCodeInterpreterTool(document.RootElement, options);
112+
return DeserializeCodeInterpreterTool(document.RootElement, options);
113113
}
114114
default:
115-
throw new FormatException($"The model {nameof(InternalCodeInterpreterTool)} does not support reading '{options.Format}' format.");
115+
throw new FormatException($"The model {nameof(CodeInterpreterTool)} does not support reading '{options.Format}' format.");
116116
}
117117
}
118118

119-
string IPersistableModel<InternalCodeInterpreterTool>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
119+
string IPersistableModel<CodeInterpreterTool>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
120120
}
121121
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <auto-generated/>
2+
3+
#nullable disable
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
8+
using OpenAI;
9+
10+
namespace OpenAI.Responses
11+
{
12+
[Experimental("OPENAI001")]
13+
public partial class CodeInterpreterTool : ResponseTool
14+
{
15+
public CodeInterpreterTool(BinaryData container) : base(InternalToolType.CodeInterpreter)
16+
{
17+
Argument.AssertNotNull(container, nameof(container));
18+
19+
Container = container;
20+
}
21+
22+
internal CodeInterpreterTool(InternalToolType kind, IDictionary<string, BinaryData> additionalBinaryDataProperties, BinaryData container) : base(kind, additionalBinaryDataProperties)
23+
{
24+
Container = container;
25+
}
26+
27+
public BinaryData Container { get; set; }
28+
}
29+
}

src/Generated/Models/Responses/InternalCodeInterpreterTool.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Generated/Models/Responses/ResponseTool.Serialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal static ResponseTool DeserializeResponseTool(JsonElement element, ModelR
8989
case "web_search_preview":
9090
return WebSearchTool.DeserializeWebSearchTool(element, options);
9191
case "code_interpreter":
92-
return InternalCodeInterpreterTool.DeserializeInternalCodeInterpreterTool(element, options);
92+
return CodeInterpreterTool.DeserializeCodeInterpreterTool(element, options);
9393
case "image_generation":
9494
return InternalImageGenTool.DeserializeInternalImageGenTool(element, options);
9595
case "local_shell":

0 commit comments

Comments
 (0)