`parse(to=...)` raises AttributeError on endpoints with a post-parser (embeddings, chat.completions.parse)
維護者通常 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)raisesAttributeError: 'dict' object has no attribute 'data'(same throughwith_streaming_response).client.chat.completions.with_raw_response.parse(..., response_format=Model).parse(to=dict)raisesAttributeError: '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
- Run the snippet below. It answers the request from a mock transport, so it needs no key and no network.
- The first
parse()prints[0.5, -1.0]. 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
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
openai/openai-python 的其他 Issue
-
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 天內回覆
-
難度 1/5 1 小時以內 新手友好度 82/100
openai/openai-python#4308 · 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 天內回覆