ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

如何在 agno 中让 Agent 按 Pydantic 模型返回结构化输出?

如何在 agno 中让 Agent 按 Pydantic 模型返回结构化输出? 如何在 agno 中让 Agent 按 Pydantic 模型返回结构化输出【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno当你用 agno 构建 Agent 时agent.run()默认返回的是一段自由文本。如果你的下游代码需要按字段读取结果比如把新闻摘要拆成主题、要点、情绪几个字段就需要让 Agent 按你定义的 Pydantic 模型返回结构化数据。agno 中对应的参数是 Agent 的output_schema定义一个继承自pydantic.BaseModel的类传给 Agent运行后run.content就会返回匹配该模型的结构化数据。本文以仓库中 output_schema.py 为例走通从定义模型到拿到结构化结果的完整路径。准备条件以下前置步骤来自 02_input_output 目录的 README用direnv allow加载环境变量其中必须包含OPENAI_API_KEYcookbook 示例使用 OpenAI 模型用./scripts/demo_setup.sh创建 demo 虚拟环境之后所有示例都通过.venvs/demo/bin/python运行。direnv allow ./scripts/demo_setup.sh第一步定义 Pydantic 输出模型结构化输出的形状完全由你写的 Pydantic 模型决定。在 output_schema.py 中作者定义了一个BreakingNewsSummary模型用Field(..., description...)为每个字段写上描述——这些描述会进入模型可见的 schema直接影响字段内容的生成质量from typing import List from pydantic import BaseModel, Field class BreakingNewsSummary(BaseModel): topic: str Field(..., descriptionThe topic or region being summarized) summary: str Field( ..., descriptionA concise summary of the latest developments ) key_updates: List[str] Field( ..., descriptionImportant updates or headlines related to the topic ) overall_sentiment: str Field( ..., descriptionOverall tone of the news coverage, such as positive or mixed )字段类型按需选择示例中用了str和List[str]。parser_model.py 的示例还展示了带约束的整数字段写法例如Field(..., ge1, le5)限制评分在 1 到 5 之间以及default[]提供默认值。第二步把模型传给 Agent 的 output_schema创建 Agent 时把模型类不是实例赋给output_schema参数from agno.agent import Agent, RunOutput from agno.models.openai import OpenAIResponses agent Agent( modelOpenAIResponses(idgpt-5.2), descriptionYou summarize current events into clean structured outputs., output_schemaBreakingNewsSummary, )运行并验证结果按 cookbook README 给出的方式运行.venvs/demo/bin/python cookbook/02_agents/02_input_output/output_schema.py示例脚本的完整运行入口是if __name__ __main__: run: RunOutput agent.run(Latest news from France?) pprint(run.content)验证方式运行后run.content返回的是匹配BreakingNewsSummary的结构化数据topic、summary、key_updates、overall_sentiment四个字段而不是自由文本。output_schema.py的说明明确写着该参数用于return structured data that matches a Pydantic model。可选分支用 parser_model 指定解析模型parser_model.py 展示了在output_schema基础上再传一个parser_model的写法agent Agent( modelOpenAIResponses(idgpt-5.2), descriptionYou help people plan amazing national park adventures and provide detailed park guides., output_schemaNationalParkAdventure, parser_modelOpenAIResponses(idgpt-5.2), )这里parser_model是负责产出结构化 JSON 的模型。从源码 libs/agno/agno/agent/_messages.py 可以看到两条对应的行为当output_schema已提供、但主模型不支持原生结构化输出或 JSON schema 输出时agno 会向系统消息追加一段 JSON 输出提示词当设置了parser_model且output_schema是 Pydantic 模型而非 dict时会追加 response model format 提示词。也就是说即使所用模型没有原生结构化输出能力output_schema也能通过提示词注入方式工作。与相近参数的区分input_schema约束输入而不是输出。input_schema.py 中 Agent 的input_schemaResearchTopic要求调用方传入匹配该模型的 dict 或 Pydantic 实例运行方式变成agent.print_response(input{...})。output_model不是结构化输出参数。output_model.py 的说明指出它用另一个模型替换主模型的最终回答例如便宜模型做推理、强模型润色其 docstring 明确建议结构化 JSON 输出请改用 parser_model。expected_output只是用自然语言提示期望的响应形态见 expected_output.py不保证字段结构不适用于需要按字段读取的场景。小结要让 agno 的 Agent 按 Pydantic 模型返回结构化输出定义BaseModel子类并给字段写好description→ 传给Agent(output_schema...)→agent.run(...)后用run.content按字段取用。需要指定解析模型时再加parser_model输入侧的结构化校验则用input_schema。更多变体流式、保存文件、模型替换等都可以在 02_input_output 目录 下找到对应示例。【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表