GenerativeModelSession
public final class GenerativeModelSession : SendableA session that handles multi-turn interactions with a generative model, similar to Chat.
A GenerativeModelSession retains history between requests. For single-turn requests to a
model, use generativeModelSession(model:instructions:) to start a new session.
GenerativeModelSession is particularly useful for generating structured data.
Public Preview: This API is a public preview and may be subject to change.
Example usage:
@Generable
struct UserProfile {
@Guide(description: "A unique username for the user.")
var username: String
@Guide(description: "A short bio about the user, no more than 100 characters.")
var bio: String
@Guide(description: "A list of the user's favorite topics.", .count(3))
var favoriteTopics: [String]
}
let firebaseAI = // ... a `FirebaseAI` instance
let session = firebaseAI.generativeModelSession(model: "gemini-model-name")
let prompt = "Generate a user profile for a cat lover who enjoys hiking."
let response = try await session.respond(to: prompt, generating: UserProfile.self)
print("Username: \(response.content.username)")
print("Bio: \(response.content.bio)")
print("Favorite Topics: \(response.content.favoriteTopics.joined(separator: ", "))")
-
Sends a new prompt to the model and returns a
Responsecontaining the generated content as aString.Public Preview: This API is a public preview and may be subject to change.
Throws
AGenerationErrorif the model fails to generate a response.Declaration
Swift
@discardableResult func respond(to prompt: PartsRepresentable..., options: GenerationConfig? = nil) async throws -> GenerativeModelSession.Response<String>Parameters
promptThe content to send to the model.
optionsAn optional
GenerationConfigto override the model’s default generation configuration.Return Value
A
Responsecontaining the generated content as aString. -
Sends a new prompt to the model and returns a
Responsecontaining the generated content as aGenerabletype.Public Preview: This API is a public preview and may be subject to change.
Throws
AGenerationErrorif the model fails to generate a response.Declaration
Swift
@available(iOS 26.0, macOS 26.0, *) @available(tvOS, unavailable) @available(watchOS, unavailable) @discardableResult func respond<Content>(to prompt: PartsRepresentable..., generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationConfig? = nil) async throws -> GenerativeModelSession.Response<Content> where Content: GenerableParameters
promptThe content to send to the model.
typeThe
Generabletype to decode the response into.includeSchemaInPromptWhether to include the schema in the prompt to the model.
optionsAn optional
GenerationConfigto override the model’s default generation configuration.Return Value
A
Responsecontaining the generated content as the specifiedGenerabletype. -
Streams the model’s response as a
Generabletype.Public Preview: This API is a public preview and may be subject to change.
Declaration
Swift
@available(iOS 26.0, macOS 26.0, *) @available(tvOS, unavailable) @available(watchOS, unavailable) public func streamResponse<Content>(to prompt: PartsRepresentable..., generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationConfig? = nil) -> sending GenerativeModelSession.ResponseStream<Content, Content.PartiallyGenerated> where Content: GenerableParameters
promptThe content to send to the model.
typeThe
Generabletype to decode the response into.includeSchemaInPromptWhether to include the schema in the prompt to the model.
optionsAn optional
GenerationConfigto override the model’s default generation configuration.Return Value
A
ResponseStreamthat yields snapshots of the generated content. -
Streams the model’s response as a
String.Public Preview: This API is a public preview and may be subject to change.
Declaration
Swift
@available(macOS 12.0, watchOS 8.0, *) public func streamResponse(to prompt: PartsRepresentable..., options: GenerationConfig? = nil) -> sending GenerativeModelSession.ResponseStream<String, String>Parameters
promptThe content to send to the model.
optionsAn optional
GenerationConfigto override the model’s default generation configuration.Return Value
A
ResponseStreamthat yields snapshots of the generated content.
-
The response from a
respondcall.Declaration
Swift
struct Response<Content> -
An asynchronous sequence of snapshots of the model’s response.
Declaration
Swift
struct ResponseStream<Content, PartialContent> : AsyncSequence -
An error that occurs during content generation.
Declaration
Swift
public enum GenerationError : Error, LocalizedError