You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Langchain的StructuredParser输出嵌套JSON(字典列表)

如何用LangChain的StructuredOutputParser解析出字典列表格式结果

我正在使用LangChain的StructuredOutputParser,目前解析器仅返回扁平字典,需要配置输出解析器以获取字典列表形式的结果。

现有代码

PROMPT_TEMPLATE = """ 
You are an android developer. 
Parse this error message and provide me identifiers & texts mentioned in error message. 
--------
Error message is {msg}
--------
{format_instructions}
"""

def get_output_parser():
    missing_id = ResponseSchema(name="identifier", description="This is missing identifier.")
    missing_text = ResponseSchema(name="text", description="This is missing text.")

    response_schemas = [missing_id, missing_text]
    output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
    return output_parser


def predict_result(msg):
    model = ChatOpenAI(open_api_key="", openai_api_base="", model="llama-2-70b-chat-hf", temperature=0, max_tokens=2000)
    output_parser = get_output_parser()
    format_instructions = output_parser.get_format_instructions()
    
    prompt = ChatPromptTemplate.from_template(template=PROMPT_TEMPLATE)
    message = prompt.format_messages(msg=msg, format_instructions=format_instructions)
    response = model.invoke(message)

    response_as_dict = output_parser.parse(response.content)
    print(response_as_dict)


predict_result("ObjectNotFoundException AnyOf(AllOf(withId:identifier1, withText:text1),AllOf(withId:identifier2, withText:text1),AllOf(withId:identifier3, withText:text1))")

当前输出

{
    "identifier":"identifier1",
    "text":"text1"
}

期望输出

[
    {
        "identifier":"identifier1",
        "text":"text1"
    },
    {
        "identifier":"identifier2",
        "text":"text1"
    },
    {
        "identifier":"identifier3",
        "text":"text1"
    }
]

解决方案

要让StructuredOutputParser返回字典列表,需要定义嵌套的ResponseSchema,指定顶层结构为包含子元素的列表:

  1. 修改get_output_parser函数,创建一个顶层的ResponseSchema,将其type设为"array",并通过child_schemas指定列表中每个字典的结构。
  2. 微调prompt模板,明确告知模型需要返回所有匹配的条目组成的列表。

修改后的完整代码

PROMPT_TEMPLATE = """ 
You are an android developer. 
Parse this error message and extract ALL identifiers & texts mentioned in it. Return them as a list of objects, each containing 'identifier' and 'text'.
--------
Error message is {msg}
--------
{format_instructions}
"""

def get_output_parser():
    # 定义列表中每个元素的字段结构
    item_id = ResponseSchema(name="identifier", description="Missing identifier from error message")
    item_text = ResponseSchema(name="text", description="Corresponding text for the identifier")
    # 定义顶层的列表结构
    missing_items = ResponseSchema(
        name="missing_items",
        description="List of all missing identifier-text pairs extracted from the error",
        type="array",
        child_schemas=[item_id, item_text]
    )
    output_parser = StructuredOutputParser.from_response_schemas([missing_items])
    return output_parser


def predict_result(msg):
    model = ChatOpenAI(open_api_key="", openai_api_base="", model="llama-2-70b-chat-hf", temperature=0, max_tokens=2000)
    output_parser = get_output_parser()
    format_instructions = output_parser.get_format_instructions()
    
    prompt = ChatPromptTemplate.from_template(template=PROMPT_TEMPLATE)
    message = prompt.format_messages(msg=msg, format_instructions=format_instructions)
    response = model.invoke(message)

    response_as_dict = output_parser.parse(response.content)
    # 提取并打印目标列表
    print(response_as_dict["missing_items"])


predict_result("ObjectNotFoundException AnyOf(AllOf(withId:identifier1, withText:text1),AllOf(withId:identifier2, withText:text1),AllOf(withId:identifier3, withText:text1))")

关键说明

  • 顶层ResponseSchema的type="array"标记告诉解析器这是一个列表结构,child_schemas定义了列表内每个字典的字段规则。
  • 模板中明确要求返回所有匹配条目,避免模型仅提取第一个结果。
  • 最终解析结果会是一个包含missing_items键的字典,其对应的值就是期望的字典列表。

内容的提问来源于stack exchange,提问作者HarshIT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:44:57