http and apis — making the call6 / 9
parsing nested api responses — without crashing on a missing key
Claude wrote a parser that pulls the assistant text out of a fake
LLM response. The chain uses strict [] access at every level. If
the response was filtered (no choices), the parser crashes with
KeyError.
Replace the strict chain on line 6 with the safe pattern: default
choices to [], return the string "(blocked)" if the list is
empty, otherwise pull message.content out with chained .gets.
The starter has a content-filtered response (no choices key).
Expected output:
(blocked)
The break is on line 2 — but read the whole snippet first.
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _