One Logo Pool Ball
FoundationModels · IOS API REFERENCE

LanguageModelSession

class · iOS 26.0+ · SDK 27.1

final class LanguageModelSession

An object that represents a session that interacts with a language model. A session is a single context that you use to generate content with, and maintains state between requests. You can reuse the existing instance or create a new one each time you call the model. When you create a session you can provide instructions that tells the model what its role is and provides guidance on how to respond. ```swift let session = LanguageModelSession(instructions: """ You are a motivational workout coach that provides quotes to inspire \ and motivate athletes. """ ) let prompt = "Generate a motivational quote for my next workout." let response = try await session.respond(to: prompt) ``` The framework records each call to the model in a ``Transcript`` that includes all prompts and responses. If your session exceeds the available context size, it throws ``LanguageModelError/contextSizeExceeded(_:)``. For more information on managing the context window size, see <doc:managing-the-context-window>. Use Instruments to analyze token consumption while your app is running and to look for opportunities to improve performance, like with ``prewarm(promptPrefix:)``. For more information on Instruments, see <doc:analyzing-the-runtime-performance-of-your-foundation-models-app>.

Members (44)

get · iOS 27.0+

properties

TypeScript
get properties(): SessionPropertyValues
Swift declaration
final var properties: SessionPropertyValues { get }
get · iOS 26.0+

transcript

TypeScript
get transcript(): Transcript
Swift declaration
final var transcript: Transcript { get set }

A full history of interactions, including user inputs and model responses.

get · iOS 26.0+

isResponding

TypeScript
get isResponding(): boolean
Swift declaration
final var isResponding: Bool { get }

A Boolean value that indicates whether a response is being generated. - Important: You should not call any of the respond methods while this property is `true`. Disable buttons and other interactions to prevent users from submitting a second prompt while the model is responding to their first prompt. ```swift struct ShopView: View { @State var session = LanguageModelSession() @State var joke = "" var body: some View { Text(joke) Button("Generate joke") { Task { assert(!session.isResponding, "It should not be possible to tap this button while the model is responding") joke = try await session.respond(to: "Tell me a joke").content } } .disabled(session.isResponding) // Prevent concurrent calls to respond } } ```

method · iOS 26.0+

streamResponse

TypeScript
streamResponseIncludeSchemaInPromptOptionsPrompt(includeSchemaInPrompt: boolean, options: GenerationOptions, prompt: (() => Prompt)): LanguageModelSession_ResponseStream<number>
Swift declaration
final func streamResponse<Content>(generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions(), @PromptBuilder prompt: () throws -> Prompt) rethrows -> sending LanguageModelSession.ResponseStream<Content> where Content : Generable

Produces a response stream to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseGeneratingOptionsContextOptionsMetadataPrompt(type: "Bool" | "Int" | "Double" | "Float" | "String" | "Foundation.Decimal" | "FoundationModels.GeneratedContent" | "FoundationModels.ImageReference", options: GenerationOptions, contextOptions: ContextOptions, metadata: Record<string, SDKHandle>, prompt: (() => Prompt)): LanguageModelSession_ResponseStream<unknown>
Swift declaration
final func streamResponse<Content>(generating type: Content.Type = Content.self, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:], @PromptBuilder prompt: () throws -> Prompt) rethrows -> sending LanguageModelSession.ResponseStream<Content> where Content : Generable

Produces a response stream to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseToIncludeSchemaInPromptOptions(prompt: Prompt, includeSchemaInPrompt?: boolean, options?: GenerationOptions): LanguageModelSession_ResponseStream<number>
Swift declaration
final func streamResponse<Content>(to prompt: Prompt, generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) -> sending LanguageModelSession.ResponseStream<Content> where Content : Generable

Produces a response stream to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseToIncludeSchemaInPromptOptions49503(prompt: string, includeSchemaInPrompt?: boolean, options?: GenerationOptions): LanguageModelSession_ResponseStream<number>
Swift declaration
final func streamResponse<Content>(to prompt: String, generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) -> sending LanguageModelSession.ResponseStream<Content> where Content : Generable

Produces a response stream to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseToGeneratingOptionsContextOptionsMetadata(prompt: Prompt, type?: "Bool" | "Int" | "Double" | "Float" | "String" | "Foundation.Decimal" | "FoundationModels.GeneratedContent" | "FoundationModels.ImageReference", options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): LanguageModelSession_ResponseStream<unknown>
Swift declaration
final func streamResponse<Content>(to prompt: Prompt, generating type: Content.Type = Content.self, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) -> sending LanguageModelSession.ResponseStream<Content> where Content : Generable

