beta.agents: AgentTurnResult.parse() swallows TypeError for invalid output_type and raises AgentOutputParseError
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
這個 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
- Obtain an
AgentTurnResultfrom a completed agent turn (e.g.stream.get_final_result()). - Call
result.parse(str),result.parse(dict), orresult.parse(object). - Observe that
AgentOutputParseError(__cause__=None) is raised instead ofTypeError.
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
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
openai/openai-python 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 76/100
openai/openai-python#4338 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 65/100
openai/openai-python#4337 ·
維護者通常 1 天內回覆
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters可能已有人在做 @Rewaskc02-lang 於 1 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
openai/openai-python#4311 · 4 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
openai/openai-python#4310 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
openai/openai-python#4309 · 1 則留言 ·
維護者通常 1 天內回覆
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
triage
難度 1/5 1-3 小時 新手友好度 88/100
kornia/kornia-rs#1416 · 1 則留言 ·
維護者通常 2 天內回覆
-
ui
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
PolicyEngine/ai-inequality#64 ·
維護者通常 1 天內回覆
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over time可能已有人在做 @PabloRoque 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 88/100
pymc-devs/pymc-extras#839 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