Skip to content

yukiny0811/OpenAIStructure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAIStructure

Release Swift Compatibility Platform Compatibility License

Swift Macro for OpenAI API Structured Outputs.

Usage

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>"
)

Notice

Optimised for the Responses API with one prompt in, one structured object out.

Supports String, Bool, Double, [String], [Bool], [Double], CustomStruct, [CustomStruct]