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

オープン 初心者向け
#4,338 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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 を使った embeddings と chat completions で parse(to=dict) が機能し、デフォルトの解析も引き続き機能することを検証します。

索引モデルが 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) 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)

主要言語
Python
スター
32.2k
フォーク
5.8k
平均マージ
12時間 29分
マージ済み PR(30日)
112

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openai/openai-python のほかの issue

openai/openai-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。