Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "3.22.0"
".": "3.22.1"
}
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## [3.22.1](https://github.com/openai/openai-python/compare/v3.22.0...v3.22.1) (2026-09-30)


### Bug Fixes

* **api:** correct the missing authentication error message ([#3993](https://github.com/openai/openai-python/issues/3993)) ([51692ed](https://github.com/openai/openai-python/commit/51692ed3f9d2b87084801295f824c2202dac3785)), closes [#3962](https://github.com/openai/openai-python/issues/3962)
* transform NotRequired typed dictionary fields ([#3995](https://github.com/openai/openai-python/issues/3995)) ([5544c18](https://github.com/openai/openai-python/commit/5544c18c19f125718ed9c4f7c42866c27060158e))


### Chores

* **tests:** mark sample_file.txt as text with LF line endings ([#3879](https://github.com/openai/openai-python/issues/3879)) ([fd78a48](https://github.com/openai/openai-python/commit/fd78a48d2be489fde978696e1ad32032c20f0fab))


### Documentation

* **examples:** fix two stale example comments ([#3877](https://github.com/openai/openai-python/issues/3877)) ([1c4830b](https://github.com/openai/openai-python/commit/1c4830b6e735d2ca3c4bd95f66467fa007f8b451))
* **examples:** print event.delta in the streaming helpers doc ([#3876](https://github.com/openai/openai-python/issues/3876)) ([063375b](https://github.com/openai/openai-python/commit/063375b7a12f3f3a0de10aec829c0416651be8cc))

## [3.22.0](https://github.com/openai/openai-python/compare/v3.21.0...v3.22.0) (2026-09-29)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openai"
version = "3.22.0"
version = "3.22.1"
description = "The official Python library for the openai API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
5 changes: 3 additions & 2 deletions src/openai/_utils/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
is_required_type,
is_sequence_type,
is_annotated_type,
is_not_required_type,
strip_annotated_type,
)

Expand Down Expand Up @@ -117,8 +118,8 @@ def _get_annotated_type(type_: type) -> type | None:

This also unwraps the type when applicable, e.g. `Required[Annotated[T, ...]]`
"""
if is_required_type(type_):
# Unwrap `Required[Annotated[T, ...]]` to `Annotated[T, ...]`
if is_required_type(type_) or is_not_required_type(type_):
# Unwrap Required or NotRequired to expose the Annotated metadata
type_ = get_args(type_)[0]

if is_annotated_type(type_):
Expand Down
9 changes: 7 additions & 2 deletions src/openai/_utils/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TypeIs,
Required,
Annotated,
NotRequired,
get_args,
get_origin,
)
Expand Down Expand Up @@ -45,6 +46,10 @@ def is_required_type(typ: type) -> bool:
return get_origin(typ) == Required


def is_not_required_type(typ: type) -> bool:
return get_origin(typ) == NotRequired


def is_typevar(typ: type) -> bool:
# type ignore is required because type checkers
# think this expression will always return False
Expand All @@ -71,10 +76,10 @@ def is_type_alias_type(tp: Any, /) -> TypeIs[typing_extensions.TypeAliasType]:
return isinstance(tp, _TYPE_ALIAS_TYPES)


# Extracts T from Annotated[T, ...] or from Required[Annotated[T, ...]]
# Extracts T from Annotated[T, ...], including Required and NotRequired wrappers
@lru_cache(maxsize=8096)
def strip_annotated_type(typ: type) -> type:
if is_required_type(typ) or is_annotated_type(typ):
if is_required_type(typ) or is_not_required_type(typ) or is_annotated_type(typ):
return strip_annotated_type(cast(type, get_args(typ)[0]))

return typ
Expand Down
2 changes: 1 addition & 1 deletion src/openai/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = "openai"
__version__ = "3.22.0" # x-release-please-version
__version__ = "3.22.1" # x-release-please-version
8 changes: 4 additions & 4 deletions src/openai/lib/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def wait_for_file_processing(
"""Poll a file using the caller's resource and sleep hooks."""
TERMINAL_STATES = {"processed", "error", "deleted"}

start = time.time()
start = time.monotonic()
file = files.retrieve(id)
while file.status not in TERMINAL_STATES:
files._sleep(poll_interval)

file = files.retrieve(id)
if time.time() - start > max_wait_seconds:
if time.monotonic() - start > max_wait_seconds:
raise RuntimeError(
f"Giving up on waiting for file {id} to finish processing after {max_wait_seconds} seconds."
)
Expand All @@ -43,13 +43,13 @@ async def async_wait_for_file_processing(
"""Poll a file using the caller's async resource and sleep hooks."""
TERMINAL_STATES = {"processed", "error", "deleted"}

start = time.time()
start = time.monotonic()
file = await files.retrieve(id)
while file.status not in TERMINAL_STATES:
await files._sleep(poll_interval)

file = await files.retrieve(id)
if time.time() - start > max_wait_seconds:
if time.monotonic() - start > max_wait_seconds:
raise RuntimeError(
f"Giving up on waiting for file {id} to finish processing after {max_wait_seconds} seconds."
)
Expand Down
18 changes: 14 additions & 4 deletions src/openai/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,18 @@ def _configure_realtime(self, model: str, extra_query: Query) -> tuple[httpx2.UR

if self.websocket_base_url is not None:
base_url = normalize_httpx_url(self.websocket_base_url)
merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
path, separator, url_query = base_url.raw_path.partition(b"?")
merge_raw_path = path.rstrip(b"/") + b"/realtime" + separator + url_query
realtime_url = base_url.copy_with(raw_path=merge_raw_path)
else:
base_url = self._prepare_url("/realtime")
realtime_url = base_url.copy_with(scheme="wss")

url = realtime_url.copy_with(params={**query})
url = realtime_url.copy_with(
params=httpx2.QueryParams(self.qs.stringify(cast(Any, self.default_query)))
.merge(realtime_url.params)
.merge(cast(Any, query))
)
return url, auth_headers


Expand Down Expand Up @@ -883,11 +888,16 @@ async def _configure_realtime(self, model: str, extra_query: Query) -> tuple[htt

if self.websocket_base_url is not None:
base_url = normalize_httpx_url(self.websocket_base_url)
merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
path, separator, url_query = base_url.raw_path.partition(b"?")
merge_raw_path = path.rstrip(b"/") + b"/realtime" + separator + url_query
realtime_url = base_url.copy_with(raw_path=merge_raw_path)
else:
base_url = self._prepare_url("/realtime")
realtime_url = base_url.copy_with(scheme="wss")

url = realtime_url.copy_with(params={**query})
url = realtime_url.copy_with(
params=httpx2.QueryParams(self.qs.stringify(cast(Any, self.default_query)))
.merge(realtime_url.params)
.merge(cast(Any, query))
)
return url, auth_headers
44 changes: 30 additions & 14 deletions src/openai/resources/beta/realtime/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,19 @@ async def __aenter__(self) -> AsyncRealtimeConnection:

url, auth_headers = await self.__client._configure_realtime(self.__model, extra_query)
else:
url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
"model": self.__model,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(
cast(
Any,
{
"model": self.__model,
**extra_query,
},
)
),
)
log.debug("Connecting to WebSocket API")
if self.__websocket_connection_options:
Expand Down Expand Up @@ -400,7 +407,8 @@ def _prepare_url(self) -> httpx2.URL:
else:
base_url = self.__client._base_url.copy_with(scheme="wss")

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
path, separator, query = base_url.raw_path.partition(b"?")
merge_raw_path = path.rstrip(b"/") + b"/realtime" + separator + query
return base_url.copy_with(raw_path=merge_raw_path)

async def __aexit__(
Expand Down Expand Up @@ -547,12 +555,19 @@ def __enter__(self) -> RealtimeConnection:
if is_azure_client(self.__client):
url, auth_headers = self.__client._configure_realtime(self.__model, extra_query)
else:
url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
"model": self.__model,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(
cast(
Any,
{
"model": self.__model,
**extra_query,
},
)
),
)
log.debug("Connecting to WebSocket API")
if self.__websocket_connection_options:
Expand Down Expand Up @@ -583,7 +598,8 @@ def _prepare_url(self) -> httpx2.URL:
else:
base_url = self.__client._base_url.copy_with(scheme="wss")

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
path, separator, query = base_url.raw_path.partition(b"?")
merge_raw_path = path.rstrip(b"/") + b"/realtime" + separator + query
return base_url.copy_with(raw_path=merge_raw_path)

def __exit__(
Expand Down
30 changes: 16 additions & 14 deletions src/openai/resources/beta/responses/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4267,7 +4267,7 @@ def __init__(
self._extra_headers = extra_headers
self._intentionally_closed = False
self._is_reconnecting = False
self._send_queue = send_queue or SendQueue()
self._send_queue = send_queue if send_queue is not None else SendQueue()
self._event_handler_registry = EventHandlerRegistry(use_lock=False)

self.response = AsyncResponsesResponseResource(self)
Expand Down Expand Up @@ -4674,11 +4674,11 @@ async def _connect_ws(self, extra_query: Query, extra_headers: Headers) -> Async
except ImportError as exc:
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc

url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(cast(Any, extra_query)),
)
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
options = await self.__client._prepare_options(
Expand Down Expand Up @@ -4722,7 +4722,8 @@ def _prepare_url(self) -> httpx2.URL:
ws_scheme = "ws" if scheme == "http" else "wss"
base_url = self.__client._base_url.copy_with(scheme=ws_scheme)

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/responses"
path, separator, query = base_url.raw_path.partition(b"?")
merge_raw_path = path.rstrip(b"/") + b"/responses" + separator + query
return base_url.copy_with(raw_path=merge_raw_path)

async def __aexit__(
Expand Down Expand Up @@ -4763,7 +4764,7 @@ def __init__(
self._extra_headers = extra_headers
self._intentionally_closed = False
self._is_reconnecting = False
self._send_queue = send_queue or SendQueue()
self._send_queue = send_queue if send_queue is not None else SendQueue()
self._event_handler_registry = EventHandlerRegistry(use_lock=True)

self.response = ResponsesResponseResource(self)
Expand Down Expand Up @@ -5160,11 +5161,11 @@ def _connect_ws(self, extra_query: Query, extra_headers: Headers) -> WebSocketCo
except ImportError as exc:
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc

url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(cast(Any, extra_query)),
)
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
options = self.__client._prepare_options(
Expand Down Expand Up @@ -5208,7 +5209,8 @@ def _prepare_url(self) -> httpx2.URL:
ws_scheme = "ws" if scheme == "http" else "wss"
base_url = self.__client._base_url.copy_with(scheme=ws_scheme)

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/responses"
path, separator, query = base_url.raw_path.partition(b"?")
merge_raw_path = path.rstrip(b"/") + b"/responses" + separator + query
return base_url.copy_with(raw_path=merge_raw_path)

def __exit__(
Expand Down
20 changes: 10 additions & 10 deletions src/openai/resources/live/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,11 @@ async def _connect_ws(self, extra_query: Query, extra_headers: Headers) -> Async
except ImportError as exc:
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc

url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(cast(Any, extra_query)),
)
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
options = await self.__client._prepare_options(
Expand Down Expand Up @@ -1025,11 +1025,11 @@ def _connect_ws(self, extra_query: Query, extra_headers: Headers) -> WebSocketCo
except ImportError as exc:
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc

url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(cast(Any, extra_query)),
)
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
options = self.__client._prepare_options(
Expand Down
20 changes: 10 additions & 10 deletions src/openai/resources/live/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,11 @@ async def _connect_ws(self, extra_query: Query, extra_headers: Headers) -> Async
except ImportError as exc:
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc

url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(cast(Any, extra_query)),
)
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
options = await self.__client._prepare_options(
Expand Down Expand Up @@ -1240,11 +1240,11 @@ def _connect_ws(self, extra_query: Query, extra_headers: Headers) -> WebSocketCo
except ImportError as exc:
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc

url = self._prepare_url().copy_with(
params={
**self.__client.base_url.params,
**extra_query,
},
url = self._prepare_url()
url = url.copy_with(
params=httpx2.QueryParams(self.__client.qs.stringify(cast(Any, self.__client.default_query)))
.merge(url.params)
.merge(cast(Any, extra_query)),
)
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
options = self.__client._prepare_options(
Expand Down
Loading
Loading