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

Open Beginner friendly
#4,338 0 comments 0 reactions 0 assignees View on GitHub

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

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) 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)

Dominant language
Python
Stars
32.2k
Forks
5.8k
Avg merge
12h 29m
Merged PRs (30d)
112

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openai/openai-python

All issues in openai/openai-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.