AI Agents with LangChain & Autonomous Workflows · レッスン

構造化出力の解析と検証

出力パーサーとスキーマを使ってLLMに信頼できる構造化データを返させ、形式不正の出力が生成された場合は検証または再試行する方法を学びます。

レッスン 4/413 ステップ

「構造化出力の解析と検証」はCoddyKit上の無料AI Agents with LangChain & Autonomous Workflowsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAI Agents with LangChain & Autonomous Workflows学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 AI Agents with LangChain & Autonomous Workflowsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Problem with Free Text

LLMs return prose by default, but your code needs structured data: JSON, a list, a typed object. Parsing free text with regex is fragile.

This lesson covers getting reliable structured output from models.

Asking for a Format

The first step is simply instructing the model to produce a specific format. But instruction alone is not enough; models drift, add prose, or wrap output in markdown.

prompt = 'Extract name and age as JSON: "Lena is 30"'
# model might reply: 'Sure! {"name":"Lena","age":30}'

Output Parsers

LangChain output parsers do two jobs: they generate format instructions to inject into the prompt, and they parse the model's response back into a structured object.

from langchain.output_parsers import CommaSeparatedListOutputParser
parser = CommaSeparatedListOutputParser()
print(parser.get_format_instructions())

Schema-Based Parsing

Define the shape you want with a schema (e.g. a Pydantic model). The parser turns it into instructions and validates the result against the fields and types.

from pydantic import BaseModel
class Person(BaseModel):
    name: str
    age: int

Injecting Format Instructions

Add the parser's instructions into your prompt template so the model knows exactly what structure to emit.

template = 'Extract info.\n{format_instructions}\nText: {text}'
prompt = template.format(
    format_instructions=parser.get_format_instructions(),
    text='Lena is 30')

Parsing the Response

After the model replies, the parser converts the text into your typed object, raising an error if it does not match the schema.

result = parser.parse(model_output)
print(result.name, result.age)

Handling Malformed Output

Models occasionally produce invalid JSON. A retry/fixing parser detects the failure and asks the model to correct its own output, turning a hard crash into a recoverable step.

from langchain.output_parsers import RetryOutputParser
robust = RetryOutputParser.from_llm(parser=parser, llm=llm)

Native JSON / Tool Modes

Many modern models support a JSON mode or function/tool calling that constrains output to valid structured data at the API level. When available, this is far more reliable than prompt instructions alone.

Validation Beyond Types

A value can be the right type but still wrong: a negative age, an empty required field. Add validators so business rules are enforced, not just the data shape.

if result.age < 0 or result.age > 130:
    raise ValueError('age out of range')

Why It Matters for Agents

Agents chain steps together, feeding one output into the next. If a step emits malformed data, the whole chain breaks. Structured, validated output is what makes multi-step agents dependable.

A Reliable Output Workflow

Putting it together:

  • Define a schema for the data you need
  • Inject format instructions into the prompt
  • Prefer native JSON/tool mode when available
  • Parse and validate, with a retry parser as a safety net

Quick Check

Test your understanding of structured output.

Recap

You learned to get reliable structured data from LLMs.

  • Output parsers generate instructions and parse responses
  • Schemas validate shape and types
  • Retry parsers recover from malformed output
  • Native JSON/tool modes are most reliable when available
無料で開始

AI チューターと学ぶ AI Agents with LangChain & Autonomous Workflows — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
50

よくある質問

「構造化出力の解析と検証」レッスンは無料ですか?

はい。「構造化出力の解析と検証」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Agents with LangChain & Autonomous Workflowsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Agents with LangChain & Autonomous Workflowsコースには全4レッスンが含まれています。

「構造化出力の解析と検証」で何を学びますか?

出力パーサーとスキーマを使ってLLMに信頼できる構造化データを返させ、形式不正の出力が生成された場合は検証または再試行する方法を学びます。 ブラウザで直接実行するハンズオンコードでAI Agents with LangChain & Autonomous Workflowsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

AI Agents with LangChain & Autonomous Workflowsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAI Agents with LangChain & Autonomous Workflowsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「構造化出力の解析と検証」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAI Agents with LangChain & Autonomous Workflowsレッスンでコードを書いて実行できますか?

はい。すべてのAI Agents with LangChain & Autonomous Workflowsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 効果的なプロンプト設計手法
  2. LLMとLangChainの統合
  3. モデルパラメータとコストの管理
  4. 構造化出力の解析と検証
← AI Agents with LangChain & Autonomous Workflowsに戻る