FirebaseAILogic Framework Reference

GenerativeModelSession

public final class GenerativeModelSession : Sendable

A 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 Response containing the generated content as a String.

    Public Preview: This API is a public preview and may be subject to change.

    Throws

    A GenerationError if the model fails to generate a response.

    Declaration

    Swift

    @discardableResult
    func respond(to prompt: PartsRepresentable..., options: GenerationConfig? = nil) async throws
      -> GenerativeModelSession.Response<String>

    Parameters

    prompt

    The content to send to the model.

    options

    An optional GenerationConfig to override the model’s default generation configuration.

    Return Value

    A Response containing the generated content as a String.

  • Sends a new prompt to the model and returns a Response containing the generated content as a Generable type.

    Public Preview: This API is a public preview and may be subject to change.

    Throws

    A GenerationError if 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: Generable

    Parameters

    prompt

    The content to send to the model.

    type

    The Generable type to decode the response into.

    includeSchemaInPrompt

    Whether to include the schema in the prompt to the model.

    options

    An optional GenerationConfig to override the model’s default generation configuration.

    Return Value

    A Response containing the generated content as the specified Generable type.

  • Streams the model’s response as a Generable type.

    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: Generable

    Parameters

    prompt

    The content to send to the model.

    type

    The Generable type to decode the response into.

    includeSchemaInPrompt

    Whether to include the schema in the prompt to the model.

    options

    An optional GenerationConfig to override the model’s default generation configuration.

    Return Value

    A ResponseStream that 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

    prompt

    The content to send to the model.

    options

    An optional GenerationConfig to override the model’s default generation configuration.

    Return Value

    A ResponseStream that yields snapshots of the generated content.

  • The response from a respond call.

    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