Skip to content

Commit 2a65d4d

Browse files
feat(api): new streaming helpers for background responses
1 parent 5d1d5b4 commit 2a65d4d

21 files changed

+76
-153
lines changed

.stats.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 111
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai%2Fopenai-6af14840a810139bf407013167ce1c8fb21b6ef8eb0cc3db58b51af7d52c4b5a.yml
3-
openapi_spec_hash: 3241bde6b273cfec0035e522bd07985d
4-
config_hash: 7367b68a4e7db36885c1a886f57b17f6
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai%2Fopenai-fc64d7c2c8f51f750813375356c3f3fdfc7fc1b1b34f19c20a5410279d445d37.yml
3+
openapi_spec_hash: 618285fc70199ee32b9ebe4bf72f7e4c
4+
config_hash: c497f6b750cc89c0bf2eefc0bc839c70

api.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ from openai.types.responses import (
764764
ResponseRefusalDoneEvent,
765765
ResponseStatus,
766766
ResponseStreamEvent,
767-
ResponseTextAnnotationDeltaEvent,
768767
ResponseTextConfig,
769768
ResponseTextDeltaEvent,
770769
ResponseTextDoneEvent,

examples/responses/background_streaming.py

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env -S rye run python
12
from typing import List
23

34
import rich

src/openai/lib/streaming/responses/_events.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
ResponseMcpCallInProgressEvent,
3232
ResponseMcpListToolsFailedEvent,
3333
ResponseAudioTranscriptDoneEvent,
34-
ResponseTextAnnotationDeltaEvent,
3534
ResponseAudioTranscriptDeltaEvent,
3635
ResponseMcpCallArgumentsDoneEvent,
3736
ResponseReasoningSummaryDoneEvent,
@@ -118,7 +117,6 @@ class ResponseCompletedEvent(RawResponseCompletedEvent, GenericModel, Generic[Te
118117
ResponseOutputItemDoneEvent,
119118
ResponseRefusalDeltaEvent,
120119
ResponseRefusalDoneEvent,
121-
ResponseTextAnnotationDeltaEvent,
122120
ResponseTextDoneEvent,
123121
ResponseWebSearchCallCompletedEvent,
124122
ResponseWebSearchCallInProgressEvent,

src/openai/resources/beta/realtime/realtime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ def clear(self, *, event_id: str | NotGiven = NOT_GIVEN) -> None:
820820
stop generating audio and emit a `output_audio_buffer.cleared` event. This
821821
event should be preceded by a `response.cancel` client event to stop the
822822
generation of the current response.
823-
[Learn more](https://platform.openai.com/docs/guides/realtime-model-capabilities#client-and-server-events-for-audio-in-webrtc).
823+
[Learn more](https://platform.openai.com/docs/guides/realtime-conversations#client-and-server-events-for-audio-in-webrtc).
824824
"""
825825
self._connection.send(
826826
cast(RealtimeClientEventParam, strip_not_given({"type": "output_audio_buffer.clear", "event_id": event_id}))
@@ -1072,7 +1072,7 @@ async def clear(self, *, event_id: str | NotGiven = NOT_GIVEN) -> None:
10721072
stop generating audio and emit a `output_audio_buffer.cleared` event. This
10731073
event should be preceded by a `response.cancel` client event to stop the
10741074
generation of the current response.
1075-
[Learn more](https://platform.openai.com/docs/guides/realtime-model-capabilities#client-and-server-events-for-audio-in-webrtc).
1075+
[Learn more](https://platform.openai.com/docs/guides/realtime-conversations#client-and-server-events-for-audio-in-webrtc).
10761076
"""
10771077
await self._connection.send(
10781078
cast(RealtimeClientEventParam, strip_not_given({"type": "output_audio_buffer.clear", "event_id": event_id}))

src/openai/resources/chat/completions/completions.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ def create(
323323
324324
We generally recommend altering this or `temperature` but not both.
325325
326-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
327-
and detect abuse.
326+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
327+
bucketing similar requests and to help OpenAI detect and prevent abuse.
328328
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
329329
330330
web_search_options: This tool searches the web for relevant results to use in a response. Learn more
@@ -592,8 +592,8 @@ def create(
592592
593593
We generally recommend altering this or `temperature` but not both.
594594
595-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
596-
and detect abuse.
595+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
596+
bucketing similar requests and to help OpenAI detect and prevent abuse.
597597
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
598598
599599
web_search_options: This tool searches the web for relevant results to use in a response. Learn more
@@ -861,8 +861,8 @@ def create(
861861
862862
We generally recommend altering this or `temperature` but not both.
863863
864-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
865-
and detect abuse.
864+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
865+
bucketing similar requests and to help OpenAI detect and prevent abuse.
866866
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
867867
868868
web_search_options: This tool searches the web for relevant results to use in a response. Learn more
@@ -1426,8 +1426,8 @@ async def create(
14261426
14271427
We generally recommend altering this or `temperature` but not both.
14281428
1429-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
1430-
and detect abuse.
1429+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
1430+
bucketing similar requests and to help OpenAI detect and prevent abuse.
14311431
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
14321432
14331433
web_search_options: This tool searches the web for relevant results to use in a response. Learn more
@@ -1695,8 +1695,8 @@ async def create(
16951695
16961696
We generally recommend altering this or `temperature` but not both.
16971697
1698-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
1699-
and detect abuse.
1698+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
1699+
bucketing similar requests and to help OpenAI detect and prevent abuse.
17001700
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
17011701
17021702
web_search_options: This tool searches the web for relevant results to use in a response. Learn more
@@ -1964,8 +1964,8 @@ async def create(
19641964
19651965
We generally recommend altering this or `temperature` but not both.
19661966
1967-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
1968-
and detect abuse.
1967+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
1968+
bucketing similar requests and to help OpenAI detect and prevent abuse.
19691969
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
19701970
19711971
web_search_options: This tool searches the web for relevant results to use in a response. Learn more

src/openai/resources/containers/files/files.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from typing import Mapping, cast
56
from typing_extensions import Literal
67

78
import httpx
@@ -16,7 +17,7 @@
1617
AsyncContentWithStreamingResponse,
1718
)
1819
from ...._types import NOT_GIVEN, Body, Query, Headers, NoneType, NotGiven, FileTypes
19-
from ...._utils import maybe_transform, async_maybe_transform
20+
from ...._utils import extract_files, maybe_transform, deepcopy_minimal, async_maybe_transform
2021
from ...._compat import cached_property
2122
from ...._resource import SyncAPIResource, AsyncAPIResource
2223
from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
@@ -88,15 +89,21 @@ def create(
8889
"""
8990
if not container_id:
9091
raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
92+
body = deepcopy_minimal(
93+
{
94+
"file": file,
95+
"file_id": file_id,
96+
}
97+
)
98+
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
99+
# It should be noted that the actual Content-Type header that will be
100+
# sent to the server will contain a `boundary` parameter, e.g.
101+
# multipart/form-data; boundary=---abc--
102+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
91103
return self._post(
92104
f"/containers/{container_id}/files",
93-
body=maybe_transform(
94-
{
95-
"file": file,
96-
"file_id": file_id,
97-
},
98-
file_create_params.FileCreateParams,
99-
),
105+
body=maybe_transform(body, file_create_params.FileCreateParams),
106+
files=files,
100107
options=make_request_options(
101108
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
102109
),
@@ -295,15 +302,21 @@ async def create(
295302
"""
296303
if not container_id:
297304
raise ValueError(f"Expected a non-empty value for `container_id` but received {container_id!r}")
305+
body = deepcopy_minimal(
306+
{
307+
"file": file,
308+
"file_id": file_id,
309+
}
310+
)
311+
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
312+
# It should be noted that the actual Content-Type header that will be
313+
# sent to the server will contain a `boundary` parameter, e.g.
314+
# multipart/form-data; boundary=---abc--
315+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
298316
return await self._post(
299317
f"/containers/{container_id}/files",
300-
body=await async_maybe_transform(
301-
{
302-
"file": file,
303-
"file_id": file_id,
304-
},
305-
file_create_params.FileCreateParams,
306-
),
318+
body=await async_maybe_transform(body, file_create_params.FileCreateParams),
319+
files=files,
307320
options=make_request_options(
308321
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
309322
),

src/openai/resources/responses/input_items.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def list(
7272
limit: A limit on the number of objects to be returned. Limit can range between 1 and
7373
100, and the default is 20.
7474
75-
order: The order to return the input items in. Default is `asc`.
75+
order: The order to return the input items in. Default is `desc`.
7676
7777
- `asc`: Return the input items in ascending order.
7878
- `desc`: Return the input items in descending order.
@@ -160,7 +160,7 @@ def list(
160160
limit: A limit on the number of objects to be returned. Limit can range between 1 and
161161
100, and the default is 20.
162162
163-
order: The order to return the input items in. Default is `asc`.
163+
order: The order to return the input items in. Default is `desc`.
164164
165165
- `asc`: Return the input items in ascending order.
166166
- `desc`: Return the input items in descending order.

src/openai/resources/responses/responses.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ def create(
250250
- `disabled` (default): If a model response will exceed the context window size
251251
for a model, the request will fail with a 400 error.
252252
253-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
254-
and detect abuse.
253+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
254+
bucketing similar requests and to help OpenAI detect and prevent abuse.
255255
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
256256
257257
extra_headers: Send extra headers
@@ -443,8 +443,8 @@ def create(
443443
- `disabled` (default): If a model response will exceed the context window size
444444
for a model, the request will fail with a 400 error.
445445
446-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
447-
and detect abuse.
446+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
447+
bucketing similar requests and to help OpenAI detect and prevent abuse.
448448
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
449449
450450
extra_headers: Send extra headers
@@ -636,8 +636,8 @@ def create(
636636
- `disabled` (default): If a model response will exceed the context window size
637637
for a model, the request will fail with a 400 error.
638638
639-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
640-
and detect abuse.
639+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
640+
bucketing similar requests and to help OpenAI detect and prevent abuse.
641641
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
642642
643643
extra_headers: Send extra headers
@@ -1353,8 +1353,8 @@ async def create(
13531353
- `disabled` (default): If a model response will exceed the context window size
13541354
for a model, the request will fail with a 400 error.
13551355
1356-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
1357-
and detect abuse.
1356+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
1357+
bucketing similar requests and to help OpenAI detect and prevent abuse.
13581358
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
13591359
13601360
extra_headers: Send extra headers
@@ -1546,8 +1546,8 @@ async def create(
15461546
- `disabled` (default): If a model response will exceed the context window size
15471547
for a model, the request will fail with a 400 error.
15481548
1549-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
1550-
and detect abuse.
1549+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
1550+
bucketing similar requests and to help OpenAI detect and prevent abuse.
15511551
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
15521552
15531553
extra_headers: Send extra headers
@@ -1739,8 +1739,8 @@ async def create(
17391739
- `disabled` (default): If a model response will exceed the context window size
17401740
for a model, the request will fail with a 400 error.
17411741
1742-
user: A unique identifier representing your end-user, which can help OpenAI to monitor
1743-
and detect abuse.
1742+
user: A stable identifier for your end-users. Used to boost cache hit rates by better
1743+
bucketing similar requests and to help OpenAI detect and prevent abuse.
17441744
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
17451745
17461746
extra_headers: Send extra headers

src/openai/types/chat/completion_create_params.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ class CompletionCreateParamsBase(TypedDict, total=False):
292292
"""
293293

294294
user: str
295-
"""
296-
A unique identifier representing your end-user, which can help OpenAI to monitor
297-
and detect abuse.
295+
"""A stable identifier for your end-users.
296+
297+
Used to boost cache hit rates by better bucketing similar requests and to help
298+
OpenAI detect and prevent abuse.
298299
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
299300
"""
300301

src/openai/types/responses/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
from .response_audio_transcript_done_event import ResponseAudioTranscriptDoneEvent as ResponseAudioTranscriptDoneEvent
9898
from .response_file_search_tool_call_param import ResponseFileSearchToolCallParam as ResponseFileSearchToolCallParam
9999
from .response_mcp_list_tools_failed_event import ResponseMcpListToolsFailedEvent as ResponseMcpListToolsFailedEvent
100-
from .response_text_annotation_delta_event import ResponseTextAnnotationDeltaEvent as ResponseTextAnnotationDeltaEvent
101100
from .response_audio_transcript_delta_event import (
102101
ResponseAudioTranscriptDeltaEvent as ResponseAudioTranscriptDeltaEvent,
103102
)

src/openai/types/responses/input_item_list_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class InputItemListParams(TypedDict, total=False):
3030
"""
3131

3232
order: Literal["asc", "desc"]
33-
"""The order to return the input items in. Default is `asc`.
33+
"""The order to return the input items in. Default is `desc`.
3434
3535
- `asc`: Return the input items in ascending order.
3636
- `desc`: Return the input items in descending order.

src/openai/types/responses/response.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,10 @@ class Response(BaseModel):
209209
"""
210210

211211
user: Optional[str] = None
212-
"""
213-
A unique identifier representing your end-user, which can help OpenAI to monitor
214-
and detect abuse.
212+
"""A stable identifier for your end-users.
213+
214+
Used to boost cache hit rates by better bucketing similar requests and to help
215+
OpenAI detect and prevent abuse.
215216
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
216217
"""
217218

src/openai/types/responses/response_create_params.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ class ResponseCreateParamsBase(TypedDict, total=False):
199199
"""
200200

201201
user: str
202-
"""
203-
A unique identifier representing your end-user, which can help OpenAI to monitor
204-
and detect abuse.
202+
"""A stable identifier for your end-users.
203+
204+
Used to boost cache hit rates by better bucketing similar requests and to help
205+
OpenAI detect and prevent abuse.
205206
[Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
206207
"""
207208

src/openai/types/responses/response_stream_event.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from .response_mcp_call_in_progress_event import ResponseMcpCallInProgressEvent
2929
from .response_audio_transcript_done_event import ResponseAudioTranscriptDoneEvent
3030
from .response_mcp_list_tools_failed_event import ResponseMcpListToolsFailedEvent
31-
from .response_text_annotation_delta_event import ResponseTextAnnotationDeltaEvent
3231
from .response_audio_transcript_delta_event import ResponseAudioTranscriptDeltaEvent
3332
from .response_reasoning_summary_done_event import ResponseReasoningSummaryDoneEvent
3433
from .response_mcp_call_arguments_done_event import ResponseMcpCallArgumentsDoneEvent
@@ -93,7 +92,6 @@
9392
ResponseReasoningSummaryTextDoneEvent,
9493
ResponseRefusalDeltaEvent,
9594
ResponseRefusalDoneEvent,
96-
ResponseTextAnnotationDeltaEvent,
9795
ResponseTextDeltaEvent,
9896
ResponseTextDoneEvent,
9997
ResponseWebSearchCallCompletedEvent,

0 commit comments

Comments
 (0)