
前言上一篇我们分享了LangChain框架大模型的初始化和调用方式本文将分享LangChain框架大模型三种结构化输出方式解决大模型返回自由文本无法被程序直接解析的痛点。大模型输出结果如果不做约束大模型输出往往夹杂冗余描述很难对接数据库、后端接口或者前端页面通过预定义规范的数据结构Pydantic 对象、字典、JSON大模型的输出结果就可以无缝的集成到系统中。下面介绍大模型结构输出的三种方式Pydantic 模型、TypedDict、JSON Schema一、大模型结构化输出1.1 Pydantic 模型Pydantic 是一套基于 Python 类型注解的数据校验库运行时自动校验字段类型适合结构复杂、生产环境需要强数据校验的业务场景常用于 API 返回、表单校验等场景。 使用方式自定义类继承BaseModel搭配类型注解与Field描述字段含义。1) 输出单结构from pydantic import BaseModel, Field from init_model import deepseek class Movie(BaseModel): A movie with details. title: str Field(description电影标题) year: int Field(description发行年份) director: str Field(description电影导演) rating: float Field(description电影评分1-10分) model_with_structure deepseek.with_structured_output(Movie) response model_with_structure.invoke(Provide details about the movie Inception) print(response)输出内容class __main__.Movie titleInception year2010 directorChristopher Nolan rating8.82嵌套复杂结构from pydantic import BaseModel, Field from typing import List #1. 定义第一层结构 class Actor(BaseModel): name: str Field(description演员姓名) role: str Field(description饰演的角色) #2、嵌套第一层结构 class Movie(BaseModel): title: str Field(description电影标题) year: int Field(description发行年份) director: str Field(description电影导演) cast: List[Actor] Field(description演员列表) rating: float Field(description评分1-10分) #3. 模型绑定输出结构 structured_model deepseek.with_structured_output(Movie) #4. 调用模型 response structured_model.invoke(请介绍电影《盗梦空间》) #5. 访问嵌套数据 print(5response) print(f电影名: {response.title}) print(f演员列表: {response.cast})1.2 TypedDictTypedDict用来定义带类型提示的字典对象不需要额外的实体类依赖适合快速调试、轻量场景最终返回原生dict。from typing_extensions import TypedDict, Annotated from init_model import deepseek class MovieTypedDict(TypedDict): title: Annotated[str, 电影标题] year: Annotated[int, 上映年份] director: Annotated[str, 导演] rating: Annotated[float,评分10分制] # 模型绑定输出结构 model_with_structure deepseek.with_structured_output(MovieTypedDict) # 调用模型 resp model_with_structure.invoke(Provide details about the movie Inception) print(type(resp)) print(resp)输出结果class dict {title: Inception, year: 2010, director: Christopher Nolan, rating: 8.8}1.3 JsonSchema遵循JSON 对象描述规范手写JSON对象不依赖任何 Python 库from init_model import deepseek json_schema { title: Movie, #不能使用中文 description: 电影信息, type: object, properties: { title: {type: string, description: 电影标题}, year: {type: integer, description: 发行年份}, director: {type: string, description: 导演}, rating: {type: number, description: 电影评分1-10分}, cast: { type: array, items: { type: object, properties: { name: {type: string, description: 演员姓名}, role: {type: string, description: 角色}, }, required: [name, role], }, description: 演员列表, }, }, required: [title, year, director, rating, cast], } #模型绑定结构化输出 jsonSchema model_with_structured_output deepseek.with_structured_output(json_schema) response model_with_structured_output.invoke(Provide details about the movie Inception) print(type(response)) print(response)二、总结1、上述三种结构化输出方式推荐使用Pydantic模型输出方式自带强类型校验支持多层嵌套代码可读性最好2、三种方式统一使用with_structured_output()绑定输出格式后再调用模型3、使用 DeepSeek Pydantic 结构化输出时必须关闭思考模式否则接口返回 400 报错4、如果需要同时拿到格式化对象 模型原始返回消息即AIMessage可以增加参数include_rawTrue用于调试。LangChain框架往期系列文章LangChain 从入门到实战系列一框架了解与开发初体验-CSDN博客LangChain 从入门到实战系列二Model初始化及调用方式实操-CSDN博客下一篇预告后续将分析Agent原理、创建及调用方式欢迎持续关注交流讨论三种结构化方案你项目里会优先选哪一个 生产环境 Pydantic 强校验还是快速调试用 TypedDict 有没有在 DeepSeek 或者其他国产模型上踩过结构化输出的坑欢迎评论区留言交流。 如果本文对你开发 AI 应用有帮助欢迎点赞收藏 关注持续更新 LangChain 全套实战教程