FirebaseAI Framework Reference

LiveSession

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@available(watchOS, unavailable)
public final class LiveSession : Sendable

A live WebSocket session, capable of streaming content to and from the model.

Messages are streamed through responses, and can be sent through either the dedicated realtime API function (such as sendAudioRealtime(_:) and sendTextRealtime(_:)), or through the incremental API (such as sendContent(_:turnComplete:)).

To create an instance of this class, see LiveGenerativeModel.

  • An asynchronous stream of messages from the server.

    These messages from the incremental updates from the model, for the current conversation.

    Declaration

    Swift

    public var responses: AsyncThrowingStream<LiveServerMessage, Error> { get }
  • Response to a LiveServerToolCall received from the server.

    This method is used both for the realtime API and the incremental API.

    Declaration

    Swift

    public func sendFunctionResponses(_ responses: [FunctionResponsePart]) async

    Parameters

    responses

    Client generated function results, matched to their respective FunctionCallPart by the functionId field.

  • Sends an audio input stream to the model, using the realtime API.

    To learn more about audio formats, and the required state they should be provided in, see the docs on Supported audio formats.

    Declaration

    Swift

    public func sendAudioRealtime(_ audio: Data) async

    Parameters

    audio

    Raw 16-bit PCM audio at 16Hz, used to update the model on the client’s conversation.

  • Sends a text input stream to the model, using the realtime API.

    Declaration

    Swift

    public func sendTextRealtime(_ text: String) async

    Parameters

    text

    Text content to append to the current client’s conversation.

  • Incremental update of the current conversation.

    The content is unconditionally appended to the conversation history and used as part of the prompt to the model to generate content.

    Sending this message will also cause an interruption, if the server is actively generating content.

    Declaration

    Swift

    public func sendContent(_ content: [ModelContent], turnComplete: Bool = false) async

    Parameters

    content

    Content to append to the current conversation with the model.

    turnComplete

    Whether the server should start generating content with the currently accumulated prompt, or await additional messages before starting generation. By default, the server will await additional messages.

  • Incremental update of the current conversation.

    The content is unconditionally appended to the conversation history and used as part of the prompt to the model to generate content.

    Sending this message will also cause an interruption, if the server is actively generating content.

    Declaration

    Swift

    public func sendContent(_ parts: any PartsRepresentable...,
                            turnComplete: Bool = false) async

    Parameters

    content

    Content to append to the current conversation with the model (see PartsRepresentable for conforming types).

    turnComplete

    Whether the server should start generating content with the currently accumulated prompt, or await additional messages before starting generation. By default, the server will await additional messages.

  • Permanently stop the conversation with the model, and close the connection to the server

    This method will be called automatically when the LiveSession is deinitialized, but this method can be called manually to explicitly end the session.

    Attempting to receive content from a closed session will cause a LiveSessionUnexpectedClosureError error to be thrown.

    Declaration

    Swift

    public func close() async