Skip to content

Commit 62a8b14

Browse files
Adding output text
1 parent 0a2a810 commit 62a8b14

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Sources/OpenAI/Public/ResponseModels/Response/ResponseModel.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ public struct ResponseModel: Decodable {
9393
/// A unique identifier representing your end-user.
9494
public let user: String?
9595

96+
/// Convenience property that aggregates all text output from output_text items in the output array.
97+
/// Similar to the outputText property in Python and JavaScript SDKs.
98+
public var outputText: String? {
99+
let outputTextItems = output.compactMap { outputItem -> String? in
100+
switch outputItem {
101+
case .message(let message):
102+
return message.content.compactMap { contentItem -> String? in
103+
switch contentItem {
104+
case .outputText(let outputText):
105+
return outputText.text
106+
}
107+
}.joined()
108+
default:
109+
return nil
110+
}
111+
}
112+
113+
return outputTextItems.isEmpty ? nil : outputTextItems.joined()
114+
}
115+
96116
public struct ErrorObject: Decodable {
97117

98118
/// The error code for the response.

0 commit comments

Comments
 (0)