解析 strands-agents Python SDK v0.1.9:从结构化输出到可观测性与 A2A 集成的版本演进
人工智能大模型AI AgentAgent 框架多智能体工具调用MCP 服务【免费下载链接】harness-sdkBuild an agent harness and control it end-to-end. Open-source SDK for production AI agents in Python TypeScript - any model, any cloud.项目地址https://gitcode.com/GitHub_Trending/sdkpython13/harness-sdk点击查看免费下载版本速览本篇文章基于 strands-agentsPython SDKv0.1.92025-06-24 发布的官方变更日志见 site/src/content/changelog/sdk/python-v0.1.9.md逐条解析该版本的 11 项变更。本文以变更日志为骨架深入到当前仓库中strands-py的源码与集成测试说明每项变更背后的实现原理、API 形态与实战用法。读完本文你将掌握如何用 Pydantic 模型驱动 Agent 的结构化输出、如何开启 Meter 指标采集、如何理解 OpenAI 图像 base64 处理链路、A2A 服务端集成方式以及tool装饰器返回对象的新语义。一、版本概览v0.1.9 变更总览本次发布共有 11 条变更记录涵盖 3 个feat新增功能、5 个other其他改动、2 个chore维护性改动和 1 个fix缺陷修复全部为非破坏性变更breaking: false意味着已有代码可以平滑升级。类型标题涉及领域PRfeatadd meter可观测性otel#219featadd structured output support using Pydantic models结构化输出#60featUpdate tool to return an AgentTool that also acts as a functiontool#258othermodels - openai - images - b64 validatemodel#251othermodels - openai - b64encode methodmodel#260otherchore/update metricsotel#248otheriterative streamingbidirectional-streaming#241otherInitial A2A server Integrationa2a#218otherlitellm - bug in v1.73.0model#270choreInline event loop helper functions—#222fixEmit warning that default region behavior will be changing—#254从源码结构看v0.1.9 的几项重点能力结构化输出、Meter、A2A在 strands-py/src/strands 目录下都有对应的独立实现模块且均有配套集成测试如 test_structured_output_agent_loop.py。下文按功能主线分别展开。二、结构化输出用 Pydantic 模型约束 Agent 的返回格式PR #60这是 v0.1.9 最重要的能力之一基于 Pydantic 模型的结构化输出支持。它把“让模型按固定 Schema 返回 JSON”这件事从提示词工程升级为可校验的工具调用流程。2.1 使用方法两种调用姿势姿势一单次调用传入模型推荐API 更现代在Agent的run方法上直接传入structured_output_model返回结果对象上通过.structured_output属性读取解析后的 Pydantic 实例from pydantic import BaseModel from strands import Agent class UserProfile(BaseModel): name: str age: int occupation: str agent Agent() result agent( Create a profile for John Doe who is a 25 year old dentist, structured_output_modelUserProfile, ) profile result.structured_output # 类型为 UserProfile assert profile.name John Doe assert profile.age 25 assert profile.occupation.lower() dentist以上用法在 test_structured_output_agent_loop.py 的test_simple_structured_output中有完整验证不仅断言structured_output非空还逐字段校验了解析结果。姿势二在Agent构造时声明默认模型agent Agent(structured_output_modelUserProfile)此时“所有 agent 调用都会尝试返回该类型的结构化输出”。该参数在 agent.py 中被定义为构造参数并以self._default_structured_output_model保存见 agent.py单次调用传入的模型优先级更高会覆盖默认值见 agent.py。2.2 底层原理工具调用 校验闭环从源码看结构化输出不是简单的 JSON 解析而是一条完整的工具链路Schema 转换StructuredOutputTool把 Pydantic 模型转换为工具规范ToolSpec转换逻辑在 structured_output_utils.py 的convert_pydantic_to_tool_spec中实现转换结果带进程级缓存_TOOL_SPEC_CACHE见 structured_output_tool.py避免同一模型重复计算。工具注入StructuredOutputContext负责在运行时向工具注册表动态注册/注销该工具register_tool/cleanup见 _structured_output_context.py并通过is_enabled()判断当前调用是否需要结构化输出。模型调用工具描述中明确要求模型“作为返回结果前的最后一个工具调用”见 structured_output_tool.py迫使模型以工具参数的形式输出 JSON。校验与纠错stream()方法用self._structured_output_type(**tool_input)做实例化校验若 Pydantic 抛ValidationError会把逐字段错误信息以status: error的 ToolResult 回传给模型让其自行修正后重试见 structured_output_tool.py校验成功后结果存入StructuredOutputContext.store_result。结果提取extract_result从工具调用中取出最终 Pydantic 实例挂到返回结果的structured_output字段上。2.3 与工具联用及流式场景v0.1.9 的结构化输出可以和普通工具共存集成测试test_tool_use_with_structured_output展示了Agent(tools[calculator])配合structured_output_modelMathResult的场景见 test_structured_output_agent_loop.py。流式调用同样支持test_streaming_with_structured_output覆盖了该路径见 test_structured_output_agent_loop.py。注意仓库中旧的Agent.structured_output(...)/structured_output_async(...)方法已在源码中标记为deprecated源码提示直接改用structured_output_model参数见 agent.py升级到 v0.1.9 后应优先使用新 API。三、新增 MeterAgent 运行指标采集PR #219 PR #248v0.1.9 引入了 OpenTelemetryMeter指标支持与既有 Tracer追踪共同构成完整的可观测性能力。3.1 开启方式通过StrandsTelemetry.setup_meter一键开启默认两个导出通道都关闭需显式打开from strands import StrandsTelemetry # 同时开启控制台与 OTLP 指标导出 StrandsTelemetry().setup_meter( enable_console_exporterTrue, enable_otlp_exporterTrue, )实现位于 telemetry/config.py控制台使用PeriodicExportingMetricReader(ConsoleMetricExporter())OTLP 使用PeriodicExportingMetricReader(OTLPMetricExporter())随后构造MeterProvider并设为全局 provider。配置失败仅记录日志、不会抛出异常见 telemetry/config.py。3.2 指标清单MetricsClient单例内部采用双重检查锁保证线程安全见 telemetry/metrics.py在create_instruments中注册了如下指标名称常量见 telemetry/metrics_constants.py指标名类型单位语义strands.event_loop.cycle_countCounterCount事件循环周期总数strands.event_loop.start_cycleCounterCount周期开始次数strands.event_loop.end_cycleCounterCount周期结束次数strands.event_loop.cycle_durationHistograms单周期耗时strands.event_loop.latencyHistogramms周期延迟strands.event_loop.input.tokensHistogramtoken输入 token 数strands.event_loop.output.tokensHistogramtoken输出 token 数strands.event_loop.cache_read.input.tokensHistogramtoken缓存命中读取token 数strands.event_loop.cache_write.input.tokensHistogramtoken缓存写入 token 数strands.model.time_to_first_tokenHistogramms首 token 延迟strands.tool.call_countCounterCount工具调用次数strands.tool.success_countCounterCount工具成功次数strands.tool.error_countCounterCount工具失败次数strands.tool.durationHistograms工具执行耗时其中事件循环类指标围绕 event_loop.py 的event_loop_cycle执行过程打点工具类指标覆盖调用、成功、失败与耗时四个维度可直接用于构建 Agent 的 SLO 监控与成本核算面板。四、OpenAI 模型图像处理加固PR #251 PR #260v0.1.9 对 OpenAI 模型的图像输入处理做了两项加固均位于 models 领域models - openai - images - b64 validate#251为图像内容块引入 base64 校验在发送前验证图像数据是否为合法 base64 编码避免把无效数据直接发给模型导致请求失败。models - openai - b64encode method#260新增b64encode方法提供统一、显式的 base64 编码入口将“原始字节 → base64 字符串”的转换集中封装供多模态调用链路复用。这两项改动配合构成了多模态输入从“编码 → 校验 → 发送”的完整防御链先编码确保格式统一再校验确保数据合法从而提升多模态请求的健壮性。适用于任何通过 OpenAI 兼容接口上传图片如截图、文档扫描件的 Agent 场景。五、A2A 服务端集成Agent 互联互通的第一步PR #218v0.1.9 完成了A2AAgent-to-Agent服务端的初始集成。A2A 协议用于让不同 Agent 之间通过标准化的 Agent Card 与任务接口互相发现、调用。仓库中的实现位于 strands-py/src/strands/multiagent/a2a/server.pyA2AServer类建立在a2aPython 生态之上基于FastAPI暴露服务支持A2AFastAPIApplication/A2AStarletteApplication两种应用形态见 server.py提供public_agent_card()生成符合协议的 Agent Card工具列表会转换为AgentSkill_get_skills_from_tools见 server.py使用InMemoryTaskStore管理任务状态并支持enable_a2a_compliant_streaming开启符合 A2A 规范的流式响应见 server.py。配套的集成测试见 tests_integ/a2a/a2a_server.py 与 tests_integ/test_a2a_executor.py。需要说明的是该版本标注为“Initial Integration”属于起步能力主要打通了服务端发布 Agent Card、接收任务的骨架多 Agent 编排的完整能力仍在演进中。六、工具模型调整tool 返回“可调用的 AgentTool”PR #258v0.1.9 调整了tool装饰器的返回语义现在返回的AgentTool同时也可作为普通函数直接调用。这一变化对使用者意味着同一个被tool装饰的函数既可以注册进 Agent 的工具列表保持原来的AgentTool形态又可以在自己的业务代码里像普通 Python 函数一样直接调用无需额外取内部函数。从源码结构看这是为了让工具对象同时满足“供模型调用的 Schema 描述”与“供本地代码直接执行的函数”两种身份减少样板代码。此改动为非破坏性变更原有将返回值当作工具注册的用法不受影响。七、流式、事件循环与告警类改动PR #241 / #222 / #254 / #2707.1 双向流式iterative streamingPR #241bidirectional-streaming领域的iterative streaming改动增强了双向流式通信的迭代式处理能力。从仓库结构看双向流式相关实现与测试集中在 tests_integ/bidi 目录含 11 个 Python 测试文件该改动属于流式管道的渐进式完善。7.2 事件循环辅助函数内联PR #222Inline event loop helper functions是一次代码整理将原先散落的事件循环辅助函数直接内联到事件循环模块中。当前 strands-py/src/strands/event_loop/event_loop.py 中的_check_limits、_estimate_input_tokens、_build_checkpoint_stop_event等辅助函数即为该次整理后的形态属于无行为变化的维护性重构。7.3 默认区域行为变更告警PR #254该fix会在默认区域region相关行为即将变化时发出告警日志提前提示用户调整配置避免未来版本切换默认值时产生意外。属于“先行预警、后行变更”的渐进式兼容策略。7.4 litellm v1.73.0 兼容性处理PR #270针对 litellmv1.73.0中暴露的 bug 做了兼容性规避litellm - bug in v1.73.0。这是模型适配层的防御性修复确保使用该 litellm 版本时模型调用链路不受影响。八、升级建议与验证升级到 v0.1.9 的建议动作清单结构化输出新代码优先使用structured_output_model参数单次或构造时传入不要再使用已废弃的Agent.structured_output()系列方法可观测性需要指标监控时调用StrandsTelemetry().setup_meter(enable_console_exporterTrue, enable_otlp_exporterTrue)开启并按需消费第三节的指标表多模态接入 OpenAI 图像输入时可依赖新增的 base64 编码/校验链路保证图片数据合法性A2A需要对外暴露 Agent 时可基于A2AServer搭建初始服务关注后续版本的协议完善告警处理留意默认区域行为变更告警提前为未来默认值调整做准备。验证方式仓库内已有覆盖上述能力的集成测试可参照 test_structured_output_agent_loop.py结构化输出与 test_a2a_executor.pyA2A编写自己的回归用例。本变更日志全文见 site/src/content/changelog/sdk/python-v0.1.9.md各 PR 对应的核心实现均可在 strands-py/src/strands 下找到对应模块继续深入阅读。赞分享人工智能大模型AI AgentAgent 框架多智能体工具调用MCP 服务【免费下载链接】harness-sdkBuild an agent harness and control it end-to-end. Open-source SDK for production AI agents in Python TypeScript - any model, any cloud.项目地址https://gitcode.com/GitHub_Trending/sdkpython13/harness-sdk点击查看免费下载相关推荐ok-ww 鸣潮挂机指南后台自动刷图的完整设置流程ok ww 鸣潮挂机指南后台自动刷图的完整设置流程 鸣潮的日常、刷声骸、打虚域重复操作确实费时间。ok ww 是一个基于图像识别的开源自动化工具通过模拟键GUI 自动化计算机视觉RPA人工智能AI SDK 与 Valibot 集成指南valibotSchema 原理、结构化输出与版本演进全解析AI SDK 与 Valibot 集成指南valibotSchema 原理、结构化输出与版本演进全解析 导读 ai sdk/valibot 是 AI SD人工智能AI 应用AI Agent工具调用MCP ClientsStrands TypeScript SDK v1.0.0-rc.4 版本解读多智能体会话恢复、A2A 认证与消息级可观测性Strands TypeScript SDK v1.0.0 rc.4 版本解读多智能体会话恢复、A2A 认证与消息级可观测性 本文基于 Strands 开源人工智能大模型AI AgentAgent 框架多智能体工具调用MCP 服务创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考