`parse()` / `pydantic_function_tool()` send `name: "Page[Item]"` for a parameterized generic pydantic model, outside the documented name pattern
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start by reading the three affected entry points: lib/_parsing/_completions.py, type_to_text_format_param in lib/_parsing/_responses.py, and lib/_tools.py. Then find their existing tests and check how derived and user-supplied names are handled. Done means derived generic-model names match the documented character and length constraints across these helpers, with regression tests; user-supplied names remain unchanged.
索引モデルが 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
For a parameterized generic pydantic model such as Page[Item], the structured-output and tool helpers take the name straight from __name__. pydantic v2 sets that to "Page[Item]".
The API params document the name field as "Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64". That constraint is in types/shared_params/response_format_json_schema.py and types/shared_params/function_definition.py. So the SDK builds a request with a name outside that pattern, and the user never chose the name.
The affected sites all use __name__ verbatim, with no sanitizing and no length check:
lib/_parsing/_completions.py:283, used bychat.completions.parse/streamtype_to_text_format_paraminlib/_parsing/_responses.py, used byresponses.parse/streamlib/_tools.py:53, used bypydantic_function_tool
Nested generics can also go past the 64-character limit.
To Reproduce
- Define a generic model
Page[T]and usePage[Item]asresponse_format(ortext_format, or pass it topydantic_function_tool). - Inspect the outgoing request body. The script below uses a mocked transport and makes no real API call.
Code snippets
import json, re
from typing import Generic, List, TypeVar
import httpx2 as httpx # this SDK version's transport
from pydantic import BaseModel
import openai
from openai import OpenAI
from openai.lib._parsing._responses import type_to_text_format_param
T = TypeVar("T")
class Item(BaseModel):
title: str
class Page(BaseModel, Generic[T]):
items: List[T]
captured = {}
def handler(request):
captured["body"] = json.loads(request.content)
return httpx.Response(400, json={"error": {"message": "mock"}})
client = OpenAI(api_key="sk-test", max_retries=0,
http_client=httpx.Client(transport=httpx.MockTransport(handler)))
try:
client.chat.completions.parse(model="gpt-4o", messages=[{"role": "user", "content": "hi"}],
response_format=Page[Item])
except Exception:
pass
name = captured["body"]["response_format"]["json_schema"]["name"]
print(repr(name), bool(re.fullmatch(r"[a-zA-Z0-9_-]{1,64}", name))) # 'Page[Item]' False
print(type_to_text_format_param(Page[Item])["name"]) # Page[Item]
print(openai.pydantic_function_tool(Page[Item])["function"]["name"]) # Page[Item]
Expected: a name that fits the documented pattern, e.g. Page_Item_. Generic wrappers like Result[T] / Page[T] are a common pydantic pattern, and today the workaround is to subclass (class ItemPage(Page[Item]): ...) or pass name= by hand where that is possible.
I haven't sent this to the live API, so I'm relying on the documented constraint rather than an observed 400.
Suggested fix: normalize the derived name in one helper and use it at all three sites. A user-supplied name= would be left as is.
def _to_api_name(n: str) -> str:
return re.sub(r"[^a-zA-Z0-9_-]", "_", n)[:64]
Searched first: issues and PRs for generic / TypeVar / brackets / name pattern with parse and pydantic_function_tool. Nothing matched; #2004, #2024, #1950 and #1733 are different structured-output issues. CHANGELOG has no related fix.
Disclosure: found while auditing with AI tooling; reproduced locally with the script above.
OS
Linux (WSL2)
Python version
Python 3.12.3
Library version
openai 3.24.0 (main @ becc1d2), pydantic 2.13.5
- 主要言語
- Python
- スター
- 32.2k
- フォーク
- 5.8k
- 平均マージ
- 12時間 29分
- マージ済み PR(30日)
- 112
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
openai/openai-python のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
openai/openai-python#4338 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
openai/openai-python#4337 ·
メンテナーはふだん 1 日以内に返信
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters対応中かも @Rewaskc02-lang が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
openai/openai-python#4311 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
openai/openai-python#4310 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
openai/openai-python#4309 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
openai/openai-python の issue をすべて見る
似ている issue
-
triage
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
kornia/kornia-rs#1416 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
ui
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
PolicyEngine/ai-inequality#64 ·
メンテナーはふだん 1 日以内に返信
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over time対応中かも @PabloRoque が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
pymc-devs/pymc-extras#839 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信