Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
LLMの出力を構造化したい
Search
北村
January 29, 2026
0
210
LLMの出力を構造化したい
北村
January 29, 2026
Tweet
Share
More Decks by 北村
See All by 北村
Azure Container Appsを使ってみた
shu_kita
0
150
ギャルのパンティおくれ
shu_kita
1
150
LSMツリー
shu_kita
0
21
掲示板への不適切な投稿を防ぐ
shu_kita
0
150
正規表現エンジンを自作した話
shu_kita
0
17
Featured
See All Featured
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
220
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
750
SEO for Brand Visibility & Recognition
aleyda
0
4.2k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
370
Technical Leadership for Architectural Decision Making
baasie
1
230
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
55
50k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.1k
30 Presentation Tips
portentint
PRO
1
210
sira's awesome portfolio website redesign presentation
elsirapls
0
140
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
130
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%ではないが、一番良かった