Produces a response stream to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseToGeneratingOptionsContextOptionsMetadata69944(prompt: string, type?: "Bool" | "Int" | "Double" | "Float" | "String" | "Foundation.Decimal" | "FoundationModels.GeneratedContent" | "FoundationModels.ImageReference", options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): LanguageModelSession_ResponseStream<unknown>
Swift declaration
final func streamResponse<Content>(to prompt: String, generating type: Content.Type = Content.self, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) -> sending LanguageModelSession.ResponseStream<Content> where Content : Generable

Produces a response stream to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseToSchemaIncludeSchemaInPromptOptions(prompt: Prompt, schema: GenerationSchema, includeSchemaInPrompt?: boolean, options?: GenerationOptions): LanguageModelSession_ResponseStream<GeneratedContent>
Swift declaration
final func streamResponse(to prompt: Prompt, schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) -> sending LanguageModelSession.ResponseStream<GeneratedContent>

Produces a response stream to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseToSchemaIncludeSchemaInPromptOptions56182(prompt: string, schema: GenerationSchema, includeSchemaInPrompt?: boolean, options?: GenerationOptions): LanguageModelSession_ResponseStream<GeneratedContent>
Swift declaration
final func streamResponse(to prompt: String, schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) -> sending LanguageModelSession.ResponseStream<GeneratedContent>

Produces a response stream to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseToSchemaOptionsContextOptionsMetadata(prompt: Prompt, schema: GenerationSchema, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): LanguageModelSession_ResponseStream<GeneratedContent>
Swift declaration
final func streamResponse(to prompt: Prompt, schema: GenerationSchema, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) -> sending LanguageModelSession.ResponseStream<GeneratedContent>

Produces a response stream to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseToSchemaOptionsContextOptionsMetadata77454(prompt: string, schema: GenerationSchema, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): LanguageModelSession_ResponseStream<GeneratedContent>
Swift declaration
final func streamResponse(to prompt: String, schema: GenerationSchema, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) -> sending LanguageModelSession.ResponseStream<GeneratedContent>

Produces a response stream to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseToOptionsContextOptionsMetadata(prompt: Prompt, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): LanguageModelSession_ResponseStream<string>
Swift declaration
final func streamResponse(to prompt: Prompt, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(), metadata: [String : any ConvertibleToGeneratedContent] = [:]) -> sending LanguageModelSession.ResponseStream<String>

Produces a response stream to a prompt. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces aggregated tokens.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseToOptionsContextOptionsMetadata41858(prompt: string, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): LanguageModelSession_ResponseStream<string>
Swift declaration
final func streamResponse(to prompt: String, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(), metadata: [String : any ConvertibleToGeneratedContent] = [:]) -> sending LanguageModelSession.ResponseStream<String>

Produces a response stream to a prompt. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces aggregated tokens.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseToOptions(prompt: Prompt, options?: GenerationOptions): LanguageModelSession_ResponseStream<string>
Swift declaration
final func streamResponse(to prompt: Prompt, options: GenerationOptions = GenerationOptions()) -> sending LanguageModelSession.ResponseStream<String>

Produces a response stream to a prompt. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces aggregated tokens.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseToOptions65097(prompt: string, options?: GenerationOptions): LanguageModelSession_ResponseStream<string>
Swift declaration
final func streamResponse(to prompt: String, options: GenerationOptions = GenerationOptions()) -> sending LanguageModelSession.ResponseStream<String>

Produces a response stream to a prompt. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces aggregated tokens.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseSchemaIncludeSchemaInPromptOptionsPrompt(schema: GenerationSchema, includeSchemaInPrompt: boolean, options: GenerationOptions, prompt: (() => Prompt)): LanguageModelSession_ResponseStream<GeneratedContent>
Swift declaration
final func streamResponse(schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions(), @PromptBuilder prompt: () throws -> Prompt) rethrows -> sending LanguageModelSession.ResponseStream<GeneratedContent>

