From add055f07f863f1cf3214323b950af916138bddd Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 15:15:41 -0500 Subject: [PATCH 1/2] fix(examples): guard non-streaming choices access to match streaming pattern The streaming example already checks `if not chunk.choices: continue` before accessing chunk.choices[0]. Apply the same guard to the two non-streaming completions in demo.py for consistency. Content-policy filters can return choices=[], which raises IndexError. --- examples/demo.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/demo.py b/examples/demo.py index ac1710f3e0..8be86f6e0e 100755 --- a/examples/demo.py +++ b/examples/demo.py @@ -16,7 +16,8 @@ }, ], ) -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) # Streaming: print("----- streaming request -----") @@ -50,4 +51,5 @@ ) completion = response.parse() print(response.request_id) -print(completion.choices[0].message.content) +if completion.choices: + print(completion.choices[0].message.content) From d566bef3b3d4bbc429a92bf9605e1edb2a781952 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 16:35:39 -0500 Subject: [PATCH 2/2] fix(examples): also guard choices[0].message is None for Gemini content-filter case Gemini 2.5 Flash returns HTTP 200 with choices[0].message=None when content is filtered (finish_reason: PROHIBITED_CONTENT). The previous guard `if completion.choices:` only prevented IndexError on empty lists; this extends it to also handle the None-message case to prevent AttributeError. --- examples/demo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/demo.py b/examples/demo.py index 8be86f6e0e..7aba135f02 100755 --- a/examples/demo.py +++ b/examples/demo.py @@ -16,7 +16,7 @@ }, ], ) -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content) # Streaming: @@ -51,5 +51,5 @@ ) completion = response.parse() print(response.request_id) -if completion.choices: +if completion.choices and completion.choices[0].message is not None: print(completion.choices[0].message.content)