From 5ffac3dd9e1d70a5f9a7deb18fa252118d15e293 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 14:20:53 -0500 Subject: [PATCH 1/2] fix(ch06): guard against IndexError on empty LLM choices list Nine example scripts in chapter 06 access choices[0] without checking whether the API returned any choices. Content-policy filters, token limits, and non-standard responses can all return choices=[]. - aoai-app.py, aoai-history-bot.py, aoai-study-buddy.py: wrap print in `if completion.choices` - oai-app.py, oai-history-bot.py, oai-study-buddy.py: same - githubmodels-app.py: wrap print in `if response.choices` - aoai-app-recipe.py, oai-app-recipe.py: guard both choices accesses and skip the dependent shopping-list call when first response is empty --- .../python/aoai-app-recipe.py | 27 ++++++++++--------- 06-text-generation-apps/python/aoai-app.py | 3 ++- .../python/aoai-history-bot.py | 3 ++- .../python/aoai-study-buddy.py | 3 ++- .../python/githubmodels-app.py | 3 ++- .../python/oai-app-recipe.py | 27 ++++++++++--------- 06-text-generation-apps/python/oai-app.py | 3 ++- .../python/oai-history-bot.py | 3 ++- .../python/oai-study-buddy.py | 3 ++- 9 files changed, 44 insertions(+), 31 deletions(-) diff --git a/06-text-generation-apps/python/aoai-app-recipe.py b/06-text-generation-apps/python/aoai-app-recipe.py index 68736a61d2..919e090a9a 100644 --- a/06-text-generation-apps/python/aoai-app-recipe.py +++ b/06-text-generation-apps/python/aoai-app-recipe.py @@ -69,16 +69,19 @@ def validate_text_input(value: str, max_length: int = 500) -> str: # print response print("Recipes:") -print(completion.choices[0].message.content) - -old_prompt_result = completion.choices[0].message.content -prompt_shopping = "Produce a shopping list, and please don't include ingredients that I already have at home: " - -new_prompt = f"Given ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping}" -messages = [{"role": "user", "content": new_prompt}] -completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600, temperature=0) - -# print response -print("\n=====Shopping list ======= \n") -print(completion.choices[0].message.content) +if not completion.choices: + print("No response received.") +else: + old_prompt_result = completion.choices[0].message.content + print(old_prompt_result) + + prompt_shopping = "Produce a shopping list, and please don't include ingredients that I already have at home: " + new_prompt = f"Given ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping}" + messages = [{"role": "user", "content": new_prompt}] + completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600, temperature=0) + + # print response + print("\n=====Shopping list ======= \n") + if completion.choices: + print(completion.choices[0].message.content) diff --git a/06-text-generation-apps/python/aoai-app.py b/06-text-generation-apps/python/aoai-app.py index 7f3da60472..05f58495e1 100644 --- a/06-text-generation-apps/python/aoai-app.py +++ b/06-text-generation-apps/python/aoai-app.py @@ -23,7 +23,8 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/aoai-history-bot.py b/06-text-generation-apps/python/aoai-history-bot.py index 76efdaa16b..b75137219d 100644 --- a/06-text-generation-apps/python/aoai-history-bot.py +++ b/06-text-generation-apps/python/aoai-history-bot.py @@ -29,7 +29,8 @@ completion = client.chat.completions.create(model=deployment, messages=messages, temperature=0) # print response -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/aoai-study-buddy.py b/06-text-generation-apps/python/aoai-study-buddy.py index ab93859734..398ab98f01 100644 --- a/06-text-generation-apps/python/aoai-study-buddy.py +++ b/06-text-generation-apps/python/aoai-study-buddy.py @@ -32,7 +32,8 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/githubmodels-app.py b/06-text-generation-apps/python/githubmodels-app.py index 94ab9bd203..43e8db3f8f 100644 --- a/06-text-generation-apps/python/githubmodels-app.py +++ b/06-text-generation-apps/python/githubmodels-app.py @@ -33,4 +33,5 @@ top_p=1. ) -print(response.choices[0].message.content) \ No newline at end of file +if response.choices: + print(response.choices[0].message.content) \ No newline at end of file diff --git a/06-text-generation-apps/python/oai-app-recipe.py b/06-text-generation-apps/python/oai-app-recipe.py index 5683f40cb7..b047e64198 100644 --- a/06-text-generation-apps/python/oai-app-recipe.py +++ b/06-text-generation-apps/python/oai-app-recipe.py @@ -24,16 +24,19 @@ # print response print("Recipes:") -print(completion.choices[0].message.content) - -old_prompt_result = completion.choices[0].message.content -prompt_shopping = "Produce a shopping list, and please don't include ingredients that I already have at home: " - -new_prompt = f"Given ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping}" -messages = [{"role": "user", "content": new_prompt}] -completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600, temperature=0) - -# print response -print("\n=====Shopping list ======= \n") -print(completion.choices[0].message.content) +if not completion.choices: + print("No response received.") +else: + old_prompt_result = completion.choices[0].message.content + print(old_prompt_result) + + prompt_shopping = "Produce a shopping list, and please don't include ingredients that I already have at home: " + new_prompt = f"Given ingredients at home {ingredients} and these generated recipes: {old_prompt_result}, {prompt_shopping}" + messages = [{"role": "user", "content": new_prompt}] + completion = client.chat.completions.create(model=deployment, messages=messages, max_tokens=600, temperature=0) + + # print response + print("\n=====Shopping list ======= \n") + if completion.choices: + print(completion.choices[0].message.content) diff --git a/06-text-generation-apps/python/oai-app.py b/06-text-generation-apps/python/oai-app.py index 4b1ca44f1f..474f8c3db2 100644 --- a/06-text-generation-apps/python/oai-app.py +++ b/06-text-generation-apps/python/oai-app.py @@ -16,7 +16,8 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/oai-history-bot.py b/06-text-generation-apps/python/oai-history-bot.py index 7c5082c714..97480e1bd9 100644 --- a/06-text-generation-apps/python/oai-history-bot.py +++ b/06-text-generation-apps/python/oai-history-bot.py @@ -24,7 +24,8 @@ completion = client.chat.completions.create(model=deployment, messages=messages, temperature=0) # print response -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/oai-study-buddy.py b/06-text-generation-apps/python/oai-study-buddy.py index 01fd85951d..9046f20657 100644 --- a/06-text-generation-apps/python/oai-study-buddy.py +++ b/06-text-generation-apps/python/oai-study-buddy.py @@ -27,7 +27,8 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # very unhappy _____. From 05bd9ac71a3ced31e61b7eddddb7b70642b2b9e1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 17:00:58 -0500 Subject: [PATCH 2/2] fix(ch06): extend guards to cover choices[0].message is None (Gemini content-filter) Gemini 2.5 Flash returns HTTP 200 with choices[0].message=None when content is filtered, causing AttributeError even when choices is non-empty. Extends all 9 app guards to also check `message is not None` / `message is None`. --- 06-text-generation-apps/python/aoai-app-recipe.py | 4 ++-- 06-text-generation-apps/python/aoai-app.py | 2 +- 06-text-generation-apps/python/aoai-history-bot.py | 2 +- 06-text-generation-apps/python/aoai-study-buddy.py | 2 +- 06-text-generation-apps/python/githubmodels-app.py | 2 +- 06-text-generation-apps/python/oai-app-recipe.py | 4 ++-- 06-text-generation-apps/python/oai-app.py | 2 +- 06-text-generation-apps/python/oai-history-bot.py | 2 +- 06-text-generation-apps/python/oai-study-buddy.py | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/06-text-generation-apps/python/aoai-app-recipe.py b/06-text-generation-apps/python/aoai-app-recipe.py index 919e090a9a..c6d738f777 100644 --- a/06-text-generation-apps/python/aoai-app-recipe.py +++ b/06-text-generation-apps/python/aoai-app-recipe.py @@ -69,7 +69,7 @@ def validate_text_input(value: str, max_length: int = 500) -> str: # print response print("Recipes:") -if not completion.choices: +if not completion.choices or completion.choices[0].message is None: print("No response received.") else: old_prompt_result = completion.choices[0].message.content @@ -82,6 +82,6 @@ def validate_text_input(value: str, max_length: int = 500) -> str: # print response print("\n=====Shopping list ======= \n") - if completion.choices: + if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) diff --git a/06-text-generation-apps/python/aoai-app.py b/06-text-generation-apps/python/aoai-app.py index 05f58495e1..61acbe9875 100644 --- a/06-text-generation-apps/python/aoai-app.py +++ b/06-text-generation-apps/python/aoai-app.py @@ -23,7 +23,7 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/aoai-history-bot.py b/06-text-generation-apps/python/aoai-history-bot.py index b75137219d..e1c868d362 100644 --- a/06-text-generation-apps/python/aoai-history-bot.py +++ b/06-text-generation-apps/python/aoai-history-bot.py @@ -29,7 +29,7 @@ completion = client.chat.completions.create(model=deployment, messages=messages, temperature=0) # print response -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/aoai-study-buddy.py b/06-text-generation-apps/python/aoai-study-buddy.py index 398ab98f01..06cd0cb232 100644 --- a/06-text-generation-apps/python/aoai-study-buddy.py +++ b/06-text-generation-apps/python/aoai-study-buddy.py @@ -32,7 +32,7 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/githubmodels-app.py b/06-text-generation-apps/python/githubmodels-app.py index 43e8db3f8f..348679507f 100644 --- a/06-text-generation-apps/python/githubmodels-app.py +++ b/06-text-generation-apps/python/githubmodels-app.py @@ -33,5 +33,5 @@ top_p=1. ) -if response.choices: +if response.choices and response.choices[0].message is not None: print(response.choices[0].message.content) \ No newline at end of file diff --git a/06-text-generation-apps/python/oai-app-recipe.py b/06-text-generation-apps/python/oai-app-recipe.py index b047e64198..871058933e 100644 --- a/06-text-generation-apps/python/oai-app-recipe.py +++ b/06-text-generation-apps/python/oai-app-recipe.py @@ -24,7 +24,7 @@ # print response print("Recipes:") -if not completion.choices: +if not completion.choices or completion.choices[0].message is None: print("No response received.") else: old_prompt_result = completion.choices[0].message.content @@ -37,6 +37,6 @@ # print response print("\n=====Shopping list ======= \n") - if completion.choices: + if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) diff --git a/06-text-generation-apps/python/oai-app.py b/06-text-generation-apps/python/oai-app.py index 474f8c3db2..49e848f883 100644 --- a/06-text-generation-apps/python/oai-app.py +++ b/06-text-generation-apps/python/oai-app.py @@ -16,7 +16,7 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/oai-history-bot.py b/06-text-generation-apps/python/oai-history-bot.py index 97480e1bd9..64a2958164 100644 --- a/06-text-generation-apps/python/oai-history-bot.py +++ b/06-text-generation-apps/python/oai-history-bot.py @@ -24,7 +24,7 @@ completion = client.chat.completions.create(model=deployment, messages=messages, temperature=0) # print response -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # very unhappy _____. diff --git a/06-text-generation-apps/python/oai-study-buddy.py b/06-text-generation-apps/python/oai-study-buddy.py index 9046f20657..f1b6f27280 100644 --- a/06-text-generation-apps/python/oai-study-buddy.py +++ b/06-text-generation-apps/python/oai-study-buddy.py @@ -27,7 +27,7 @@ completion = client.chat.completions.create(model=deployment, messages=messages) # print response -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # very unhappy _____.