Produces a response stream to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseSchemaOptionsContextOptionsMetadataPrompt(schema: GenerationSchema, options: GenerationOptions, contextOptions: ContextOptions, metadata: Record<string, SDKHandle>, prompt: (() => Prompt)): LanguageModelSession_ResponseStream<GeneratedContent>
Swift declaration
final func streamResponse(schema: GenerationSchema, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:], @PromptBuilder prompt: () throws -> Prompt) rethrows -> sending LanguageModelSession.ResponseStream<GeneratedContent>

Produces a response stream to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

streamResponse

TypeScript
streamResponseOptionsContextOptionsMetadataPrompt(options: GenerationOptions, contextOptions: ContextOptions, metadata: Record<string, SDKHandle>, prompt: (() => Prompt)): LanguageModelSession_ResponseStream<string>
Swift declaration
final func streamResponse(options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(), metadata: [String : any ConvertibleToGeneratedContent] = [:], @PromptBuilder prompt: () throws -> Prompt) rethrows -> sending LanguageModelSession.ResponseStream<String>

Produces a response stream to a prompt. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A response stream that produces aggregated tokens.

method · iOS 26.0+

streamResponse

TypeScript
streamResponseOptionsPrompt(options: GenerationOptions, prompt: (() => Prompt)): LanguageModelSession_ResponseStream<string>
Swift declaration
final func streamResponse(options: GenerationOptions = GenerationOptions(), @PromptBuilder prompt: () throws -> Prompt) rethrows -> sending LanguageModelSession.ResponseStream<String>

Produces a response stream to a prompt. - Important: If running in the background, use the non-streaming ``LanguageModelSession/respond(to:options:)-(Prompt,_)`` method to reduce the likelihood of encountering ``LanguageModelError/rateLimited(_:)`` errors. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A response stream that produces aggregated tokens.

method · iOS 26.0+

logFeedbackAttachment

TypeScript
logFeedbackAttachmentSentimentIssuesDesiredOutput(sentiment?: (LanguageModelFeedback_Sentiment) | null, issues?: Array<LanguageModelFeedback_Issue>, desiredOutput?: (Transcript_Entry) | null): ArrayBuffer
Swift declaration
@discardableResult final func logFeedbackAttachment(sentiment: LanguageModelFeedback.Sentiment?, issues: [LanguageModelFeedback.Issue] = [], desiredOutput: Transcript.Entry? = nil) -> Data

