Langchain入门到精通0x01:结果解析器

Result Parser

上一篇 学习了什么是langchain和prompt templete。那么,很多时候大模型直接返回的数据并不能满足各个项目的具体需求,如何将大模型自由、非结构化的文本输出,转化为程序可稳定、高效处理的结构化数据

langchain提供了几个标准的接口来实现这个目的。今天就挨个来学习下几个常用的解析器接口。

StrOutputParser

字符串解析器。用于清理模型输出,去除多余换行等,返回纯净字符串。它是最常用的"兜底"解析器。

ini 复制代码
parser = StrOutputParser()
chain = prompt | model | parser

JsonOutputParser

Json格式解析器。

ini 复制代码
parser = JsonOutputParser()
chain = prompt | model | parser

CommaSeparatedListOutputParser

逗号分隔列表解析器。适用于生成标签、关键词、选项列表等场景。比如生成csv格式的常常也用到这个。

ini 复制代码
parser = CommaSeparatedListOutputParser()
chain = prompt | model | parser

DatetimeOutputParser

日期解析器将人类模糊、灵活的自然语言时间描述,精准、可靠地转换为程序可计算的 datetime对象

ini 复制代码
parser = DatetimeOutputParser()
chain = prompt | client | parser

PydanticOutputParser

基于Pydantic模型的结构化解析 。这是企业级应用中最核心、最推荐的解析器。它通过定义严格的Pydantic数据模型来确保输出结构的质量和类型安全。

python 复制代码
# 1. 定义你的结构化数据模型
class MovieReview(BaseModel):
    title: str = Field(description="电影标题")
    year: int = Field(description="上映年份")
    rating: float = Field(description="评分,0-10分")
    tags: List[str] = Field(description="电影标签")

    @validator('year')
    def year_must_be_valid(cls, v):
        if v > 2025:
            raise ValueError('年份不能超过2025年')
        return v

# 2. 创建解析器,并注入格式指令到提示词
parser = PydanticOutputParser(pydantic_object=MovieReview)
prompt = PromptTemplate(
    template="请根据以下影评文本,提取信息。\n{format_instructions}\n文本:{review}\n",
    input_variables=["review"],
    partial_variables={"format_instructions": parser.get_format_instructions()}
)

model = get_lc_model_client()
# 3. 构建并执行链
chain = prompt | model | parser
review_text = "《流浪地球2》于2023年上映,是一部宏大的科幻灾难片,我认为可以打9.3分,它包含了科幻、灾难和爱国情怀等元素。"
try:
    result = chain.invoke({"review": review_text})
    print(result)
    print("--" * 10)
    print(f"标题:{result.title}")
    print(f"年份:{result.year}")
    print(f"标签:{result.tags}")
except Exception as e:
    print(f"解析失败,模型输出格式可能不符合要求:{e}")

CustomKeyValueParser

自定义解析器 。是 "面向极端或遗留格式的自由扩展" ​ 的逃生通道。当标准解析器或Pydantic模型无法描述你的特殊输出格式时,它给你底层控制权。

当你的输出格式无法用JSON描述,或需要复杂后处理时,就需要继承 BaseOutputParser[T]

  • parse:核心重写方法。可以在这里写任何Python逻辑:正则匹配、多轮解析、调用外部API清洗数据等。
  • get_format_instructions是指令生成器:清晰、无歧义地告诉模型你期望的精确文本格式。
python 复制代码
class MixedFormatParser(BaseOutputParser[dict]):
    """解析混合格式:第一部分是键值对,第二部分是列表"""
    def get_format_instructions(self) -> str:
        return (
            "严格按以下格式回答:\n"
            "Key1: Value1\n"
            "Key2: Value2\n"
            "---\n"
            "Item1, Item2, Item3"
        )

    def parse(self, text: str) -> dict:
        # 1. 分割上下两部分
        if "---" not in text:
            raise ValueError("缺少分隔符'---'")
        header_section, list_section = text.split("---", 1)

        # 2. 解析键值对部分(自定义逻辑)
        key_value_dict = {}
        for line in header_section.strip().split("\n"):
            if ": " in line:
                key, value = line.split(": ", 1)
                key_value_dict[key.strip()] = value.strip()

        # 3. 解析列表部分(自定义逻辑)
        item_list = [item.strip() for item in list_section.strip().split(",")]

        # 4. 返回结构化数据
        return {
            "metadata": key_value_dict,
            "items": item_list
        }

    @property
    def _type(self) -> str:
        return "mixed_format_parser"

如何抉择

  • 优先使用 PydanticOutputParser :当你的输出结构是已知的、可定义的,尤其是需要与其他系统(数据库、API)交换数据时。它覆盖了90%的企业应用场景。

  • 考虑自定义解析器:仅当遇到以下情况:

    • 输出格式是非JSON的特定文本格式(如固定宽度的日志、特定的CSV变体、 legacy系统接口)。
    • 需要复杂的后处理,例如从大段文本中提取多个实体并建立关系图。
    • 模型输出包含多个逻辑部分,需要用截然不同的规则解析。
相关推荐
Dawson Zhu1 小时前
工作流与 Agent 的工程选型:从“控制权归属“看 LLM 应用架构
人工智能·语言模型·架构·aigc·agi
计算机源码社1 小时前
【大数据项目实战】基于大数据的影视内容生态综合质量分析与可视化-基于数据挖掘的影视内容类型共现与口碑聚类分析系统
大数据·人工智能·python·数据挖掘·数据分析·毕业设计·课程设计
C^h2 小时前
pytorch 适合初学者 0基础学习
人工智能·pytorch·python
Rocky Ding*2 小时前
【三年面试五年模拟】2026-09-06 拼多多 AI Agent研发岗秋招笔试4道算法题完整题解
论文阅读·人工智能·深度学习·机器学习·aigc·ai-native·拼多多
小柯南敲键盘2 小时前
跨马翻译:AI批量图片翻译工具,跨境电商视频字幕翻译与智能抠图一体搞定
人工智能·python·音视频
luckystar513~2 小时前
Geo + AI:【时空智能体】技术剖析
人工智能·ai·gis·geoai·空间智能体·时空智能体
吨吨ai3 小时前
2026年9月8日|GPT‑6 Astra + Codex:Pro 开发者的 AI Agent 工具链
人工智能·gpt
leoZ2313 小时前
2026-09-09-springboot-cloud-deploy-pitfalls
java·前端·javascript·vue.js·人工智能·spring boot·后端
dozenyaoyida3 小时前
AI与大模型新闻日报 | 2026-09-09
人工智能·ai·chatgpt·大模型·新闻
xqqxqxxq3 小时前
AI Agent学习:主动工具发现(李博杰《深入理解 AI Agent》4.8观后总结)
人工智能·学习