Parsing nested API responses — without crashing on a missing key — step 6 of 9
Claude wrote a parser that pulls the assistant text out of a fake
Anthropic response. The chain uses strict [] access at every
level. If the response was filtered (no content array), the
parser crashes with KeyError.
Replace the strict chain on line 2 with the safe pattern: default
content to [], return the string "(blocked)" if the list is
empty, otherwise pull the first block's text out with .get().
The starter has a content-filtered response (no content key).
Expected output:
(blocked)
Claude wrote a parser that pulls the assistant text out of a fake
Anthropic response. The chain uses strict [] access at every
level. If the response was filtered (no content array), the
parser crashes with KeyError.
Replace the strict chain on line 2 with the safe pattern: default
content to [], return the string "(blocked)" if the list is
empty, otherwise pull the first block's text out with .get().
The starter has a content-filtered response (no content key).
Expected output:
(blocked)
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.