Skip to content

Commit d2c4436

Browse files
committed
Allow returning images and files from tool calls
1 parent d799839 commit d2c4436

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/Models/Input.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public extension Input.ListItem {
297297
/// - Parameter status: The status of the item. Populated when items are returned via API.
298298
/// - Parameter callId: The ID of the computer tool call that produced the output.
299299
/// - Parameter output: A JSON string of the output of the function tool call.
300-
static func functionCallOutput(id: String? = nil, status: Item.FunctionCall.Status? = nil, callId: String, output: String) -> Self {
300+
static func functionCallOutput(id: String? = nil, status: Item.FunctionCall.Status? = nil, callId: String, output: Input.Content) -> Self {
301301
.item(Item.Input.functionCallOutput(Item.FunctionCallOutput(id: id, status: status, callId: callId, output: output)))
302302
}
303303

src/Models/Item.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,15 @@ import MetaCodable
408408
public var callId: String
409409

410410
/// A JSON string of the output of the function tool call.
411-
public var output: String
411+
public var output: ModelInput.Content
412412

413413
/// Creates a new function call output.
414414
///
415415
/// - Parameter id: The unique ID of the function tool call output. Populated when this item is returned via API.
416416
/// - Parameter status: The status of the item. Populated when items are returned via API.
417417
/// - Parameter callId: The ID of the computer tool call that produced the output.
418418
/// - Parameter output: A JSON string of the output of the function tool call.
419-
public init(id: String? = nil, status: FunctionCall.Status? = nil, callId: String, output: String) {
419+
public init(id: String? = nil, status: FunctionCall.Status? = nil, callId: String, output: ModelInput.Content) {
420420
self.id = id
421421
self.status = status
422422
self.callId = callId
@@ -1210,14 +1210,14 @@ import MetaCodable
12101210
public var callId: String
12111211

12121212
/// The output from the custom tool call generated by your code.
1213-
public var output: String
1213+
public var output: ModelInput.Content
12141214

12151215
/// Creates a new custom tool call output
12161216
///
12171217
/// - Parameter id: The unique ID of the custom tool call output in the OpenAI platform.
12181218
/// - Parameter callId: The call ID, used to map this custom tool call output to a custom tool call.
12191219
/// - Parameter output: The output from the custom tool call generated by your code.
1220-
public init(id: String? = nil, callId: String, output: String) {
1220+
public init(id: String? = nil, callId: String, output: ModelInput.Content) {
12211221
self.id = id
12221222
self.callId = callId
12231223
self.output = output
@@ -1322,7 +1322,7 @@ public extension Item.Input {
13221322
/// - Parameter status: The status of the item. Populated when items are returned via API.
13231323
/// - Parameter callId: The ID of the computer tool call that produced the output.
13241324
/// - Parameter output: A JSON string of the output of the function tool call.
1325-
static func functionCallOutput(id: String? = nil, status: Item.FunctionCall.Status? = nil, callId: String, output: String) -> Self {
1325+
static func functionCallOutput(id: String? = nil, status: Item.FunctionCall.Status? = nil, callId: String, output: Input.Content) -> Self {
13261326
.functionCallOutput(Item.FunctionCallOutput(id: id, status: status, callId: callId, output: output))
13271327
}
13281328

@@ -1436,7 +1436,7 @@ public extension Item.Input {
14361436
/// - Parameter id: The unique ID of the custom tool call output in the OpenAI platform.
14371437
/// - Parameter callId: The call ID, used to map this custom tool call output to a custom tool call.
14381438
/// - Parameter output: The output from the custom tool call generated by your code.
1439-
static func customToolCallOutput(id: String? = nil, callId: String, output: String) -> Self {
1439+
static func customToolCallOutput(id: String? = nil, callId: String, output: ModelInput.Content) -> Self {
14401440
.customToolCallOutput(Item.CustomToolCallOutput(id: id, callId: callId, output: output))
14411441
}
14421442
}

src/Protocol/Toolable.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,22 @@ package extension Toolable {
4949
let output = try await call(parameters: parameters)
5050

5151
if output is NullableVoid { return nil }
52+
53+
if output is String {
54+
return Item.FunctionCallOutput(callId: functionCall.callId, output: .text(output as! String))
55+
}
56+
57+
if output is Input.Content {
58+
return Item.FunctionCallOutput(callId: functionCall.callId, output: output as! ModelInput.Content)
59+
}
60+
61+
if output is Input.Content.ContentItem {
62+
return Item.FunctionCallOutput(callId: functionCall.callId, output: .list([output as! ModelInput.Content.ContentItem]))
63+
}
64+
5265
return try Item.FunctionCallOutput(
5366
callId: functionCall.callId,
54-
output: encoder.encodeToString(output)
67+
output: .text(encoder.encodeToString(output))
5568
)
5669
}
5770
}

0 commit comments

Comments
 (0)