﻿{
    "cells":  [
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "# Module 7 Self-Driven Lab\n",
                                     "\n",
                                     "Use this notebook as your workspace for **Prompt Engineering for Extraction**. The answer-key notebook sits next to this file. Work through each checkpoint before comparing against the completed version.\n"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "# Module 7 â€” Prompt Engineering for Extraction\n",
                                     "\n",
                                     "Two pillars of structured outputs:\n",
                                     "\n",
                                     "- **`output_config.format`** â€” forces Claude\u0027s final response into a JSON object matching your schema.\n",
                                     "- **`strict: true` on tools** â€” guarantees tool inputs match the schema exactly.\n",
                                     "\n",
                                     "Limits: **20** strict tools, **24** optional params, **16** union-type params per request. Citations and structured outputs are mutually exclusive â€” sending both returns a 400."
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Setup"
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# Setup cell. Run this before the exercise cells below.\n",
                                     "import anthropic\n",
                                     "from dotenv import load_dotenv\n",
                                     "\n",
                                     "load_dotenv()\n",
                                     "client = anthropic.Anthropic()"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## 1. JSON output + strict tool input\n",
                                     "\n",
                                     "The first request a new schema is sent on incurs grammar-compilation latency; compiled grammars are cached for 24 hours from last use. Changing the schema or the set of tools invalidates the cache."
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Checkpoint 1\n",
                                     "\n",
                                     "Implement this step yourself. Use the preceding explanation and the module page as your guide, then compare your approach with the answer key after you have a working version.\n"
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# TODO: Implement checkpoint 1 for Module 7.\n",
                                     "# Keep the cell focused, run it, inspect the output, then move to the next checkpoint.\n",
                                     "\n",
                                     "raise NotImplementedError(\u0027Complete checkpoint 1\u0027)\n"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## 2. Nullable fields prevent hallucination\n",
                                     "\n",
                                     "Use a type array `[\"string\", \"null\"]` so missing data comes back as `null` instead of an invented placeholder. Each nullable field counts against the 16-union-type budget per request."
                                 ]
                  },
                  {
                      "cell_type":  "code",
                      "execution_count":  null,
                      "metadata":  {

                                   },
                      "outputs":  [

                                  ],
                      "source":  [
                                     "# Setup cell. Run this before the exercise cells below.\n",
                                     "prospect_schema = {\n",
                                     "    \"type\": \"object\",\n",
                                     "    \"properties\": {\n",
                                     "        \"company_name\":  {\"type\": \"string\"},\n",
                                     "        \"contact_email\": {\"type\": \"string\"},\n",
                                     "        \"budget_range\":  {\"type\": [\"string\", \"null\"]},  # may be unknown\n",
                                     "        \"decision_date\": {\"type\": [\"string\", \"null\"]},  # may be unknown\n",
                                     "    },\n",
                                     "    \"required\": [\"company_name\", \"contact_email\", \"budget_range\", \"decision_date\"],\n",
                                     "    \"additionalProperties\": False,\n",
                                     "}\n",
                                     "\n",
                                     "import json\n",
                                     "print(json.dumps(prospect_schema, indent=2))"
                                 ]
                  },
                  {
                      "cell_type":  "markdown",
                      "metadata":  {

                                   },
                      "source":  [
                                     "## Reflection\n",
                                     "\n",
                                     "Before opening the answer key, write down what worked, what failed, and which API behavior or agent-design rule you would rely on in production.\n"
                                 ]
                  }
              ],
    "metadata":  {
                     "kernelspec":  {
                                        "display_name":  "Python 3",
                                        "language":  "python",
                                        "name":  "python3"
                                    },
                     "language_info":  {
                                           "name":  "python",
                                           "version":  "3.9"
                                       }
                 },
    "nbformat":  4,
    "nbformat_minor":  5
}
