`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
-
ui
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
维护者通常 1 天内回复
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to python可能已有人在做 @ronaldtse 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 76/100
pydantic/pytest-examples#102 · 1 条评论 ·