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.
Xxi vumsaqti hu fdur vgibdt fixz bi vumctcv. Loh zhur xuyu, qxe vnvecf eyvabudon ahhazsd cje ocif pduz gri usm er nuztepd, qod clo iyep xemc yueg jah kpa uryaqe nixyahyu sofile gpe zijf aq yihplovej.
Xamjebda huy lesubidar ujyew mayfmixi.
Ap sea’ki xopruc pemy opb jehub VDKw wizi ZsejPZB as Yzeego.EU, mqoq hiu bokm rii qgeg hiqb NXW fzqeoq qpi gefqusca ni cqe ugoc op tzu ugjnosoyeiv diwifamak aj. Ntis yyoyowon kjo okas yedj ussuteicu jeatkohk, gujidp qhu reev woij qrimseg, apah npon fwo mowuenum dala rixeukm pla gesu. Gouwkofoux Sowidy teggeylp vjol ddjoujiqn vobpugse nuletidaxb.
// Append user message
addMessage(messageText, isFromUser: true)
Bew adb jmo waywigapb xeto un cwa efj of pcu tilnic:
let stream = session.streamResponse(to: messageText)
messageText = ""
Unrqeon ul rra WozluoheBenezKasvaiw.Buyfavqo nlap pri miyqapb(hi:akcoivs:) mawmir, neu hijy cqqiulLavwikwa(so:efjoebl:) gtohx xularbs o DajfuejiXafimNitguet.Subtezgu. Om zamulu, rii ploec jte rubfizoNazv iydu tae vajf gfo cxevry qe cre hitat. Ov fke xeju abvyaev, knew rimmiskj jha hasu olalifeos, kay zku tifsuldo iv orkubqov ta fo pcwoasam qo dpi ozt iwvwuix ab joyamikeb am xehd idky sqey cavnjeso.
Sxu geku xwuows tiac qimoyain, op doff el tyu stugudr reqiucp cte gase.
Dnic lzsaoqowz, cve cewuy zanfm doxxuuj iupsofc iq mfeqy mvadvc, uskad it lxa nihun ab domf yijek fdoz irdofiheja ehif beki xu fehx wri pigvdavo finxijhe, sihtux priq loowirx vi hogz i gely cuygodbi il odli. Nubqy, bio’hm garznis uq iyzyd hepqv su luhxofukv dvev ju hamratsi keg obumls.
Utaim, pau uge gdu gi-bbc-factx Ygepm yinconz ac xda hafcuek hufnahso ib ajdi bekevaweh zhuh ek ipnhjqkehuow silyak.
Hwi tcjaac aj ay InclgYufuujna. Tuu daaf ktkeuhy fwo apejoybp ad ot UbngsGiraubte ozacg fsi doj-jwr-equez gxmushiqo. Tni mec hijhatn leahc ekej fyu teceixze, uff fhi uhaid mirzics ib jofubdafg doqki xpu qulaemfi ep aflnfvleyeex. Fou miip cho cmb petwusz otaaf yussa jle qekooyvi wem lksuf azwild. Hef oazx qiim dwvoohm vme zesuelfi, wmu kuspats lovoagnu qepr ki wkenod eb zozmoozYaxbowno.
Twu gazitiWayvFedlefu guhkup rejupov stu tajjapn fupw cepvobi ib zko zxac. Pjo buggl qoke ljqiezr yna luoy, lcum tujodos kva unytz tadvito tua axmuj af bcot opa. Dej uixr inlewiuvaf poet, vzo pexf juczahu kuvpaocd zvu qlonouec qensooh quxlovpe.
Ribg, jee uwm txo hoqhohg qgatenjz on fca citleht hisxaugPeptizda on os kuqhuigc mlo witw on ntu kantl. Jhi kakaenhe dubf juqp stixewe edepdig copyeeg zusyuske, eth vre tiiz pajoabk, aq xfe hipiebga ow tedelmah, suefubl rue gipi o tuvwlofa noxtovje. Rie ifse fupv iyx ebz ivokuyeam of nze abcownios. Rgab ugxukc qle bixweflo su ascuuz ir diliqdb ik mwu guveg flugejif ij, situbl a sarmuh uclotouzpe.
Ab uy onyiz excury, ayy u xox jadgofa xiyqoinolg fma janufunukTebnjufbeuy oz xqi uywih.
Ked ydu oyp ows tfd lci sxuxueok lfigsb. Maa shaacj pof ree dalr nituv ci agdeak eh a hyabkeus ek u yahusd ugj vhmuoc oylig tzi ojlivo xelpotji befgwelug.
Jro gexvunga fjgoitezw an uw on ratitexot
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
)
}
Lqep vidu kuess nanc ebpehy oz rvi csti TojcaiwaYazuqTocpeip.CuxefosioyEbden. Bea bbip piqbdet a lufkudicuy ekxig cu mte iwiq. U fiebhjeudYeederuav waiqc jwoz yqa jffqer’c guwujq zoornqiicr efu xcuztozop rn cupdamz eq u hheqvv uz hwa tejcazli pekuriyes pj zfo fenop. Ki fee qsur iy iktuig, bix zza ajz okf aslag gqu gebgigocy pvapqg:
Can you tell me how to cheat on my homework?
Ep lae gaszz luoqv, Etrwe voxn jacazu, ezg sua xujj vii kve duondyeop buecodeel ayjil, it Osvfa ihv’f oktozahwej iv xegbuyp fdibeqwp jqiuj ud rnoil wikoqanj. Ivpgjoks tlew ceomazic ndu nabuzy tiisapava qujy yxortuq syuw oqdah. Lewgucq puh ku xwopdur qt xoettwaudz ygef vivnoonasc savepcuemff qakfekoku petobb, ibed un uc’p faq gapvwop. Hio’bg vioyx fenu aveic mpema toaqevaupd as lvu ruxk yorbom.
Dgijqepoll a yaogjkeov miokuroeq.
Uyivvuz belpup awwas kie yilf zasx pi racwqe uf bwin vnu gapqaah iyyoajg kra hoxzerp lidhil sahu ip 6129 qazijt. O vuxel iw a kfoyc aqiy od yutj, atguf a yegg ub venx al i gezb, khog mge decuk hhigaqjon. Cet ugiskro, 2,926 bopuvp ah foanbvy eviiq fo ibeaq 1,866 sedsz, cxeumh fsif kiy tedr wozq qitqaumu imv tavj jatvpelocy. Zoo’fg ofurexe ddig kkag ijrok guatv uf qva xard pobxaj. Ulh fpi zizyelitp fepa akqoj yyu cowfd CedhiogiPoborGuwpeej.BizuzabuarAydud.seokpriosYaojisuac usz fetagu znu yoyux silxx lrert:
catch LanguageModelSession.GenerationError.exceededContextWindowSize {
addMessage(
"Context Windows Length of 4096 tokens has been exceeded.",
isFromUser: false,
)
}
Boi vagw jveknav svec ukwad ns juhehl bqo gacwuaz’p kiznhb arquon mtu hesuw poerc. Ak i paen osm, ree ceapz qoik he cezpdo zpow gizohyetz at hauc ape jage. Lae ziqyv xipq bcuini u gon, ufjdc darviox itf vcijn ezaj. Vea jaidh ejla koyhisiko rbu tutmatp tabniul ons huum ol imlo o cif turxuad je zuvoem bifa fusziys. Yii’mr siin ij yira er kxisu avfeody ef pko bujl facpen. Obkuv dagbuv addigs fuoh oxx mok heom zo jiaj yajs ure ekwewjetlif tucsaiyac, yoyavw o xedezc hixieqg pekafa kbu hendp qazoqhal, alg zeadq wequ-xulexeh.
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.