Schema
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public final class Schema : Sendableextension Schema: EncodableA Schema object allows the definition of input and output data types.
These types can be objects, but also primitives and arrays. Represents a select subset of an OpenAPI 3.0 schema object.
-
Modifiers describing the expected format of a string
Schema.Declaration
Swift
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public struct StringFormat : EncodableProtoEnum -
Modifiers describing the expected format of an integer
Schema.Declaration
Swift
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) public struct IntegerFormat : EncodableProtoEnum, Sendable -
The data type.
Declaration
Swift
public var type: String { get } -
The format of the data.
Declaration
Swift
public let format: String? -
A human-readable explanation of the purpose of the schema or property. While not strictly enforced on the value itself, good descriptions significantly help the model understand the context and generate more relevant and accurate output.
Declaration
Swift
public let description: String? -
A human-readable name/summary for the schema or a specific property. This helps document the schema’s purpose but doesn’t typically constrain the generated value. It can subtly guide the model by clarifying the intent of a field.
Declaration
Swift
public let title: String? -
Indicates if the value may be null.
Declaration
Swift
public let nullable: Bool? -
Possible values of the element of type “STRING” with “enum” format.
Declaration
Swift
public let enumValues: [String]? -
Defines the schema for the elements within the
"ARRAY". All items in the generated array must conform to this schema definition. This can be a simple type (like .string) or a complex nested object schema.Declaration
Swift
public let items: Schema? -
An integer specifying the minimum number of items the generated
"ARRAY"must contain.Declaration
Swift
public let minItems: Int? -
An integer specifying the maximum number of items the generated
"ARRAY"must contain.Declaration
Swift
public let maxItems: Int? -
The minimum value of a numeric type.
Declaration
Swift
public let minimum: Double? -
The maximum value of a numeric type.
Declaration
Swift
public let maximum: Double? -
Defines the members (key-value pairs) expected within an object. It’s a dictionary where keys are the property names (strings) and values are nested
Schemadefinitions describing each property’s type and constraints.Declaration
Swift
public let properties: [String : Schema]? -
An array of
Schemaobjects. The generated data must be valid against any (one or more) of the schemas listed in this array. This allows specifying multiple possible structures or types for a single field.For example, a value could be either a
Stringor anInteger:Schema.anyOf(schemas: [.string(), .integer()])Declaration
Swift
public let anyOf: [Schema]? -
An array of strings, where each string is the name of a property defined in the
propertiesdictionary that must be present in the generated object. If a property is listed here, the model must include it in the output.Declaration
Swift
public let requiredProperties: [String]? -
A specific hint provided to the Gemini model, suggesting the order in which the keys should appear in the generated JSON string. Important: Standard JSON objects are inherently unordered collections of key-value pairs. While the model will try to respect propertyOrdering in its textual JSON output, subsequent parsing into native Swift objects (like Dictionaries or Structs) might not preserve this order. This parameter primarily affects the raw JSON string serialization.
Declaration
Swift
public let propertyOrdering: [String]? -
Returns a
Schemarepresenting a string value.This schema instructs the model to produce data of type
"STRING", which is suitable for decoding into a SwiftString(orString?, ifnullableis set totrue).Tip
If a specific set of string values should be generated by the model (for example, “north”, “south”, “east”, or “west”), use
enumeration(values:description:nullable:)instead to constrain the generated values.Declaration
Swift
public static func string(description: String? = nil, title: String? = nil, nullable: Bool = false, format: StringFormat? = nil) -> SchemaParameters
descriptionAn optional description of what the string should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may generatenullinstead of a string; defaults tofalse, enforcing that a string value is generated.formatAn optional modifier describing the expected format of the string. Currently no formats are officially supported for strings but custom values may be specified using
custom(_:), for example.custom("email")or.custom("byte"); these provide additional hints for how the model should respond but are not guaranteed to be adhered to. -
Returns a
Schemarepresenting an enumeration of string values.This schema instructs the model to produce data of type
"STRING"with theformat"enum". This data is suitable for decoding into a SwiftString(orString?, ifnullableis set totrue), or anenumwith strings as raw values.Example: The values
["north", "south", "east", "west"]for an enumeration of directions.enum Direction: String, Decodable { case north, south, east, west }Declaration
Swift
public static func enumeration(values: [String], description: String? = nil, title: String? = nil, nullable: Bool = false) -> SchemaParameters
valuesThe list of string values that may be generated by the model.
descriptionAn optional description of what the
valuescontain or represent; may use Markdown format.titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may generatenullinstead of one of the strings specified invalues; defaults tofalse, enforcing that one of the string values is generated. -
Returns a
Schemarepresenting a single-precision floating-point number.This schema instructs the model to produce data of type
"NUMBER"with theformat"float", which is suitable for decoding into a SwiftFloat(orFloat?, ifnullableis set totrue).Important
This
Schemaprovides a hint to the model that it should generate a single-precision floating-point number, afloat, but only guarantees that the value will be a number.Declaration
Swift
public static func float(description: String? = nil, title: String? = nil, nullable: Bool = false, minimum: Float? = nil, maximum: Float? = nil) -> SchemaParameters
descriptionAn optional description of what the number should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may generatenullinstead of a number; defaults tofalse, enforcing that a number is generated.minimumIf specified, instructs the model that the value should be greater than or equal to the specified minimum.
maximumIf specified, instructs the model that the value should be less than or equal to the specified maximum.
-
Returns a
Schemarepresenting a floating-point number.This schema instructs the model to produce data of type
"NUMBER", which is suitable for decoding into a SwiftDouble(orDouble?, ifnullableis set totrue).Declaration
Swift
public static func double(description: String? = nil, title: String? = nil, nullable: Bool = false, minimum: Double? = nil, maximum: Double? = nil) -> SchemaParameters
descriptionAn optional description of what the number should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may returnnullinstead of a number; defaults tofalse, enforcing that a number is returned.minimumIf specified, instructs the model that the value should be greater than or equal to the specified minimum.
maximumIf specified, instructs the model that the value should be less than or equal to the specified maximum.
-
Returns a
Schemarepresenting an integer value.This schema instructs the model to produce data of type
"INTEGER", which is suitable for decoding into a SwiftInt(orInt?, ifnullableis set totrue) or other integer types (such asInt32) based on the expected size of values being generated.Important
If a
formatofint32orint64is specified, this provides a hint to the model that it should generate 32-bit or 64-bit integers but thisSchemaonly guarantees that the value will be an integer. Therefore, it is possible that decoding into anInt32could overflow even if aformatofint32is specified.Declaration
Swift
public static func integer(description: String? = nil, title: String? = nil, nullable: Bool = false, format: IntegerFormat? = nil, minimum: Int? = nil, maximum: Int? = nil) -> SchemaParameters
descriptionAn optional description of what the integer should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may returnnullinstead of an integer; defaults tofalse, enforcing that an integer is returned.formatAn optional modifier describing the expected format of the integer. Currently the formats
int32andint64are supported; custom values may be specified usingcustom(_:)but may be ignored by the model.minimumIf specified, instructs the model that the value should be greater than or equal to the specified minimum.
maximumIf specified, instructs the model that the value should be less than or equal to the specified maximum.
-
Returns a
Schemarepresenting a boolean value.This schema instructs the model to produce data of type
"BOOLEAN", which is suitable for decoding into a SwiftBool(orBool?, ifnullableis set totrue).Declaration
Swift
public static func boolean(description: String? = nil, title: String? = nil, nullable: Bool = false) -> SchemaParameters
descriptionAn optional description of what the boolean should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may returnnullinstead of a boolean; defaults tofalse, enforcing that a boolean is returned. -
Returns a
Schemarepresenting an array.This schema instructs the model to produce data of type
"ARRAY", which has elements of any other data type (including nested"ARRAY"s). This data is suitable for decoding into many Swift collection types, includingArray, holding elements of types suitable for decoding from the respectiveitemstype.Declaration
Swift
public static func array(items: Schema, description: String? = nil, title: String? = nil, nullable: Bool = false, minItems: Int? = nil, maxItems: Int? = nil) -> SchemaParameters
itemsThe
Schemaof the elements that the array will hold.descriptionAn optional description of what the array should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may returnnullinstead of an array; defaults tofalse, enforcing that an array is returned.minItemsInstructs the model to produce at least the specified minimum number of elements in the array; defaults to
nil, meaning any number.maxItemsInstructs the model to produce at most the specified maximum number of elements in the array.
-
Returns a
Schemarepresenting an object.This schema instructs the model to produce data of type
"OBJECT", which has keys of type"STRING"and values of any other data type (including nested"OBJECT"s). This data is suitable for decoding into Swift keyed collection types, includingDictionary, or other customstructorclasstypes.Example: A
Citycould be represented with the following objectSchema.Schema.object(properties: [ "name" : .string(), "population": .integer() ])The generated data could be decoded into a Swift native type:
struct City: Decodable { let name: String let population: Int }Declaration
Swift
public static func object(properties: [String: Schema], optionalProperties: [String] = [], propertyOrdering: [String]? = nil, description: String? = nil, title: String? = nil, nullable: Bool = false) -> SchemaParameters
propertiesA dictionary containing the object’s property names as keys and their respective
Schemas as values.optionalPropertiesA list of property names that may be be omitted in objects generated by the model; these names must correspond to the keys provided in the
propertiesdictionary and may be an empty list.propertyOrderingAn optional hint to the model suggesting the order for keys in the generated JSON string. See
propertyOrderingfor details.descriptionAn optional description of what the object should contain or represent; may use Markdown format.
titleAn optional human-readable name/summary for the schema.
nullableIf
true, instructs the model that it may returnnullinstead of an object; defaults tofalse, enforcing that an object is returned. -
Returns a
Schemarepresenting a value that must conform to any (one or more) of the provided sub-schemas.This schema instructs the model to produce data that is valid against at least one of the schemas listed in the
schemasarray. This is useful when a field can accept multiple distinct types or structures.Example: A field that can hold either a simple user ID (integer) or a detailed user object.
Schema.anyOf(schemas: [ .integer(description: "User ID"), .object(properties: [ "userId": .integer(), "userName": .string() ], description: "Detailed User Object") ])The generated data could be decoded based on which schema it matches.
Declaration
Swift
public static func anyOf(schemas: [Schema]) -> SchemaParameters
schemasAn array of
Schemaobjects. The generated data must be valid against at least one of these schemas. The array must not be empty.