`parse(to=...)` raises AttributeError on endpoints with a post-parser (embeddings, chat.completions.parse)
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 76/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- backend-api-design
Direzione di ricerca
Leggi LegacyAPIResponse.parse in _legacy_response.py e APIResponse.parse e AsyncAPIResponse.parse in _response.py; l’issue identifica questi punti come i luoghi in cui il post-parser viene eseguito incondizionatamente. Controlla i test esistenti per l’analisi delle risposte raw e streaming, poi verifica che parse(to=dict) funzioni con embeddings e chat completions usando un post-parser, mentre l’analisi predefinita continua a funzionare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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)
- Lingua principale
- Python
- Stelle
- 32.2k
- Fork
- 5.8k
- Merge medio
- 12h 29m
- PR unite (30g)
- 112
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-python
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
openai/openai-python#4337 ·
I maintainer di solito rispondono entro 1 giorno
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersForse già presa @Rewaskc02-lang l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4311 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4310 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
openai/openai-python#4309 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
openai/openai-python#4308 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-python
Issue simili
-
ui
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
I maintainer di solito rispondono entro 1 giorno
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonForse già presa @ronaldtse l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
pydantic/pytest-examples#102 · 1 commento ·