Python: [Bug]: Content.from_dict leaves the nested content of image generation and MCP server tool results as plain dicts
维护者通常 1 天内回复
评估
调研方向
从 agent_framework/_types.py 中的 Content.from_dict 开始,这里会重建嵌套的 outputs 和 output 值。找到现有的 Content.from_dict 测试,并添加回归用例,覆盖单个图像生成 outputs 映射、MCP output 项以及普通 JSON 输出。当识别出的内容映射恢复为 Content,而普通 MCP 值保持不变时,即为完成。
由索引模型根据 Issue 内容生成。
描述
Observed Behavior
After a to_dict() / from_dict() round trip, two kinds of nested content come back as plain dicts instead of Content:
- the
outputsof animage_generation_tool_result. The OpenAI chat client sets it to a singleContent(the generated image as a data URI). - the items of the
outputlist of anmcp_server_tool_result. The OpenAI and Anthropic chat clients set it to a list ofContent.
This happens wherever messages are saved and loaded, for example when an AgentSession with its history is serialized with to_dict() and restored with AgentSession.from_dict(), or with Message.from_dict() directly. Code that expects Content then loses the data. For example, the Responses host reads the generated image with _image_generation_result(), which returns None for the restored result, so the image is dropped from the output item.
from_dict already rebuilds other nested content (function_call, screenshot, the inputs and outputs lists, items), but not a single outputs value or the output list.
Expected Behavior
The nested image and MCP output content comes back as Content, the same as before saving. MCP outputs that are plain JSON values stay unchanged.
Steps to Reproduce
- Build a message with an image generation result and an MCP server tool result, as the OpenAI chat client does.
- Save and restore it (here through an
AgentSession). - Check the type of the nested values.
Minimal Reproduction
from agent_framework import AgentSession, Content, Message
from agent_framework_hosting_responses._parsing import _image_generation_result
message = Message("assistant", [
Content.from_image_generation_tool_result(
image_id="ig_1", outputs=Content.from_uri("data:image/png;base64,iVBORw0KGgo=")
),
Content.from_mcp_server_tool_result("mcp_1", output=[Content.from_text("42")]),
])
session = AgentSession()
session.state["history"] = {"messages": [message]}
restored = AgentSession.from_dict(session.to_dict()).state["history"]["messages"][0]
print(type(restored.contents[0].outputs).__name__) # dict (expected Content)
print(type(restored.contents[1].output[0]).__name__) # dict (expected Content)
print(_image_generation_result(message.contents[0].outputs)) # iVBORw0KGgo=
print(_image_generation_result(restored.contents[0].outputs)) # None
The cause is in Content.from_dict in agent_framework/_types.py: it rebuilds outputs only when it is a list, and it does not look at output at all.
Error Messages and Stack Traces
No error. The restored values are plain dicts, so the image or tool output is silently lost where Content is expected.
Package Versions
agent-framework-core: 1.21.0 (main at a75e803ef), agent-framework-hosting-responses: 1.0.0a261008
Python Version
Python 3.12
Additional Context
I'd like to fix this. In Content.from_dict, also rebuild a single outputs mapping and the items of an output list, but only mappings whose type is a known content type, so plain MCP outputs stay as they are. Plus regression tests.
AI assistance: I used an AI coding assistant for the analysis and to draft this report. I reproduced and verified the behavior myself.
- 主要语言
- Python
- 星标
- 14k
- 派生
- 2.5k
- 平均合并
- 2 天 10 小时
- 30 天内合并 PR
- 424
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/agent-framework 的其他 Issue
-
Python: [Bug]: A non-positive MCP request timeout silently disables the timeout可能已有人在做 @jpalvarezl 于 3 天前认领。 未关闭python
难度 2/5 1-3 小时 新手友好度 82/100
microsoft/agent-framework#9313 · 已指派 1 人 ·
维护者通常 1 天内回复
-
.NET python triage
难度 1/5 1 小时以内 新手友好度 85/100
microsoft/agent-framework#9092 · 1 条评论 ·
维护者通常 1 天内回复
-
Python: raw-data content mappings lose annotations and attachment metadata可能已有人在做 @moonbox3 于 16 天前认领。 未关闭python triage
难度 2/5 1-3 小时 新手友好度 78/100
microsoft/agent-framework#8632 · 2 条评论 ·
维护者通常 1 天内回复
-
Python: Clarify when to use platform可能已有人在做 @eavanvalkenburg 于 16 天前认领。 未关闭python triage
难度 2/5 1-3 小时 新手友好度 68/100
microsoft/agent-framework#8599 · 1 条评论 ·
维护者通常 1 天内回复
-
.NET compaction documentation
难度 1/5 1 小时以内 新手友好度 82/100
microsoft/agent-framework#4629 · 1 条评论 ·
维护者通常 1 天内回复
查看 microsoft/agent-framework 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 72/100
aws-samples/sample-per-model-bedrock#261 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
pwncollege/dojo#1188 ·
维护者通常 1 天内回复
-
bug launcher packaging
难度 1/5 1 小时以内 新手友好度 88/100
tenstorrent/tt-studio#1426 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
python-attrs/cattrs#796 ·
维护者通常 1 天内回复