Create

Sign in to ReadmeX

or

How we handle your data: Privacy Policy

ReadmeX

Your AI reading companion

What would you like to catch up on?

Pick a briefing, or ask your own question.

Your community and people briefings stay personal to you.

Story

Using Function Calling for Reliable Structured LLM Output

AI summary

This tutorial shows how to use LangChain's with_structured_output with a Pydantic schema to turn an LLM's free-form response into a validated, typed object. It contrasts function calling with JSON mode and JSON Schema, then integrates the result into a LangGraph state. The author also notes that production use still requires retries, error handling, and attention to schema token overhead.

In-depth insight

The article provides a concrete implementation pattern for developers who need structured LLM data without manually cleaning code fences, parsing strings, and checking fields or enum values.

本系列用可运行的最小 demo 讲 LangGraph 实战。这是第 5 讲:让 LLM 稳定输出 JSON 的可靠做法——不是让它"输出 JSON",而是把"交卷"伪装成一次 tool call,用 Pydantic schema 当合同逼它返回合法、类型正确的对象。

TL;DR:直接让 LLM"输出 JSON"不可靠(markdown 代码围栏、废话、漏字段、类型漂移)。可靠做法是把"交卷"伪装成 tool call——model.with_structured_output(Schema) 一行,Pydantic schema 就是合同,返回类型化对象,零手工解析。


正文

§1 为什么「输出 JSON」不可靠

朴素"输出 JSON"不可靠——拿到的是字符串,json.loads 只保证"语法合法"、不保证"语义正确",字段、类型、枚举全无合同约束。

#LangChain#Pydantic#LangGraph#OpenAI#DeepSeek

Original · 11 min readRead the original
Group chat

No comments yet. Start the conversation.