Skip to content

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

Backlog

Description

@Yasser-Ameur

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)

Activity

  1. Pablo-aps commented on Nov 27, 2026

    @Pablo-aps

    For embeddings, this workaround avoids the post-parser and returns a plain dict with float vectors:

    raw = client.embeddings.with_raw_response.create(
        model="text-embedding-3-small",
        input="hi",
        encoding_format="float",
    )
    data = raw.http_response.json()

    For with_streaming_response, call response.json() inside the context manager, or await response.json() with the async client. The raw-response object above is a LegacyAPIResponse, so it needs .http_response.json() instead.

    The explicit encoding_format="float" matters: without it, the SDK requests base64 and normally decodes it in the post-parser. Reading the HTTP JSON directly skips that decoding.

    I tested this on openai 3.60.0 / Python 3.13.7 with httpx2.MockTransport, using the report's [0.5, -1.0] vector. The mock returned base64 or floats according to the outgoing request's encoding_format. All four combinations (sync/async, raw/streaming response) returned the float list with this workaround. No live API request was involved.

    One regression case for the fix: raw.parse(to=CreateEmbeddingResponse) currently decodes the base64 vector correctly, just like raw.parse(). Skipping the post-parser whenever to is supplied would change that behavior too. I'd include the explicit original response type alongside to=dict in the tests. Calling parse() first and then parse(to=dict) on the same raw response still raises today, so that order is worth covering as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions