Skip to content

Commit 05508b6

Browse files
committed
Responses input improvements
1 parent 64c7274 commit 05508b6

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

Sources/OpenAI/Common/Content.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66
//
77

88
public enum Content: Encodable, Sendable {
9+
case text(String)
10+
case content(InputContent)
11+
12+
public func encode(to encoder: any Encoder) throws {
13+
var container = encoder.singleValueContainer()
14+
switch self {
15+
case .text(let text):
16+
try container.encode(text)
17+
case .content(let content):
18+
try container.encode(content)
19+
}
20+
}
21+
}
22+
23+
public enum InputContent: Encodable, Sendable {
924
case inputText(TextInput)
1025
case inputImage(ImageInput)
1126
case inputFile(FileInput)

Sources/OpenAI/Responses/ResponsesBody.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public extension Responses {
1414
/// Specify additional output data to include in the model response.
1515
public var include: [AdditionalOutput]?
1616
/// Text, image, or file inputs to the model, used to generate a response.
17-
public var input: [Input]?
17+
public var input: Input?
1818
/// A system (or developer) message inserted into the model's context.
1919
///
2020
/// When using along with `previous_response_id`, the instructions from a previous response
@@ -123,7 +123,22 @@ public extension Responses {
123123
case reasoningEncryptedContent = "reasoning.encrypted_content"
124124
}
125125

126-
public struct Input: Encodable, Sendable {
126+
public enum Input: Encodable, Sendable {
127+
case text(String)
128+
case input([InputObject])
129+
130+
public func encode(to encoder: any Encoder) throws {
131+
var container = encoder.singleValueContainer()
132+
switch self {
133+
case .text(let text):
134+
try container.encode(text)
135+
case .input(let object):
136+
try container.encode(object)
137+
}
138+
}
139+
}
140+
141+
public struct InputObject: Encodable, Sendable {
127142
/// The role of the message input. One of `user`, `assistant`, `system`, or `developer`.
128143
public let role: Role
129144

Sources/OpenAI/Responses/ResponsesWrapper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct ResponsesWrapper {
1212
public func create(
1313
background: Bool? = nil,
1414
include: [Responses.Body.AdditionalOutput]? = nil,
15-
input: [Responses.Body.Input]? = nil,
15+
input: Responses.Body.Input? = nil,
1616
instructions: String? = nil,
1717
maxOutputTokens: Int? = nil,
1818
maxToolCalls: Int? = nil,
@@ -65,7 +65,7 @@ public struct ResponsesWrapper {
6565
public func createStream(
6666
background: Bool? = nil,
6767
include: [Responses.Body.AdditionalOutput]? = nil,
68-
input: [Responses.Body.Input]? = nil,
68+
input: Responses.Body.Input? = nil,
6969
instructions: String? = nil,
7070
maxOutputTokens: Int? = nil,
7171
maxToolCalls: Int? = nil,

0 commit comments

Comments
 (0)