swift-generation

0.2.0

Structured output generation for Swift. Define types that language models can generate with compile-time schema validation.
1amageek/swift-generation

What's New

0.2.0

2026-03-26T08:46:53Z

swift-generation 0.2.0

Breaking Change

  • GenerationMacros module has been merged into Generation
  • Users now only need import Generation to access all types, protocols, and macros (@Generable, @Guide)
  • The GenerationMacros product has been removed

Migration

- import Generation
- import GenerationMacros
+ import Generation

swift-generation

Structured output generation for Swift. Define types that language models can generate with compile-time schema validation.

Overview

swift-generation provides the core protocols, types, and macros for structured generation from language models. Use @Generable to make your Swift types generatable with schema constraints enforced at compile time.

import Generation

@Generable(description: "A movie review")
struct MovieReview {
    @Guide(description: "Movie title")
    var title: String

    @Guide(description: "Rating", .range(1...5))
    var rating: Int

    @Guide(description: "Brief summary")
    var summary: String
}

The @Generable macro automatically conforms your type to the Generable protocol, generating:

  • GenerationSchema for constrained sampling
  • init(_ content: GeneratedContent) for parsing model output
  • var generatedContent: GeneratedContent for serialization
  • PartiallyGenerated type for streaming support

Requirements

  • Swift 6.2+
  • macOS 15+ / iOS 18+ / macCatalyst 18+ / visionOS 2+

Installation

Add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/1amageek/swift-generation.git", from: "0.1.0")
]

Then add the targets to your module:

.target(
    name: "YourTarget",
    dependencies: [
        .product(name: "Generation", package: "swift-generation"),
    ]
)

Core Types

Generable Protocol

protocol Generable: ConvertibleFromGeneratedContent, ConvertibleToGeneratedContent {
    associatedtype PartiallyGenerated: ConvertibleFromGeneratedContent = Self
    static var generationSchema: GenerationSchema { get }
}

GeneratedContent

A JSON-like value type representing model output:

enum Kind {
    case null
    case bool(Bool)
    case number(Double)
    case string(String)
    case array([GeneratedContent])
    case structure(properties: [String: GeneratedContent], orderedKeys: [String])
}

GenerationSchema

Defines the structure and constraints for generation. Supports objects, arrays, enums, and dictionary types.

@Guide Constraints

@Guide(description: "Age", .range(0...120))
var age: Int

@Guide(description: "Tags", .minimumCount(1), .maximumCount(5))
var tags: [String]

@Guide(description: "Status", .anyOf(["active", "inactive"]))
var status: String

Built-in Conformances

String, Bool, Int, Float, Double, Decimal, UUID, Date, URL, Array, Optional, and Dictionary conform to Generable out of the box.

License

MIT

Description

  • Swift Tools 6.2.0
View More Packages from this Author

Dependencies

Last updated: Sun Mar 29 2026 02:56:29 GMT-0900 (Hawaii-Aleutian Daylight Time)