generateObjectArray

Generate a typed array, validating each element against a schema.

Like generateObject, but the schema describes one element and the result is an array of them.

Use it for list extraction where the element shape is known and the count is not: pulling every line item off an invoice, every attendee out of an email.

let result = try await generateObjectArray(
  model: OpenAIModel("gpt-5.1"),
  of: Attendee.self,
  elementSchema: Schema.object([
    "name": .string(), "email": .string()
  ]).jsonSchema,
  prompt: email
)

Signature

func generateObjectArray<T: Decodable & Sendable>(
    model: any LanguageModel,
    of type: T.Type = T.self,
    elementSchema: JSONValue,
    schemaName: String = "elements",
    schemaDescription: String? = nil,
    messages: [Message] = [],
    system: String? = nil,
    prompt: String? = nil,
    maxOutputTokens: Int = 1024,
    temperature: Double? = nil,
    providerOptions: JSONValue? = nil,
    maxRetries: Int = 2,
    repairText: (@Sendable (String, any Error) async -> String?)? = nil
) async throws -> GenerateObjectResult<[T]>

Defined in Sources/AI/Core/GenerateObject.swift.

Parameters

ParameterTypeDefault
modelany LanguageModelrequired
ofT.TypeT.self
elementSchemaJSONValuerequired
schemaNameString"elements"
schemaDescriptionString?nil
messages[Message][]
systemString?nil
promptString?nil
maxOutputTokensInt1024
temperatureDouble?nil
providerOptionsJSONValue?nil
maxRetriesInt2
repairText(@Sendable (String, any Error) async -> String?)?nil

Returns

GenerateObjectResult<[T]>, with the array in object.

See also