`parse(to=...)` raises AttributeError on endpoints with a post-parser (embeddings, chat.completions.parse)

未關閉 適合新手
#4,338 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
76/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python

研究方向

閱讀 _legacy_response.py 中的 LegacyAPIResponse.parse,以及 _response.py 中的 APIResponse.parse 和 AsyncAPIResponse.parse;issue 指出這些位置會無條件執行 post-parser。檢查現有針對 raw 和 streaming response 的解析測試,接著確認帶有 post-parser 時 parse(to=dict) 在 embeddings 和 chat completions 上能正常運作,且預設解析仍然正常。

由索引模型根據 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

parse(to=...) on a raw or streaming response raises AttributeError when the endpoint has a post-parser. The post-parser still runs on the value built for to, and it expects the endpoint's default model.

  • client.embeddings.with_raw_response.create(...).parse(to=dict) raises AttributeError: 'dict' object has no attribute 'data' (same through with_streaming_response).
  • client.chat.completions.with_raw_response.parse(..., response_format=Model).parse(to=dict) raises AttributeError: 'dict' object has no attribute 'choices'.

On endpoints without a post-parser, such as chat.completions.create, parse(to=dict) returns the dict.

The post-parser is applied unconditionally in _legacy_response.py (LegacyAPIResponse.parse) and in _response.py (APIResponse.parse and AsyncAPIResponse.parse). Running it only when to is not given makes all of the calls above return the requested type.

To Reproduce
  1. Run the snippet below. It answers the request from a mock transport, so it needs no key and no network.
  2. The first parse() prints [0.5, -1.0].
  3. parse(to=dict) raises:
  File ".../openai/_legacy_response.py", line 140, in parse
    parsed = self._options.post_parser(parsed)
  File ".../openai/lib/_parsing/_embeddings.py", line 22, in parse_embedding_response
    if not obj.data:
AttributeError: 'dict' object has no attribute 'data'
Code snippets
import array
import base64

import httpx2
from openai import OpenAI

EMBED = {
    "object": "list",
    "model": "text-embedding-3-small",
    "data": [{"object": "embedding", "index": 0, "embedding": base64.b64encode(array.array("f", [0.5, -1.0]).tobytes()).decode()}],
    "usage": {"prompt_tokens": 1, "total_tokens": 1},
}
client = OpenAI(
    api_key="test",
    base_url="http://localhost/v1",
    http_client=httpx2.Client(transport=httpx2.MockTransport(lambda request: httpx2.Response(200, json=EMBED))),
)

raw = client.embeddings.with_raw_response.create(model="text-embedding-3-small", input="hi")
print(raw.parse().data[0].embedding)

raw = client.embeddings.with_raw_response.create(model="text-embedding-3-small", input="hi")
print(raw.parse(to=dict))
OS

Windows 11

Python version

Python v3.14.7

Library version

openai v3.60.0 (main at f2b2399)

主要語言
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 摘要。