Logs and serializes a feedback attachment that can be submitted to Apple. This method creates a structured feedback attachment containing the session's transcript and any provided feedback information. The attachment can be saved to a file and submitted to Apple using [Feedback Assistant](https://feedbackassistant.apple.com). If an error occurred during a previous response, any rejected entries that were rolled back from the transcript are included in the feedback data. ```swift let session = LanguageModelSession() let response = try await session.respond(to: "What is the capital of France?") // Create feedback for a helpful response let feedbackData = session.logFeedbackAttachment(sentiment: .positive) // Or create feedback for a problematic response let feedbackData = session.logFeedbackAttachment( sentiment: .negative, issues: [ LanguageModelFeedback.Issue( category: .incorrect, explanation: "The model provided outdated information" ) ], desiredOutput: Transcript.Entry.response(...) ) ``` If your `desiredOutput` is a string, use ``Transcript/Entry/response(_:)`` to turn your desired output into a ``Transcript`` entry: ```swift let text = Transcript.TextSegment(content: "The capital of France is Paris.") let segment = Transcript.Segment.text(text) let response = Transcript.Response(segments: [segment]) let entry = Transcript.Entry.response(response) ``` If your `desiredOutput` is a ``Generable`` type, turning that into a ``Transcript`` entry is slightly different: ```swift let customType = MyCustomType(...) // A generable type. let structure = Transcript.StructuredSegment(schemaName: String(describing: Foo.self), content: customType.generatedContent) let segment = Transcript.Segment.structure(structure) let response = Transcript.Response(segments: [segment]) let entry = Transcript.Entry.response(response) ``` Finally, if you'd like to submit the feedback to Apple, write your feedback to a `.json` file and include the file as an attachment to [Feedback Assistant](https://feedbackassistant.apple.com). You can include one or many feedback attachment in the same file: ```swift let allFeedback = feedbackData + feedbackData2 + feedbackData3 let url = URL(fileURLWithPath: "path/to/save/feedback.json") try allFeedback.write(to: url) ``` - Parameters: - sentiment: An optional sentiment rating about the model's output (positive, negative, or neutral). - issues: An array of specific issues identified with the model's response. Defaults to an empty array. - desiredOutput: An optional transcript entry showing what the desired output should have been. - Returns: A `Data` object containing the JSON-encoded feedback attachment that can be submitted to Feedback Assistant.

method · iOS 26.0+

logFeedbackAttachment

TypeScript
logFeedbackAttachmentSentimentIssuesDesiredResponseText(sentiment?: (LanguageModelFeedback_Sentiment) | null, issues?: Array<LanguageModelFeedback_Issue>, desiredResponseText?: (string) | null): ArrayBuffer
Swift declaration
@backDeployed(before: iOS 26.1, macOS 26.1, visionOS 26.1)
@discardableResult final func logFeedbackAttachment(sentiment: LanguageModelFeedback.Sentiment?, issues: [LanguageModelFeedback.Issue] = [], desiredResponseText: String?) -> Data
method · iOS 26.0+

logFeedbackAttachment

TypeScript
logFeedbackAttachmentSentimentIssuesDesiredResponseContent(sentiment?: (LanguageModelFeedback_Sentiment) | null, issues?: Array<LanguageModelFeedback_Issue>, desiredResponseContent?: (SDKHandle) | null): ArrayBuffer
Swift declaration
@backDeployed(before: iOS 26.1, macOS 26.1, visionOS 26.1)
@discardableResult final func logFeedbackAttachment(sentiment: LanguageModelFeedback.Sentiment?, issues: [LanguageModelFeedback.Issue] = [], desiredResponseContent: (any ConvertibleToGeneratedContent)?) -> Data
get · iOS 27.0+

transcriptErrorHandlingPolicy

TypeScript
get transcriptErrorHandlingPolicy(): (TranscriptErrorHandlingPolicy) | null
Swift declaration
final var transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? { get set }

The session's policy for managing the transcript when errors occur.

set · iOS 27.0+

transcriptErrorHandlingPolicy

TypeScript
set transcriptErrorHandlingPolicy(value: (TranscriptErrorHandlingPolicy) | null)
Swift declaration
final var transcriptErrorHandlingPolicy: TranscriptErrorHandlingPolicy? { get set }

The session's policy for managing the transcript when errors occur.

init · iOS 26.0+

init

TypeScript
static initModelToolsTranscript(model: SystemLanguageModel, tools: Array<SDKHandle>, transcript: Transcript): LanguageModelSession
Swift declaration
convenience init(model: SystemLanguageModel = .default, tools: [any Tool] = [], transcript: Transcript)

Creates a session by rehydrating from a transcript. - Parameters - model: The language model to use for this session. - transcript: A transcript to resume from. - tools: Tools to make available to the model for this session.

init · iOS 26.0+

init

TypeScript
static initModelToolsInstructions(model?: SystemLanguageModel, tools?: Array<SDKHandle>, instructions?: (Instructions) | null): LanguageModelSession
Swift declaration
convenience init(model: SystemLanguageModel = .default, tools: [any Tool] = [], instructions: Instructions? = nil)

Creates a session in a blank slate state with instructions. - Parameters - model: The language model to use for this session. - tools: Tools to make available to the model for this session. - instructions: Instructions that control the model's behavior.

init · iOS 26.0+

init

TypeScript
static initModelToolsInstructions11508(model: SystemLanguageModel, tools: Array<SDKHandle>, instructions: (() => Instructions)): LanguageModelSession
Swift declaration
convenience init(model: SystemLanguageModel = .default, tools: [any Tool] = [], @InstructionsBuilder instructions: () throws -> Instructions) rethrows

Creates a session in a blank slate state with an instructions builder. - Parameters - model: The language model to use for this session. - tools: Tools to make available to the model for this session. - instructions: Instructions that control the model's behavior.

init · iOS 26.0+

init

TypeScript
static initModelToolsInstructions81584(model?: SystemLanguageModel, tools?: Array<SDKHandle>, instructions?: (string) | null): LanguageModelSession
Swift declaration
convenience init(model: SystemLanguageModel = .default, tools: [any Tool] = [], instructions: String? = nil)

Creates a session in a blank slate state with string-based instructions. - Parameters - model: The language model to use for this session. - tools: Tools to make available to the model for this session. - instructions: Instructions that control the model's behavior.

get · iOS 27.0+

usage

TypeScript
get usage(): LanguageModelSession_Usage
Swift declaration
final var usage: LanguageModelSession.Usage { get }

The total accumulated usage across all responses generated by this session. This value increases monotonically over the lifetime of the session.

method · iOS 26.0+

prewarm

TypeScript
prewarm(promptPrefix?: (Prompt) | null): void
Swift declaration
final func prewarm(promptPrefix: Prompt? = nil)

Loads the resources required for this session into memory ahead of a request. This method can be useful in cases where you have a strong signal that the user will interact with session within a few seconds. For example, you might call `prewarm(promptPrefix:)` when a person begins typing into a text field. If you know a prefix for the future prompt, passing it to `prewarm(promptPrefix:)` allows the system to process the prompt eagerly and reduce latency for the future request. - Important: You should only use prewarm when you have a window of at least 1 second before the call to a respond method, like ``respond(to:options:)-(Prompt,_)`` or ``streamResponse(to:options:)-(Prompt,_)``. Calling this method doesn't guarantee that the system loads your assets immediately, particularly if your app is running in the background or the system is under load.

method · iOS 26.0+

respond

TypeScript
respondToIncludeSchemaInPromptOptions(prompt: Prompt, includeSchemaInPrompt?: boolean, options?: GenerationOptions): Promise<LanguageModelSession_Response<number>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond<Content>(to prompt: Prompt, generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) async throws -> LanguageModelSession.Response<Content> where Content : Generable

Produces a generable object as a response to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

respond

TypeScript
respondToIncludeSchemaInPromptOptions42884(prompt: string, includeSchemaInPrompt?: boolean, options?: GenerationOptions): Promise<LanguageModelSession_Response<number>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond<Content>(to prompt: String, generating type: Content.Type = Content.self, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) async throws -> LanguageModelSession.Response<Content> where Content : Generable

Produces a generable object as a response to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

respond

TypeScript
respondToGeneratingOptionsContextOptionsMetadata(prompt: Prompt, type?: "Bool" | "Int" | "Double" | "Float" | "String" | "Foundation.Decimal" | "FoundationModels.GeneratedContent" | "FoundationModels.ImageReference", options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): Promise<LanguageModelSession_Response<unknown>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond<Content>(to prompt: Prompt, generating type: Content.Type = Content.self, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) async throws -> LanguageModelSession.Response<Content> where Content : Generable

Produces a generable object as a response to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

respond

TypeScript
respondToGeneratingOptionsContextOptionsMetadata53237(prompt: string, type?: "Bool" | "Int" | "Double" | "Float" | "String" | "Foundation.Decimal" | "FoundationModels.GeneratedContent" | "FoundationModels.ImageReference", options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): Promise<LanguageModelSession_Response<unknown>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond<Content>(to prompt: String, generating type: Content.Type = Content.self, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) async throws -> LanguageModelSession.Response<Content> where Content : Generable

Produces a generable object as a response to a prompt. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - type: A type to produce as the response. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

respond

TypeScript
respondToSchemaIncludeSchemaInPromptOptions(prompt: Prompt, schema: GenerationSchema, includeSchemaInPrompt?: boolean, options?: GenerationOptions): Promise<LanguageModelSession_Response<GeneratedContent>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: Prompt, schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) async throws -> LanguageModelSession.Response<GeneratedContent>

