There are several weaknesses to the current app. The most glaring is that you create a new LanguageModelSession for each prompt. To see the problem this causes, enter the following two prompts, waiting for the first to complete before entering the second.
Give me five popular fruits.
Which of these are commonly available in the United States in the summer?
Lack of Session Memory in Chat.
The wording of the second response will vary, but it will generally show no idea of the fruits you asked about in the first prompt. When you create a new session for each prompt, each exists as a stand-alone interaction. When you make the second prompt, it does not know the response to the first question. To fix that, you can create a single session and send each prompt to it. Doing so is simple.
Open ChatView.swift and add the following new property to the view:
@State private var session = LanguageModelSession()
This creates a property that holds a session. As long as you reuse this session, the session will retain an awareness of all prompts and responses. Now find comment one in the sendMessage method and delete the let session = LanguageModelSession() line. The method will now use the sendMessage method at the view level, which will remain across multiple prompts.
Type the same two prompts asking about fruit again. This time, the second response will build upon the context of the first response and answer using the context of the fruit provided in the first response. Using a single session lets you provide multiple prompts and responses that become known information for later prompts.
Chat with Session using responses from previous prompts.
Because generating a response takes time, a shared session can encounter errors if a new request is sent before the previous one completes. To prevent this, add the following modifier to the MessageInputView view:
.disabled(session.isResponding)
This change disables the input while the session responds, so the user cannot send a second message until the first response is complete. You can also now use this property to replace the view’s isTyping property. Find the following line in the view and delete it:
@State private var isTyping = false
Next, find the following code:
if isTyping {
TypingIndicator()
}
And change it to:
if session.isResponding {
TypingIndicator()
.transition(.scale)
}
This will display the typing indicator when the session is responding to a prompt and adds a transition to replace the prior animation. Last, find the two withAnimation(_:_:) inside the sendMessage method that set the old isTyping property in their blocks and delete them.
If you entered the first prompt about fruit above, then clear the chat, the session will still retain the context of those deleted prompts and responses. Whenever you delete the chat history, you also need to clear the session to prevent this from occurring.
Session Not Cleared and Deleted Information Remembered
To do this, find the resetChatHistory method and add the following code to the end of the method:
session = LanguageModelSession()
This replaces the existing session with a new one, clearing the context. To see this, enter the first fruit prompt, clear the chat, and now enter the second prompt, showing it no longer recalls the previous response.
Session Reset
In the next section, you will learn how to generate streamed responses to provide a better user experience and handle errors.
See forum comments
This content was released on Oct 2 2025. The official support period is 6-months
from this date.
Learn how to keep a single LanguageModelSession so the model remembers past prompts, and how to reset or block input to avoid errors.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Using Foundation Models
Next: Streaming Model Responses
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.