`parse(to=...)` raises AttributeError on endpoints with a post-parser (embeddings, chat.completions.parse)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- backend-api-design
Research direction
Read LegacyAPIResponse.parse in _legacy_response.py and APIResponse.parse and AsyncAPIResponse.parse in _response.py; the issue identifies these as the places where the post-parser runs unconditionally. Check the existing response parsing tests for raw and streaming responses, then verify parse(to=dict) works on embeddings and chat completions with a post-parser while default parsing still works.
Written by the indexing model from the issue text.
Description
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)
- Dominant language
- Python
- Stars
- 32.2k
- Forks
- 5.8k
- Avg merge
- 12h 29m
- Merged PRs (30d)
- 112
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from openai/openai-python
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
openai/openai-python#4337 ·
Maintainers usually reply within 1 day
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersPossibly taken @Rewaskc02-lang claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openai/openai-python#4311 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openai/openai-python#4310 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
openai/openai-python#4309 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
openai/openai-python#4308 · 1 comment ·
Maintainers usually reply within 1 day
All issues in openai/openai-python
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 73/100
-
day_or=True union silently becomes the ISC cron AND bug when a day field expands to its full range (e.g. '0 0 */1 * */2' fires only Sun/Tue/Thu/Sat)Possibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pallets-eco/croniter#309 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
chore
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
nyjc-computing/campus-admin#105 ·
Maintainers usually reply within 1 day
-
bug enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
AOSSIE-Org/Gitcord-GithubDiscordBot#248 · 1 comment ·
Maintainers usually reply within 4 days