Produces a generated content type as a response to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 26.0+

respond

TypeScript
respondToSchemaIncludeSchemaInPromptOptions36651(prompt: string, schema: GenerationSchema, includeSchemaInPrompt?: boolean, options?: GenerationOptions): Promise<LanguageModelSession_Response<GeneratedContent>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: String, schema: GenerationSchema, includeSchemaInPrompt: Bool = true, options: GenerationOptions = GenerationOptions()) async throws -> LanguageModelSession.Response<GeneratedContent>

Produces a generated content type as a response to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - includeSchemaInPrompt: If `true`, injects the schema into the prompt to bias the model. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

respond

TypeScript
respondToSchemaOptionsContextOptionsMetadata(prompt: Prompt, schema: GenerationSchema, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): Promise<LanguageModelSession_Response<GeneratedContent>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: Prompt, schema: GenerationSchema, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) async throws -> LanguageModelSession.Response<GeneratedContent>

Produces a generated content type as a response to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

respond

TypeScript
respondToSchemaOptionsContextOptionsMetadata41640(prompt: string, schema: GenerationSchema, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): Promise<LanguageModelSession_Response<GeneratedContent>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: String, schema: GenerationSchema, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(includeSchemaInPrompt: true), metadata: [String : any ConvertibleToGeneratedContent] = [:]) async throws -> LanguageModelSession.Response<GeneratedContent>

