Swift Macro for OpenAI API Structured Outputs.
Just attach OpenAIStructure Macro to your struct, and provide description for every fields using Field Macro.
@OpenAIStructure(name: "talk_theme")
struct TalkTheme {
@Field("Theme title")
var title: String
@Field("Theme overview")
var overview: String
@Field("Question blocks")
var questionBlocks: [QuestionBlock]
}
@OpenAIStructure(name: "question_block")
struct QuestionBlock {
@Field("Block label")
var label: String
@Field("List of questions")
var questions: [String]
}
let result: TalkThemePack = try await OpenAIRequest.request(
input: "Talk theme related to food.",
instructions: "Provide some talk themes.",
model: .o4_mini(reasoningEffort: .low),
object: TalkTheme.self,
apiKey: "<your_api_key>"
)
Optimised for the Responses API with one prompt
in, one structured object
out.
Supports String, Bool, Double, [String], [Bool], [Double], CustomStruct, [CustomStruct]