Claude With Amazon Bedrock
← All lessons
Lesson 35Claude With Amazon Bedrock

Structured data with tools

Summary audio

Spoken summary — press play to read along: the line being spoken stays near the top.

Study notes

**Structured Output: Prompt vs. Tools**

  • Two Methods for Structured Output:
  • Prompt-Based: Uses techniques like message pre-fill and stop sequences to force Claude to output JSON. (Easier to set up. )
  • Tool-Based: Uses defined tools and schemas to reliably extract structured data (primarily JSON). (More complex to set up, but more reliable. )
  • When to Use Each Method: Both methods are valuable; choose prompt-based for simplicity or tool-based for maximum reliability.

Tool-Based Structured Output Process

  • Core Concept: Define a tool whose input parameters dictate the required data structure.
  • General Steps:
  • Define Schema: Write a JSON schema specification for a tool. The inputs of this schema define the structure of the data you want to extract (e. g. , balance must be an integer, key insights must be a list of strings).
  • Input to Claude: Feed the JSON schema specification and the raw data source into Claude.
  • Claude's Action: Claude identifies the tool and responds with a tool use message containing the extracted data as arguments (the desired JSON).
  • Completion: The process is complete once the structured data is returned; no further conversation is needed.

Implementing Data Extraction with Tools

  • Goal: Extract specific data (e. g. , title, author, topics) from a text source.
  • Steps:
  • Create Tool Schema: Define a tool (e. g. , article details schema) specifying the required inputs (e. g. , title → string, author → string, topics → list of strings).
  • User Message: Write a prompt asking Claude to analyze the text and call the defined tool.
  • Tool Choice (Forcing Use): To ensure Claude uses the tool, set the tool_choice parameter in the request (e. g. , set it to the specific tool name, article details tool).
  • Result: Claude executes the tool call, and the output is the extracted data in the required JSON format.

Takeaways

  • Structured output can be achieved using Prompt-Based methods (simpler) or Tool-Based methods (more reliable).
  • Tool-Based extraction relies on defining a JSON schema that specifies the required data structure for a tool.
  • The process involves feeding the JSON schema and the raw data to Claude, which then responds with a tool use message containing the extracted data.
  • To ensure Claude performs the extraction, the tool_choice parameter must be set to force the use of the defined tool.
Flashcards 7 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 6 questions