Your First Real Summary
call the model and clean the reply
Part of: AI Text Summarizer
You have the request shape. Now make the call and pull a clean summary out of what comes back. This is the smallest thing that actually works: text in, summary out. Reading the reply The Anthropic API doesn't hand you a bare string. It returns a message whose content is a list of blocks, and the text lives in the first block: That resp.content[0].text is the line beginners miss. Print resp directly and you get an object repr, not your summary. Models add junk you didn't ask for Ask for three sentences and you'll often get exactly three sentences, wrapped in polite scaffolding: Or fenced in markdown code blocks. That preamble and those fences are fine for a human reading a chat, but your tool is a program, and the next step (printing, saving, counting words) wants the summary alone. So you clean the reply before using it: "] if lines and lines[0].rstrip().endswith(":"): lines = lines[1:] while lines and lines[0].strip() == "": lines.pop(0) while lines and lines[-1].strip() == "": lines.pop() return "\n".join(lines) Three defensive moves: drop lone code-fence lines, drop a leading label line that ends in a colon ("Here is the summary:"), and trim blank lines top and bottom. None of t
Challenge: Clean the Model's Reply