schema-first-prompting
PydanticモデルとLLMプロンプトテンプレートを設計し、構造化されたデータ抽出パイプラインを構築する際に、LLMの出力スキーマとなるモデルの作成・編集や、それに対応するプロンプト作成を支援するSkill。
📜 元の英語説明(参考)
Design Pydantic models and LLM prompt templates for structured extraction pipelines. Use when creating, editing, or reviewing Pydantic models that serve as LLM output schemas, or when writing prompt templates that pair with those models. Trigger: "pydantic model", "structured output", "extraction schema", "LLM output model", "schema design".
🇯🇵 日本人クリエイター向け解説
PydanticモデルとLLMプロンプトテンプレートを設計し、構造化されたデータ抽出パイプラインを構築する際に、LLMの出力スキーマとなるモデルの作成・編集や、それに対応するプロンプト作成を支援するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o schema-first-prompting.zip https://jpskill.com/download/10053.zip && unzip -o schema-first-prompting.zip && rm schema-first-prompting.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10053.zip -OutFile "$d\schema-first-prompting.zip"; Expand-Archive "$d\schema-first-prompting.zip" -DestinationPath $d -Force; ri "$d\schema-first-prompting.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
schema-first-prompting.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
schema-first-promptingフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
スキーマファーストプロンプティング
まず Pydantic モデルを設計し、次にプロンプトを作成します。モデルは構造的な契約であり、プロンプトはスキーマがエンコードできないものだけを伝えます。簡潔、明瞭、エレガント、そして内部的に一貫性があるようにしてください。
1. 同じことを二度言わない
スキーマは形状(名前、型、ネスト、制約)を所有します。プロンプトは意図(トーン、対象者、修辞的なルール)を所有します。同じ事実が両方に存在する場合は、重複を削除します。
| 何 | どこ |
|---|---|
| 形状、型、制限 | Pydantic モデル (Field、バリデーター) |
| バリアント/スロットからの固定値 | 検証後のコード |
| 修辞的なルール/品質ルール | プロンプト |
| 使用するスキーマ | 呼び出し元(条件付きモデル + プロンプト) |
API が JSON Schema に関連付けられた構造化された出力またはツールパラメーターをサポートしている場合は、そこに構造を配置します。ユーザー/システムメッセージは、タスク、コンテキスト、および動作に限定してください。
ネイティブの構造化された出力がサポートされておらず、スキーマをプロンプトに注入する必要がある場合は、生の JSON Schema (model_json_schema()) を注入することは避けてください。これはトークン効率が非常に悪いためです。代わりに、型定義の疑似コード(TypeScript スタイルのインターフェース)を使用すると、モデルの注意メカニズムにとってより明確であると同時に、トークンの使用量を最大 60% 削減できます。
プロンプトは、スキーマがすでにカバーしているフィールド名、型、ネスト、必須/オプション、デフォルト、またはキーの列挙を繰り返すべきではありません。
プロンプトに保持するもの: 意図とトーン、スキーマがエンコードできない制約(「入力からの固有名詞は使用しない」、「最大 50 語」)、入力とテンプレート変数、変数による条件付きブロック — モデルにセクションを「無視」するように依頼することによってではありません。
矛盾がないこと
フィールドが max_length=5 と指定している場合、プロンプトは「3〜8 個のアイテム」と言うべきではありません。Pydantic の制約は正確です。モデルはプロンプトの数値を雰囲気として読み取ります。矛盾は、どちらかのバージョン単独よりも悪い隠れたバグを作成します。
ブランチのテンプレート変数
USER_PROMPT = """あなたは計画を抽出しています。
{extra_instructions}
ソーステキスト:
{source}
"""
# 呼び出し元は、PlanWithRisks を使用する場合、extra_instructions="" または extra_instructions=RISKS_BLOCK を設定します。
モデルは、スキーマにないブランチの指示を見ることはありません。
レビューチェックリスト
プロンプト/モデルのペアをレビューするときは、以下を確認してください。
- 名前が一致する: プロンプトは、スキーマと同じフィールド名と概念名を使用します。
- 制約が一致する: カウント、制限、オプション性、およびブランチの動作が同一です。
- 責任が一致する: プロンプトはスキーマが期待するもののみを要求します。スキーマは LLM が生成する必要があるもののみをモデル化します。
2. 推論を最初に置く
スキーマ内のフィールドの順序は、表面的なものではありません。自己回帰モデルはトークンを左から右にコミットするため、スキーマが reasoning の前に decision を配置すると、モデルは考える前に回答を入力します。
- 推論/chain-of-thought フィールドを最初に — それらが通知するターゲットデータの前に。
- 詳細の前に高レベルの決定 —
body_textの前にtoneまたはstrategy。 - 依存フィールドの前に独立フィールド — B が A に依存する場合、A が最初にきます。
ネストされたモデル内の専用の推論フィールドは、そのステップの品質を向上させることができます。トークンがかかります — タスクが難しい場合にのみ使用し、すべてのリーフで使用しないでください。フィールドの説明ですでに推論方法が述べられている場合は、プロンプトで同じ指示を繰り返さないでください。
3. すでに知っていることを要求しない
値がルックアップテーブル、バリアント、または既存のメタデータから導出できる場合は、スキーマから除外します。LLM は、入力を読み取り、判断を下す必要があるフィールドのみを処理する必要があります。
コードで固定値を導出する
バリアントまたはスロットがわかれば値が固定される場合は、モデルに要求するのではなく、マッピング、match、またはヘルパーを使用して導出します。
オプションのフィールドではなく、個別のモデルを使用する
risk_section: RiskAssessment | None と、プロンプトの文章で「リスクが低い場合は省略する」を使用しないでください。これは、モデルに、すでに答えを知っている構造的な決定をさせることになります。LLM を呼び出す前に、別のルートモデルを選択してください。
class RiskAssessment(BaseModel):
summary: str
severity: Literal["low", "medium", "high"]
class PlanWithRisks(BaseModel):
outline: OutlineSection
summary: str = Field(description="Closing summary.")
risk_section: RiskAssessment
class PlanWithoutRisks(BaseModel):
outline: OutlineSection
summary: str = Field(description="Closing summary.")
# risk_section はこのモデルにはまったく存在しません
スキーマに含めるもの
| 含める | 除外する |
|---|---|
| モデルが作成する必要があるテキスト、ラベル、リスト | バリアント/スロットから導出された値 |
| モデルが選択する必要がある構造 | コードが適用するデフォルト |
| ダウンストリームが実際に消費するフィールド | 検証後にマージされる「ヘルパー」フィールド |
4. LLM の動作方法に合わせて設計する
スキーマは、言語モデルへのインターフェースです。ORM で見た目がきれいかどうかではなく、モデルが得意なことと苦手なことを中心に設計します。
LLM モデルを API および DB モデルから分離する
LLM 抽出の形状、API リクエスト/レスポンスの型、および永続化の行には、異なるフィールドと不変条件があります。すべてのレイヤーに対して 1 つの「ゴッドモデル」を使用すると、フィールドが境界を越えてリークします。ユーザーとデータベースが決して見るべきではない LLM のためのスクラッチパッドと推論フィールドを保持します。
見積もりではなく、決定を求める
LLM は、絶対的な数値(ミリ秒単位の期間、ピクセル座標、正確な単語数)が苦手です。カテゴリカルな決定(どの重大度レベルか、どのアイテムが最初にランク付けされるか、どのビンか)の方がはるかに優れています。可能な限り、数値を選択肢として再構成します。数値を要求する必要がある場合は、範囲を狭くし、フィールドの説明で明確に定義してください。
タスクの難易度に合わせて構造を調整する
タスクが簡単な場合は、「念のため」に推論フィールドや足場を追加しないでください。追加のフィールドはトークンを消費し、正当化を強制することで品質を低下させる可能性があります。タスクが難しい場合(複数エンティティの抽出、長距離の一貫性)、推論フィールドに投資し、作業をステップに分割します。適切な構造の量は、重要性ではなく、観察された難易度によって異なります。
ステップごとにコンテキストの範囲を設定する
原稿全体を 1 回の呼び出しにダンプし、
(原文がここで切り詰められています)
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Schema-First Prompting
Design the Pydantic model first, then write the prompt. The model is the structural contract; the prompt carries only what the schema cannot encode. Be brief, clean, elegant, and internally consistent.
1. Don't say the same thing twice
Schema owns shape — names, types, nesting, constraints. Prompt owns intent — tone, audience, rhetorical rules. If the same fact lives in both places, delete the duplicate.
| What | Where |
|---|---|
| Shape, types, limits | Pydantic model (Field, validators) |
| Fixed values from variant/slot | Code, after validation |
| Rhetorical / quality rules | Prompt |
| Which schema to use | Caller (conditional model + prompt) |
When the API supports structured output or tool parameters tied to JSON Schema, put structure there. Keep user/system messages to task, context, and behavior.
If native structured output is unsupported and you must inject the schema into the prompt, avoid injecting raw JSON Schema (model_json_schema()), as it is highly token-inefficient. Instead, use type-definition pseudo-code (TypeScript-style interfaces), which can reduce token usage by up to 60% while being clearer to the model's attention mechanism.
The prompt should not restate: field names, types, nesting, required vs optional, defaults, or key enumerations the schema already covers.
Keep in the prompt: intent and tone, constraints the schema cannot encode ("no proper nouns from the input", "at most 50 words"), inputs and template variables, conditional blocks via variables — not by asking the model to "ignore" a section.
No contradictions
If a field says max_length=5, the prompt must not say "3–8 items." Pydantic constraints are exact; the model reads prompt numbers as vibes. Contradictions create hidden bugs worse than either version alone.
Template variables for branches
USER_PROMPT = """You are extracting a plan.
{extra_instructions}
Source text:
{source}
"""
# Caller sets extra_instructions="" or extra_instructions=RISKS_BLOCK when using PlanWithRisks.
The model never sees instructions for a branch that is not in the schema.
Review checklist
When reviewing a prompt/model pair, verify:
- Names match: prompt uses the same field and concept names as the schema.
- Constraints match: counts, limits, optionality, and branch behavior are identical.
- Responsibilities match: prompt asks only for what the schema expects; schema models only what the LLM must produce.
2. Put reasoning first
Field order in a schema is not cosmetic. Autoregressive models commit to tokens left to right, so if your schema puts decision before reasoning, the model fills in an answer before it thinks.
- Reasoning / chain-of-thought fields first — before the target data they inform.
- High-level decisions before details —
toneorstrategybeforebody_text. - Independent fields before dependent ones — if B depends on A, A comes first.
A dedicated reasoning field in a nested model can improve quality for that step. It costs tokens — use it when the task is hard, not on every leaf. Do not duplicate the same instruction in the prompt if the field description already states how to reason.
3. Don't ask for what you already know
If a value can be derived from a lookup table, variant, or existing metadata, keep it out of the schema. The LLM should only touch fields that require reading the input and making a judgment.
Derive fixed values in code
If a value is fixed once you know the variant or slot, derive it with a mapping, match, or helper — not by asking the model.
Use separate models, not optional fields
Do not use risk_section: RiskAssessment | None plus prompt prose saying "omit when low-risk." That asks the model to make a structural decision you already know the answer to. Select a different root model before the LLM call.
class RiskAssessment(BaseModel):
summary: str
severity: Literal["low", "medium", "high"]
class PlanWithRisks(BaseModel):
outline: OutlineSection
summary: str = Field(description="Closing summary.")
risk_section: RiskAssessment
class PlanWithoutRisks(BaseModel):
outline: OutlineSection
summary: str = Field(description="Closing summary.")
# risk_section does not exist on this model at all
What belongs in the schema
| Include | Exclude |
|---|---|
| Text, labels, lists the model must author | Values derived from variant/slot |
| Structure the model must choose | Defaults your code will apply |
| Fields downstream truly consume | "Helper" fields merged in after validation |
4. Design for how LLMs work
A schema is an interface to a language model. Design around what the model is good and bad at, not what looks clean in an ORM.
Separate LLM models from API and DB models
LLM extraction shapes, API request/response types, and persistence rows have different fields and invariants. One "god model" for all layers leaks fields across boundaries. Keep scratchpad and reasoning fields for the LLM that users and databases should never see.
Ask for decisions, not estimates
LLMs are poor at absolute numeric values — millisecond durations, pixel coordinates, precise word counts. They are much better at categorical decisions: which severity level, which item ranks first, which bin. Reframe numbers as choices wherever possible. If you must ask for a number, keep the range small and well-defined in the field description.
Match structure to task difficulty
If a task is easy, do not add reasoning fields or scaffolding "just in case." Extra fields cost tokens and can reduce quality by forcing justification. If a task is hard (multi-entity extraction, long-range consistency), invest in reasoning fields and break the work into steps. The right amount of structure depends on observed difficulty, not importance.
Scope the context per step
Dumping an entire manuscript into one call and asking for a complex nested output is a recipe for degraded quality in the tail. Break large pipelines into focused steps, each with its own schema, where the input is scoped to what that step needs. Use prompt caching for shared context (style guides, instructions), but restart the generation context for each step so the model's attention is fresh. This is not just a cost optimization — it directly improves output quality on later fields.
5. Start with the tightest schema that works
Begin with the simplest schema that could work. Add reasoning fields, submodels, and constraints only when the output proves they are needed. Complexity should be earned by failure, not anticipated by speculation.
The prompt suggests; the schema enforces.
Bad: "please make sure the list has 3–5 items."
Good: min_length=3, max_length=5 in the model.
Schema shape
One model per shape
Each distinct output shape gets its own model. Optional fields that only apply to some shapes are a smell — use a discriminated union or separate models (see section 3).
Discriminated unions
Fixed slots — when the parent model has named fields, the field name tells you the shape. Do not add a kind inside each child that repeats what the field name says.
class OutlineSection(BaseModel):
title: str = Field(description="Section heading.")
bullets: list[str] = Field(default_factory=list, max_length=8)
class DocumentPlan(BaseModel):
outline: OutlineSection
summary: str = Field(description="Closing summary: 2-3 sentences.")
Tagged union — when a single value must be one of several shapes, you need a discriminator for deserialization:
from typing import Annotated, Literal, Union
from pydantic import BaseModel, Field
class SearchStep(BaseModel):
kind: Literal["search"] = "search"
query: str
class AnswerStep(BaseModel):
kind: Literal["answer"] = "answer"
text: str
Step = Annotated[
Union[SearchStep, AnswerStep],
Field(discriminator="kind"),
]
class Plan(BaseModel):
steps: list[Step]
kind is the wire-format tag the model must emit so the union parses. Do not mirror it as a second field (action, step_type).
Avoid single-string wrappers
Every nested model should earn its keep by adding real structure, clearer validation, or a stable reusable concept. A BaseModel with one str field adds nesting without structure — use a plain field with Field(description=...) on the parent. Keep a dedicated model when there are at least two meaningful fields, or when you are grouping a stable sub-object at a known serialization boundary that will genuinely grow. "Will grow" means there is a concrete next field on the roadmap — not a hypothetical one.
# Bad — wrapper adds nothing
class ClosingSummary(BaseModel):
text: str = Field(description="2-3 sentences.")
class Report(BaseModel):
closing: ClosingSummary
# Good
class Report(BaseModel):
closing: str = Field(description="Closing summary: 2-3 sentences.")
Base classes
Extract a shared base only when several shared fields justify it. One duplicated field across two models is clearer than a _Base with a single line.
Field design
- Mutable defaults:
default_factory=list, never[]. - Descriptions:
Field(description=...)guides the model; avoid internal jargon. If a field has long or subtle rules, put them in the description so they travel with the schema. - Dead fields: if nothing produces or consumes a field, drop it. Drop "legacy" aliases too.
- Names: short, specific, readable in code and JSON Schema. Prefer names that describe the actual concept, not the implementation accident. Avoid vague names like
data,info,payload,value,type2, ormisc. Avoid ornamental naming: ifBannerCopysays it, do not name a fieldbanner_copy_text_value. Keep siblings parallel —quote_text/quote_source, notquoteAttributionLine. Rename awkward names early; small schema names spread into prompts, validators, logs, tests, and downstream code. - Nullable vs empty: use
str | None = Nonewhen missing differs from empty. Under strict constrained decoding, omitted keys are not allowed — all fields must be marked as required, using nullable types (["string", "null"]) for optional values while keeping the key inrequired. - Closed sets: for
EnumorLiteral, include an escape hatch (OTHER,UNKNOWN) when the model must say "none of the above." - Bounded extras: open-ended
dict[str, str]invites huge blobs. Preferlist[SmallObject](or(key, value)tuples) withmax_itemsplus a short description of the cap. Note thatmax_itemsis stripped during strict-mode sanitization for OpenAI, so keep Python-side validation. - Entity relationships: model IDs explicitly (
parent_id,friend_ids: list[int]), not free-text names. Downstream code should not parse prose for links. - Known structure: nested models,
max_length/max_items, enums orLiteralwhere the LLM must pick from a closed set. However, some providers' strict modes forbid validation keywords — see strict mode section below. - Unknown structure:
dict[str, Any], looselist[dict[str, Any]]— use only where the content is genuinely open-ended. A known concept should not bedict[str, Any].
Strict mode and validation
Provider-specific strict mode
Not all providers handle JSON Schema validation keywords the same way. Know what your target supports before relying on field-level constraints.
Write your Pydantic models with full validation (max_length, ge, le, max_items, etc.). Then apply a provider-specific sanitizer only where required. This gives you one authoritative model with the tightest constraints, and a thin adapter layer per provider.
OpenAI (strict=True): forbids maxLength, maxItems, minimum, maximum, and similar validation keywords in JSON Schema. Sending a model with Field(ge=0, le=150) results in an immediate 400 error. additionalProperties must be false; empty dictionary annotations (dict[str, Any]) will cause immediate failure. Implement a schema sanitizer that strips these constraints from the JSON Schema before sending, while keeping the unmodified Pydantic model for Python-side validation.
Anthropic (tool use): accepts standard JSON Schema validation keywords including maxLength, minLength, pattern, minimum, maximum, minItems, and maxItems. Pydantic models with Field(ge=0, le=150) or Field(max_length=500) work as-is. However, the model may still occasionally violate soft constraints, so keep Python-side validation as a safety net.
Sanitizers
LLM completions are untrusted input. Do not assume the model returns clean JSON — raw text may include markdown fences or leading prose. Extract JSON (or use the provider's native structured output) before model_validate / model_validate_json.
Sanitizers and validators are complementary: pre-parse cleanup vs. post-parse rules.
Do: coerce None → "", list → joined string where needed, strip overlong strings, pop() keys that are not on the model (LLM hallucinated extras).
Don't: re-implement defaults or Literal enforcement the validator already applies; keep dead branches for old shapes.
Validation feedback loop
When model_validate() fails due to hallucinations or missed constraints, do not simply drop the data. Catch the Pydantic ValidationError and feed the exact error message (e.g., "Value error, Name must contain a space") back to the LLM in a new user prompt, commanding it to self-correct its previous output. Libraries like Instructor automate this retry loop by catching validation errors and returning them directly to the model alongside the original completion payload.
Production
Prompts are artifacts, not immortal strings. Separate fixed wording from runtime data. Track changes in source control — when behavior shifts, you need a diff and a rollback story. Keep a small golden set of inputs with expected or acceptable outputs; rerun when the model or prompt changes. Subjective tasks still need criteria: length, must-include fields, forbidden patterns. Log latency, token use, and validation failures per prompt version so regressions surface before users report them.