beta.agents: AgentTurnResult.parse() swallows TypeError for invalid output_type and raises AgentOutputParseError

未关闭 适合新手
#4,304 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
1/5
预计耗时
1 小时以内
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

这个 bug 位于 src/openai/lib/beta/agents/_result.py 中的 AgentTurnResult.parse() 里,其中 validate_output_type 在一个宽泛的 except Exception 内运行。先从阅读该方法以及 tests/lib/streaming/agents/test_output.py 中现有的检查(test_invalid_output_model_rejected_before_request)开始。将验证调用移到 try: 之上,并添加同步和异步回归测试,验证 parse(str)、parse(dict) 和 parse(object) 会抛出 TypeError 且不会出现 AgentOutputParseError。当这些测试通过且 agents 测试套件仍然通过时即完成。

由索引模型根据 Issue 内容生成。

描述

Confirm this is an issue with the Python library and not an underlying OpenAI API
  • This is an issue with the Python library
Describe the bug

In src/openai/lib/beta/agents/_result.py:32-51, AgentTurnResult.parse() invokes validate_output_type(output_type) inside the broad try: ... except Exception: block:

    def parse(self, output_type: type[ParseT]) -> AgentTurnResult[ParseT]:
        """Beta: parse a completed answer without changing its session configuration."""
        try:
            validate_output_type(output_type)
            parsed: ParseT | None = None
            ...
        except Exception:
            # Pydantic errors may include response text in their rendered message.
            raise AgentOutputParseError(self) from None

When a caller passes an unsupported output_type (such as str, dict, or object), validate_output_type(output_type) raises TypeError("Agents output_type must be a Pydantic model or a Pydantic v2 dataclass"), which is immediately caught, stripped of its cause (from None), and re-raised as AgentOutputParseError("Could not parse the completed agent output").

This disguises a caller-side programming/configuration error as a malformed model output error and is inconsistent with sessions.create(..., output_type=...) and sessions.stream(..., output_type=...), both of which propagate TypeError directly (covered by test_invalid_output_model_rejected_before_request in tests/lib/streaming/agents/test_output.py:456-477).

To Reproduce
  1. Obtain an AgentTurnResult from a completed agent turn (e.g. stream.get_final_result()).
  2. Call result.parse(str), result.parse(dict), or result.parse(object).
  3. Observe that AgentOutputParseError (__cause__=None) is raised instead of TypeError.
Code snippets
from openai.lib.beta.agents import AgentTurnResult
from openai.types.beta.agent_session_message import AgentSessionMessage
from openai.types.beta.agents.sessions.turn import Turn

turn = Turn.construct(
    id="turn_1", object="agent.session.turn", agent_id="a", session_id="s", created_at=1, status="completed"
)
msg = AgentSessionMessage.construct(
    id="msg_1",
    type="message",
    role="assistant",
    status="completed",
    turn_id="turn_1",
    content=[{"type": "output_text", "text": '{"ok": true}'}],
)
result = AgentTurnResult(turn=turn, messages=[msg])

for bad_type in (str, dict, object):
    try:
        result.parse(bad_type)
    except Exception as exc:
        print(f"result.parse({bad_type.__name__}) -> {type(exc).__name__}: {exc} (__cause__={exc.__cause__!r})")

Actual output:

result.parse(str) -> AgentOutputParseError: Could not parse the completed agent output (__cause__=None)
result.parse(dict) -> AgentOutputParseError: Could not parse the completed agent output (__cause__=None)
result.parse(object) -> AgentOutputParseError: Could not parse the completed agent output (__cause__=None)

Expected output:
TypeError: Agents output_type must be a Pydantic model or a Pydantic v2 dataclass

Suggested Fix

Move validate_output_type(output_type) immediately before try: in src/openai/lib/beta/agents/_result.py:

     def parse(self, output_type: type[ParseT]) -> AgentTurnResult[ParseT]:
         """Beta: parse a completed answer without changing its session configuration."""
+        validate_output_type(output_type)
         try:
-            validate_output_type(output_type)
             parsed: ParseT | None = None

(Verified locally with sync/async regression tests across str, dict, and object: 519 passed across tests/lib/streaming/agents/, tests/lib/beta/agents/test_tools.py, and tests/test_agents_budget_events.py, with ruff and pyright 0 errors.)

OS

macOS

Python version

Python 3.14.8

Library version

openai v3.57.0 (main@f63f14b)

主要语言
Python
星标
32.2k
派生
5.8k
平均合并
12 小时 29 分钟
30 天内合并 PR
112

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openai/openai-python 的其他 Issue

查看 openai/openai-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。