Produces a generated content type as a response to a prompt and schema. Consider using the default value of `true` for `includeSchemaInPrompt`. The exception to the rule is when the model has knowledge about the expected response format, either because it has been trained on it, or because it has seen exhaustive examples during this session. - Parameters: - prompt: A prompt for the model to respond to. - schema: A schema to guide the output with. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: ``GeneratedContent`` containing the fields and values defined in the schema.

method · iOS 27.0+

respond

TypeScript
respondToOptionsContextOptionsMetadata(prompt: Prompt, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): Promise<LanguageModelSession_Response<string>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: Prompt, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(), metadata: [String : any ConvertibleToGeneratedContent] = [:]) async throws -> LanguageModelSession.Response<String>

Produces a response to a prompt. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A string composed of the tokens produced by sampling model output.

method · iOS 27.0+

respond

TypeScript
respondToOptionsContextOptionsMetadata6997(prompt: string, options?: GenerationOptions, contextOptions?: ContextOptions, metadata?: Record<string, SDKHandle>): Promise<LanguageModelSession_Response<string>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: String, options: GenerationOptions = GenerationOptions(), contextOptions: ContextOptions = ContextOptions(), metadata: [String : any ConvertibleToGeneratedContent] = [:]) async throws -> LanguageModelSession.Response<String>

Produces a response to a prompt. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - contextOptions: Settings that configure how the model is prompted. - metadata: Metadata to attach to the request. - Returns: A string composed of the tokens produced by sampling model output.

method · iOS 26.0+

respond

TypeScript
respondToOptions(prompt: Prompt, options?: GenerationOptions): Promise<LanguageModelSession_Response<string>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: Prompt, options: GenerationOptions = GenerationOptions()) async throws -> LanguageModelSession.Response<String>

Produces a response to a prompt. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A string composed of the tokens produced by sampling model output.

method · iOS 26.0+

respond

TypeScript
respondToOptions26720(prompt: string, options?: GenerationOptions): Promise<LanguageModelSession_Response<string>>
Swift declaration
@discardableResult nonisolated(nonsending) final func respond(to prompt: String, options: GenerationOptions = GenerationOptions()) async throws -> LanguageModelSession.Response<String>

Produces a response to a prompt. - Parameters: - prompt: A prompt for the model to respond to. - options: Options that control how tokens are sampled from the distribution the model produces. - Returns: A string composed of the tokens produced by sampling model output.

Skipped declarations

13 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

LanguageModelSession.init(profile:history:)

This protocol needs associated types that cannot yet cross the bridge.

LanguageModelSession.init(model:dynamicInstructions:history:)

This protocol needs associated types that cannot yet cross the bridge.

LanguageModelSession.transcript

The iOS SDK compiler rejected this binding: setter for 'transcript' is only available in iOS 27.0 or newer

LanguageModelSession.init(model:tools:instructions:)

This protocol needs associated types that cannot yet cross the bridge.

LanguageModelSession.init(model:tools:transcript:)

This protocol needs associated types that cannot yet cross the bridge.

LanguageModelSession.init(model:tools:instructions:)

This protocol needs associated types that cannot yet cross the bridge.

LanguageModelSession.init(model:tools:instructions:)

This protocol needs associated types that cannot yet cross the bridge.

LanguageModelSession.respond(options:prompt:)

The iOS SDK compiler rejected this binding: 'async' call in a function that does not support concurrency

LanguageModelSession.respond(schema:includeSchemaInPrompt:options:prompt:)

The iOS SDK compiler rejected this binding: 'async' call in a function that does not support concurrency

LanguageModelSession.respond(generating:includeSchemaInPrompt:options:prompt:)

The iOS SDK compiler rejected this binding: 'async' call in a function that does not support concurrency

LanguageModelSession.respond(options:contextOptions:metadata:prompt:)

The iOS SDK compiler rejected this binding: 'async' call in a function that does not support concurrency

LanguageModelSession.respond(schema:options:contextOptions:metadata:prompt:)

The iOS SDK compiler rejected this binding: 'async' call in a function that does not support concurrency

LanguageModelSession.respond(generating:options:contextOptions:metadata:prompt:)

The iOS SDK compiler rejected this binding: 'async' call in a function that does not support concurrency

All FoundationModels types →