Claude With The Anthropic Api
← All lessons
Lesson 13Claude With The Anthropic Api

Structured data

Summary audio

No audio recap for this lesson.

Study notes

Core Use Case

  • Technique for generating structured data (JSON, Python code, bulleted lists) with no extra commentary
  • Prevents Claude from adding headers, footers, or explanatory text around the requested content
  • Critical for user experience when users need to copy/use raw output directly

The Problem

  • Claude naturally wants to explain its work and add context
  • When generating JSON, Claude often wraps it in markdown code blocks (backticks)
  • Extra commentary makes it difficult for users to extract and use just the data they need

The Solution: Two-Part Approach

**1. Pre-fill an Assistant Message**

  • Add a message as if Claude already started responding
  • Include the opening marker (e. g. , JSON `)
  • This signals to Claude it should continue from that point, not add a header

**2. Add a Stop Sequence**

  • Define a sequence that tells Claude to stop generation immediately
  • Use the closing marker (e. g. , `)
  • Prevents Claude from adding footer commentary

How It Works Together

  • Claude reads the user request and considers adding headers/footers
  • Claude encounters the pre-filled assistant message and assumes it already started the response
  • Claude generates only the core content (the JSON/data itself)
  • Claude naturally tries to close the code block with the stop sequence
  • Stop sequence triggers immediately, cutting off generation
  • Result: only the raw structured data is returned

Practical Benefits

  • Output can be directly copied and used without manual editing
  • Works for any structured data format, not just JSON
  • Removes need for post-processing to strip unwanted text
  • Improves user experience in applications requiring clean data output

Takeaways

  • Pre-fill an assistant message with the opening marker (e. g. , JSON `) to signal Claude should continue from that point rather than add headers
  • Add a stop sequence at the closing marker (e. g. , `) to prevent Claude from adding footer commentary
  • Together, these techniques generate only raw structured data with no extra explanation or markdown wrapping, making output directly usable without manual editing
Flashcards 8 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 1 questions