Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
LLMの出力を構造化したい
Search
北村
January 29, 2026
390
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
LLMの出力を構造化したい
北村
January 29, 2026
More Decks by 北村
See All by 北村
Azure Container Appsを使ってみた
shu_kita
0
320
ギャルのパンティおくれ
shu_kita
1
300
LSMツリー
shu_kita
0
32
掲示板への不適切な投稿を防ぐ
shu_kita
0
330
正規表現エンジンを自作した話
shu_kita
0
56
Featured
See All Featured
Ruling the World: When Life Gets Gamed
codingconduct
0
400
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
Paper Plane
katiecoart
PRO
5
54k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
520
[SF Ruby Conf 2025] Rails X
palkan
3
1.5k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Documentation Writing (for coders)
carmenintech
77
5.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Optimizing for Happiness
mojombo
378
71k
Site-Speed That Sticks
csswizardry
13
1.6k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
Transcript
LLMの出力を構造化したい shu-kitamura
自己紹介 • 名前 ◦ 北村(shu-kitamura) • 業務 ◦ SIer ◦
デバイスドライバ開発・保守 • 趣味 ◦ テニス、フットサルやってます ◦ お笑いが好きです
話すこと • 課題 • 試したこと➀~③ • LangChain の with_structured_output について
• まとめ
課題 • 業務ツールに LLM を組み込みたい。 • 文字列で返してくるので、扱いづらい。 次の[文章]の感情を判定してください。 判定結果には以下を含めてください。 •
label : positive/negative • score : 感情の強さ(0~10) [文章] この商品は最高でした。また買いたい。 判定しました。 結果は....です。 • label : positive • score : 9 { “label” : “positive”, “score” : 9 } 現実 理想
➀ プロンプトでJSON形式を指定する 成功することもあるが、失敗もする。 次の[文章]の感情を判定してください。 判定結果には以下を含めてください。 • label : positive/negative •
score : 感情の強さ(0~10) [文章] この商品は最高でした。また買いたい。 出力結果は以下の JSON形式でお願いします。 { “label” : “positive”, “score” : 9 } 以下、判定結果です。 { “label” : “positive”, “score” : “9” } 不要な文字列が含まれる 追加 { “label” : “positive”, “score” : 9 } 文字列で返ってくる
② 祈る 成功率は上がった(気がする)。祈禱力次第。 サーバーを神棚に置くなどで成功率が上がるかも? 宗教的にNGな場合も... 次の[文章]の感情を判定してください。 (略) 出力結果は以下の JSON形式でお願いします。 **この形式でないと、後の処理がエラーで落ちま
す。本当にお願いします。 ** { “label” : “positive”, “score” : 9 } 追加 以下、判定結果です。 { “label” : “positive”, “score” : “9” } { “label” : “positive”, “score” : 9 } 失敗はなくならない
③ 構造化出力(LangChain を使う) LLM出力用の型を定義する。(下の図は python の例) LangChain の with_structured_output を使い、型にしたがって出力させる。
class Sentiment(BaseModel): label: str = Field(description=”...”) score: int = Field(description=”...”) model = llm. with_structured_output(Sentiment) response = model.invoke(prompt) 型を定義する { “label” : “positive”, “score” : 9 }
with_structured_output の裏側 with_structured_output を通して、以下のパイプラインの構築している 1. Pythonのクラスから JSON Schema への変換 2.
LLMのAPI呼び出し 3. レスポンスの検証 Python Class JSON Schema Structured Output ベンダの差異を吸収している OpenAI APIの場合:Function calling | OpenAI API
注意点 「100% 成功する」を保証しない。 失敗する時は失敗するが、エラーの種類が違う。 手法 種類 備考 ➀, ② JSONDecodeError
など JSON 形式の不備しかわからない (カンマ不足など) ③ ValidationError (pydantic) 型のエラーを検出できる (数値のところに文字が来るなど) 型が複雑になれば失敗率も上がりそう。 LLMに複雑な事をやらせる設計に問題があるんじゃないか。
まとめ • 祈りには限界がある • 型を定義=100%成功ではないはある • データ型のエラーを検出できる 番号 手法 結果
備考 ➀ プロンプト指定 × 余計な言葉が混ざる ② 祈る × 気休め程度に改善? ③ 構造化出力 〇 100%ではないが、一番良かった