当前位置: 首页 > news >正文

Structured Output Parser in LangChain

https://python.langchain.com.cn/docs/modules/model_io/output_parsers/structured

Structured Output Parser in LangChain

This content is based on LangChain’s official documentation (langchain.com.cn) and explains the StructuredOutputParser—a tool to extract structured data with multiple fields from LLM outputs—in simplified terms. It strictly preserves original source codes, examples, and knowledge points without arbitrary additions or modifications.

Key Note: While Pydantic/JSON parsers are more powerful, the StructuredOutputParser is ideal for simple data structures with only text fields (no complex validation or nested types).

1. What is StructuredOutputParser?

StructuredOutputParser lets you define multiple named fields (e.g., “answer” + “source”) and extract them as a structured dictionary from LLM outputs.

  • Use case: When you need the LLM to return both a direct answer and supporting information (e.g., a source URL) in an organized format.
  • Key feature: It generates clear format_instructions to guide the LLM to output data matching your desired fields, ensuring easy parsing into a Python dictionary.
  • Supports both standard LLMs (e.g., OpenAI) and chat models (e.g., ChatOpenAI).

2. Step 1: Import Required Modules

The code below imports all necessary classes—exactly as in the original documentation:

from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI

3. Step 2: Define Response Schemas

First, specify the fields you want the LLM to return (each ResponseSchema defines a field’s name and description).

Code (Exact as Original):

# Define the desired fields (name + description)
response_schemas = [ResponseSchema(name="answer", description="answer to the user's question"),ResponseSchema(name="source", description="source used to answer the user's question, should be a website.")
]

4. Step 3: Initialize the Structured Output Parser

Create the parser from the response schemas and get its auto-generated format instructions (guidelines for the LLM).

Code (Exact as Original):

output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()  # Guides LLM to output structured fields

Note: The format_instructions typically tells the LLM to output data like: {"answer": "Paris", "source": "https://en.wikipedia.org/wiki/Paris"}.

5. Example 1: Use with a Standard LLM (OpenAI)

Combine the parser with a standard LLM to extract structured data for a user’s question.

Step 5.1: Create a Prompt Template

prompt = PromptTemplate(template="answer the users question as best as possible.\n{format_instructions}\n{question}",input_variables=["question"],  # Dynamic user questionpartial_variables={"format_instructions": format_instructions}  # Fixed format guidelines
)

Step 5.2: Initialize the LLM and Generate Output

model = OpenAI(temperature=0)  # Fixed temperature for consistent results
_input = prompt.format_prompt(question="what's the capital of france?")  # Format prompt with user question
output = model(_input.to_string())  # Get LLM response

Step 5.3: Parse the Output into a Dictionary

parsed_output = output_parser.parse(output)
print(parsed_output)

Parsed Output (Exact as Original):

{'answer': 'Paris', 'source': 'https://www.worldatlas.com/articles/what-is-the-capital-of-france.html'}

6. Example 2: Use with a Chat Model (ChatOpenAI)

The same parser works with chat models—only the prompt formatting and output access change (chat models return Message objects, so we use .content).

Step 6.1: Create a Chat Prompt Template

chat_prompt = ChatPromptTemplate(messages=[HumanMessagePromptTemplate.from_template("answer the users question as best as possible.\n{format_instructions}\n{question}")],input_variables=["question"],partial_variables={"format_instructions": format_instructions}
)

Step 6.2: Initialize the Chat Model and Generate Output

chat_model = ChatOpenAI(temperature=0)
_input = chat_prompt.format_prompt(question="what's the capital of france?")
output = chat_model(_input.to_messages())  # Chat model expects a list of Message objects

Step 6.3: Parse the Chat Model Output

parsed_chat_output = output_parser.parse(output.content)  # Access content via .content
print(parsed_chat_output)

Parsed Output (Exact as Original):

{'answer': 'Paris', 'source': 'https://en.wikipedia.org/wiki/Paris'}

Key Takeaways

  • StructuredOutputParser simplifies extracting multiple text fields into a dictionary.
  • ResponseSchema defines each field’s name and purpose—critical for guiding the LLM.
  • Works with both standard LLMs (use .to_string()) and chat models (use .content).
  • Best for simple structured data (no complex validation) compared to Pydantic/JSON parsers.
http://www.dtcms.com/a/610970.html

相关文章:

  • 解释性语言和编译型语言的区别与不同 | 深入了解两者的特点与应用
  • asp网站镜像代码网站建设分金手指专业十六
  • 建设一个网站首先需要什么滕州做网站哪家好
  • 多场景服务机器人代理品牌深度解析
  • 建设网站答辩情况wordpress 搜索 范围
  • “干活”机器人“教练”登场:宇树机器人推出首款轮式机器人G1-D
  • 素马网站制作开发天猫交易购买平台
  • 增强AI编程助手效能:使用开源Litho(deepwiki-rs)深度上下文赋能iFlow
  • 郑州网站建设找三牛天津建设工程信息网评标专家 终审
  • LTE和5G NR中的PDSCH和PUSCH
  • 【HarmonyOS 6】静态和动态添加应用快捷方式详解
  • 手机网站模板更换方法设计上海网站建设
  • 手机网站竞价莆田外贸自建网站
  • 让设备 “开口说话”:设备间通信艺术与以太网温湿度传感器通信实现
  • 宝安做小程序有推荐吗wordpress 百度优化
  • HTML 页面跳转实现
  • 网站登录注册做验证码的目地开一家公司需要具备什么条件
  • CS8389、CS8390:防破音,AB/D,2×6.6W立体音频功率放大器
  • 精耕“智”作,中海达携农机导航亮相AGRITECHNICA 2025!
  • css文件中这个[特殊字符]读取出来是乱码
  • 做网站的原型文件下载wordpress购物app
  • 磁共振成像原理(理论)30:基本自旋回波成像 (Basic Spin-Echo Imaging)
  • 为什么 AI 推理天然更偏向 ARM(而不是 x86)
  • 广州网站建设哪里有国家信息公示网查营业执照年报
  • 网站 需求文档益阳市网站建设
  • 基于数据的人工智能建模流程及源码示例
  • 站长之家爱站网f006网站建设
  • 中国建设银行网上银行官方网站wordpress 隐私设置
  • 4网站建设软件工程师证书报考条件
  • Navicat把sqlserver数据库转到mysql