FirebaseVertexAI Framework Reference

Schema

@available(iOS15.0,macOS12.0,tvOS15.0,watchOS8.0,*)publicfinalclassSchema:Sendable
extensionSchema:Encodable

ASchema 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 anOpenAPI 3.0 schema object.

  • Modifiers describing the expected format of a stringSchema.

    Declaration

    Swift

    @available(iOS15.0,macOS12.0,tvOS15.0,watchOS8.0,*)publicstructStringFormat:EncodableProtoEnum
  • Modifiers describing the expected format of an integerSchema.

    Declaration

    Swift

    @available(iOS15.0,macOS12.0,tvOS15.0,watchOS8.0,*)publicstructIntegerFormat:EncodableProtoEnum,Sendable
  • The data type.

    Declaration

    Swift

    publicvartype:String{get}
  • The format of the data.

    Declaration

    Swift

    publicletformat:String?
  • A human-readable explanation of the purpose of the schema or property. While not strictlyenforced on the value itself, good descriptions significantly help the model understand thecontext and generate more relevant and accurate output.

    Declaration

    Swift

    publicletdescription:String?
  • A human-readable name/summary for the schema or a specific property. This helps document theschema’s purpose but doesn’t typically constrain the generated value. It can subtly guide themodel by clarifying the intent of a field.

    Declaration

    Swift

    publiclettitle:String?
  • Indicates if the value may be null.

    Declaration

    Swift

    publicletnullable:Bool?
  • Possible values of the element of type “STRING” with “enum” format.

    Declaration

    Swift

    publicletenumValues:[String]?
  • Defines the schema for the elements within the"ARRAY". All items in the generated arraymust conform to this schema definition. This can be a simple type (like .string) or a complexnested object schema.

    Declaration

    Swift

    publicletitems:Schema?
  • An integer specifying the minimum number of items the generated"ARRAY" must contain.

    Declaration

    Swift

    publicletminItems:Int?
  • An integer specifying the maximum number of items the generated"ARRAY" must contain.

    Declaration

    Swift

    publicletmaxItems:Int?
  • The minimum value of a numeric type.

    Declaration

    Swift

    publicletminimum:Double?
  • The maximum value of a numeric type.

    Declaration

    Swift

    publicletmaximum:Double?
  • Defines the members (key-value pairs) expected within an object. It’s a dictionary where keysare the property names (strings) and values are nestedSchema definitions describing eachproperty’s type and constraints.

    Declaration

    Swift

    publicletproperties:[String:Schema]?
  • An array ofSchema objects. The generated data must be valid againstany (one or more)of the schemas listed in this array. This allows specifying multiple possible structures ortypes for a single field.

    For example, a value could be either aString or anInteger:

    Schema.anyOf(schemas:[.string(),.integer()])

    Declaration

    Swift

    publicletanyOf:[Schema]?
  • An array of strings, where each string is the name of a property defined in thepropertiesdictionary that must be present in the generated object. If a property is listed here, themodel must include it in the output.

    Declaration

    Swift

    publicletrequiredProperties:[String]?
  • A specific hint provided to the Gemini model, suggesting the order in which the keys shouldappear in the generated JSON string. Important: Standard JSON objects are inherently unorderedcollections of key-value pairs. While the model will try to respect propertyOrdering in itstextual JSON output, subsequent parsing into native Swift objects (like Dictionaries orStructs) might not preserve this order. This parameter primarily affects the raw JSON stringserialization.

    Declaration

    Swift

    publicletpropertyOrdering:[String]?
  • Returns aSchema representing a string value.

    This schema instructs the model to produce data of type"STRING", which is suitable fordecoding into a SwiftString (orString?, ifnullable is set totrue).

    Tip

    If a specific set of string values should be generated by the model (for example,“north”, “south”, “east”, or “west”), useenumeration(values:description:nullable:)instead to constrain the generated values.

    Declaration

    Swift

    publicstaticfuncstring(description:String?=nil,nullable:Bool=false,format:StringFormat?=nil)->Schema

    Parameters

    description

    An optional description of what the string should contain or represent; mayuse Markdown format.

    nullable

    Iftrue, instructs the model that itmay generatenull instead of astring; defaults tofalse, enforcing that a string value is generated.

    format

    An optional modifier describing the expected format of the string. Currently noformats are officially supported for strings but custom values may be specified usingcustom(_:), for example.custom("email") or.custom("byte"); theseprovide additional hints for how the model should respond but are not guaranteed to beadhered to.

  • Returns aSchema representing 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?, ifnullable is settotrue), or anenum with strings as raw values.

    Example:The values["north", "south", "east", "west"] for an enumeration of directions.

    enumDirection:String,Decodable{casenorth,south,east,west}

    Declaration

    Swift

    publicstaticfuncenumeration(values:[String],description:String?=nil,nullable:Bool=false)->Schema

    Parameters

    values

    The list of string values that may be generated by the model.

    description

    An optional description of what thevalues contain or represent; may useMarkdown format.

    nullable

    Iftrue, instructs the model that itmay generatenull instead of one ofthe strings specified invalues; defaults tofalse, enforcing that one of the stringvalues is generated.

  • Returns aSchema representing 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?, ifnullable isset totrue).

    Important

    ThisSchema provides a hint to the model that it should generate asingle-precision floating-point number, afloat, but only guarantees that the value willbe a number.

    Declaration

    Swift

    publicstaticfuncfloat(description:String?=nil,nullable:Bool=false,minimum:Float?=nil,maximum:Float?=nil)->Schema

    Parameters

    description

    An optional description of what the number should contain or represent; mayuse Markdown format.

    nullable

    Iftrue, instructs the model that it may generatenull instead of a number;defaults tofalse, enforcing that a number is generated.

    minimum

    If specified, instructs the model that the value should be greater than orequal to the specified minimum.

    maximum

    If specified, instructs the model that the value should be less than or equalto the specified maximum.

  • Returns aSchema representing a floating-point number.

    This schema instructs the model to produce data of type"NUMBER", which is suitable fordecoding into a SwiftDouble (orDouble?, ifnullable is set totrue).

    Declaration

    Swift

    publicstaticfuncdouble(description:String?=nil,nullable:Bool=false,minimum:Double?=nil,maximum:Double?=nil)->Schema

    Parameters

    description

    An optional description of what the number should contain or represent; mayuse Markdown format.

    nullable

    Iftrue, instructs the model that it may returnnull instead of a number;defaults tofalse, enforcing that a number is returned.

    minimum

    If specified, instructs the model that the value should be greater than orequal to the specified minimum.

    maximum

    If specified, instructs the model that the value should be less than or equalto the specified maximum.

  • Returns aSchema representing an integer value.

    This schema instructs the model to produce data of type"INTEGER", which is suitable fordecoding into a SwiftInt (orInt?, ifnullable is set totrue) or other integer types(such asInt32) based on the expected size of values being generated.

    Important

    If aformat ofint32 orint64 isspecified, this provides a hint to the model that it should generate 32-bit or 64-bitintegers but thisSchema only guarantees that the value will be an integer. Therefore, itispossible that decoding into anInt32 could overflow even if aformat ofint32 is specified.

    Declaration

    Swift

    publicstaticfuncinteger(description:String?=nil,nullable:Bool=false,format:IntegerFormat?=nil,minimum:Int?=nil,maximum:Int?=nil)->Schema

    Parameters

    description

    An optional description of what the integer should contain or represent; mayuse Markdown format.

    nullable

    Iftrue, instructs the model that it may returnnull instead of an integer;defaults tofalse, enforcing that an integer is returned.

    format

    An optional modifier describing the expected format of the integer. Currently theformatsint32 andint64 are supported; custom valuesmay be specified usingcustom(_:) but may be ignored by the model.

    minimum

    If specified, instructs the model that the value should be greater than orequal to the specified minimum.

    maximum

    If specified, instructs the model that the value should be less than or equalto the specified maximum.

  • Returns aSchema representing a boolean value.

    This schema instructs the model to produce data of type"BOOLEAN", which is suitable fordecoding into a SwiftBool (orBool?, ifnullable is set totrue).

    Declaration

    Swift

    publicstaticfuncboolean(description:String?=nil,nullable:Bool=false)->Schema

    Parameters

    description

    An optional description of what the boolean should contain or represent; mayuse Markdown format.

    nullable

    Iftrue, instructs the model that it may returnnull instead of a boolean;defaults tofalse, enforcing that a boolean is returned.

  • Returns aSchema representing an array.

    This schema instructs the model to produce data of type"ARRAY", which has elements of anyother data type (including nested"ARRAY"s). This data is suitable for decoding into manySwift collection types, includingArray, holding elements of types suitable for decodingfrom the respectiveitems type.

    Declaration

    Swift

    publicstaticfuncarray(items:Schema,description:String?=nil,nullable:Bool=false,minItems:Int?=nil,maxItems:Int?=nil)->Schema

    Parameters

    items

    TheSchema of the elements that the array will hold.

    description

    An optional description of what the array should contain or represent; mayuse Markdown format.

    nullable

    Iftrue, instructs the model that it may returnnull instead of an array;defaults tofalse, enforcing that an array is returned.

    minItems

    Instructs the model to produce at least the specified minimum number of elementsin the array; defaults tonil, meaning any number.

    maxItems

    Instructs the model to produce at most the specified maximum number of elementsin the array.

  • Returns aSchema representing 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 issuitable for decoding into Swift keyed collection types, includingDictionary, or othercustomstruct orclass types.

    Example: ACity could be represented with the following objectSchema.

    Schema.object(properties:["name":.string(),"population":.integer()])

    The generated data could be decoded into a Swift native type:

    structCity:Decodable{letname:Stringletpopulation:Int}

    Declaration

    Swift

    publicstaticfuncobject(properties:[String:Schema],optionalProperties:[String]=[],propertyOrdering:[String]?=nil,description:String?=nil,title:String?=nil,nullable:Bool=false)->Schema

    Parameters

    properties

    A dictionary containing the object’s property names as keys and theirrespectiveSchemas as values.

    optionalProperties

    A list of property names that may be be omitted in objects generatedby the model; these names must correspond to the keys provided in thepropertiesdictionary and may be an empty list.

    propertyOrdering

    An optional hint to the model suggesting the order for keys in thegenerated JSON string. SeepropertyOrdering for details.

    description

    An optional description of what the object should contain or represent; mayuse Markdown format.

    title

    An optional human-readable name/summary for the object schema.

    nullable

    Iftrue, instructs the model that it may returnnull instead of an object;defaults tofalse, enforcing that an object is returned.

  • Returns aSchema representing a value that must conform toany (one or more) of theprovided sub-schemas.

    This schema instructs the model to produce data that is valid against at least one of theschemas listed in theschemas array. This is useful when a field can accept multipledistinct types or structures.

    Example: A field that can hold either a simple user ID (integer) or a detailed userobject.

    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

    publicstaticfuncanyOf(schemas:[Schema])->Schema

    Parameters

    schemas

    An array ofSchema objects. The generated data must be valid against at leastone of these schemas. The array must not be empty.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-04-21 UTC.