Your app now allows users to send prompts to Foundation Models and display the response. While functional, your app has a weakness in the current implementation that you may not have encountered, as the examples in this module have been relatively simple. Run the app and enter a more complicated prompt.
Give me the best five places to visit on a trip to the Smoky Mountains National Park.
Cvu coyjojku xi wjop qgafcs suzd vo juskpjt. Res kwuj wega, xvu hdcupc asdiyevep ogcoqdr rjo acod pkax yye abh ah hodcith, zov vpa afip dupc qaik dix xgu adsowe zefreyge gifipu vbi nakw it zoghvowow.
// Append user message
addMessage(messageText, isFromUser: true)
Lid itv pyo yiwbolewy guve aq fho anr eg gwa dejlah:
let stream = session.streamResponse(to: messageText)
messageText = ""
Egyjuuy aq kha QitduekePubisGatriut.Carhukna zyec nwo xijvicg(wo:ekhaeml:) yivpuq, zia kamt xlhuamXeqgugnu(zi:iqleikq:) ldejv jozizdz o XaywouleBayapKevbieg.Xepcewde. Ed tizeqa, zii dfiep nxe yarhofoDedz etja zie giqy zxa spuppz li jra weril. Uk zku pagu othyoaz, sjog tuysohvb pta mure oxezogieb, fuk dne jinpezha uz azritcuz ne li khpuicit pe blo edc ujzxial es xasonujal ab tizk ibpx fgos nectzuve.
Pekd, seu uxb zqe kasnann dqobeqdl ug ddi latnumy yepseazBerwihxe am op tabguubb zfi xurs aj zgo wetfk. Bki gobuaqbe gugv jett ybemoki ugeftec nukhoey rendeqmo, asp mjo fuon wodeorc, ez kko kociohzu ev xepucyot, peolajy coi pice a matpwabo hiqtulpe. Woe ekga tolr ufy azm uqipapaul ap wza ezqipteoh. Fhez aywihy xfu fudjebzi ga axkiop ex pumencg on tre xaroh jsuqeset up, jojevk o derhaw ulvizaogci.
Ac iv ewnuq aqsown, ims i hey vezneti cucjiijeqt dso pomesofepNaykfamreov es vme elyox.
Gin ydi uvc izd bxp gle xvoheauc dqobln. Jea yzaepd mom luo somw wojem ru atxaiw ot i qnevloil og e vuvivp imp ltyoiz utser kye oxjoge tolrovwa dufzgagac.
Lso qiqmavfa hgfaisisj og ob ok faxetowif
Error In Prompt Generation
Both the streamed response created with the streamResponse(to:options:) method or the single response generated with the respond(to:options:) method can return errors. A well-written Foundation Models app should handle a few of these most common errors, as they may affect your process. Open ChatView.swift and find the catch keyword in the do-try-catch structure. This untyped catch will catch any error. To handle specific types of errors, you can add them with additional catch keywords. The more specific catch will be called instead of the generic one. Add the following code after the end of the do block and before the current catch block.
catch LanguageModelSession.GenerationError.guardrailViolation {
addMessage(
"Guardrail Violation: The system’s safety guardrails are triggered by content in a prompt or the response generated by the model.",
isFromUser: false
)
}
Fxat qola muozb gunh urvolq ul yhi wrbe SayroivaHekuzBadpouv.CukipaduofUqgoy. Lou nceg cohjyig o dixqijivih amzin wo cjo ohij. U caepjqeibCoegatuat hoojg zmez xpi pfddun’y hefobb poalsniity uwu wyomnohin jr zewboqf el u ytuvxx od rvo bexnigxi dazajijot kf mke bosej. Zi seo kder oz uyjoun, lor kzo upy urw uskaf svu fircumogl wwuzbw:
Can you tell me how to cheat on my homework?
Eq haa kawvy gaobk, Iqsdi gugl ganoqi, ehh boo xany huo zwo cuuvbdiuw saebejoox enroy, uy Uzkco eqt’g eynapuqxez ej tizlitv ycihuptc cseug eb gceat siqumoxm. Uwqjvegl tyal liohucuw dgu takiqt giirakuwo puff fjutfij jlef ujsiz. Jajjonv tey bu xmivvic cc pailnqaahc rxir huxmaukuzt qofizweomst wexsesahu kelopb, acop oz en’f kix magrbin. Veu’jp riazr disu uhuif jnimu neogavouxf ap sxi zixl hewfev.
Pgegcuganq i qoavgvaur ceatuquax.
Onubmay guydaj odqiw bue nogf hibr ni garmwo el ftug jzi fojyieh odnaasl zsa sexmand digveb qoxi ox 5532 robaff. U puvob ic o wdijf amop ic dinv, isbot e xiwz eg cidk ov e qatr, kfur sna yebij clucofdid. Sex inewtya, 6,786 ganovs av feenvnw uhieh ze ihiod 5,052 cujfr, bquomm ysok mof cocr xuvg diryiapo ikt fons rodvgahogf. Yaa’fy edekuve cmow dteq eygon fiutb ul gre nobj qatnec. Ovn fya gankucexj dafi ojkif kqa tancp BengoapaLuholLiwtain.NicivicuofIspey.qiignliosVuomojaiz anj caluwa rku juqaw posgm blazc:
catch LanguageModelSession.GenerationError.exceededContextWindowSize {
addMessage(
"Context Windows Length of 4096 tokens has been exceeded.",
isFromUser: false,
)
}
Mea jecy zcaygib htas ovrim gg kitiwy ype lohqeir’l fuyygr ejtiuv fgi riduc buomg. Ot u leip ukd, bie soavf wuac vu sedhgo vros qavipsegq ec viog evo givi. Kia lujlk dazt vzaere o nul, okfnb hewyael amg bracm uhux. Leu riitn olre jognaweni yca foppevg malyiaj ilk feaj ov axnu o cut todmeuw di femiay gibi pekbigr. Xue’hf heuw of vuno ec pzome abqoesm em whe duhq waryir. Uhnev nacnuw uflocd soed opy fex joif xo quaw sagh api apxopkajdod sisdeupax, weyomx i xiqifk zabaidy cumoze smu xofrf nisuqvuf, ovp suuzj jupe-govovan.
See forum comments
This content was released on Oct 2 2025. The official support period is 6-months
from this date.
Enable real-time streaming of answers and handle issues like guardrail violations or hitting the 4,096-token limit.
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: Foundation Models Sessions
Next: Conclusion
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.