Skip to content

Commit 7ae334d

Browse files
stainless-botvvanglro
authored andcommitted
release: 1.14.2 (openai#1244)
* perf: cache TypeAdapters (openai#1114) * perf: cache TypeAdapters (openai#1243) * docs: fix typo in CONTRIBUTING.md (openai#1245) * chore(internal): update generated pragma comment (openai#1247) * docs: assistant improvements (openai#1249) * release: 1.14.2 --------- Co-authored-by: vvanglro <[email protected]>
1 parent a7446d5 commit 7ae334d

File tree

219 files changed

+331
-233
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+331
-233
lines changed

.release-please-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "1.14.1"
2+
".": "1.14.2"
33
}

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# Changelog
22

3+
## 1.14.2 (2024-03-19)
4+
5+
Full Changelog: [v1.14.1...v1.14.2](https://github.com/openai/openai-python/compare/v1.14.1...v1.14.2)
6+
7+
### Performance Improvements
8+
9+
* cache TypeAdapters ([#1114](https://github.com/openai/openai-python/issues/1114)) ([41b6fee](https://github.com/openai/openai-python/commit/41b6feec70d3f203e36ba9a92205389bafce930c))
10+
* cache TypeAdapters ([#1243](https://github.com/openai/openai-python/issues/1243)) ([2005076](https://github.com/openai/openai-python/commit/2005076f500bef6e0a6cc8f935b9cc9fef65ab5b))
11+
12+
13+
### Chores
14+
15+
* **internal:** update generated pragma comment ([#1247](https://github.com/openai/openai-python/issues/1247)) ([3eeb9b3](https://github.com/openai/openai-python/commit/3eeb9b3a71e01c2593be443a97a353371466d01a))
16+
17+
18+
### Documentation
19+
20+
* assistant improvements ([#1249](https://github.com/openai/openai-python/issues/1249)) ([e7a3176](https://github.com/openai/openai-python/commit/e7a3176b7606822bd5ad8f7fece87de6aad1e5b6))
21+
* fix typo in CONTRIBUTING.md ([#1245](https://github.com/openai/openai-python/issues/1245)) ([adef57a](https://github.com/openai/openai-python/commit/adef57ae5c71734873ba49bccd92fa7f28068d28))
22+
323
## 1.14.1 (2024-03-15)
424

525
Full Changelog: [v1.14.0...v1.14.1](https://github.com/openai/openai-python/compare/v1.14.0...v1.14.1)

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Most tests require you to [set up a mock server](https://github.com/stoplightio/
8686

8787
```bash
8888
# you will need npm installed
89-
npx prism path/to/your/openapi.yml
89+
npx prism mock path/to/your/openapi.yml
9090
```
9191

9292
```bash

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
5151
to add `OPENAI_API_KEY="My API Key"` to your `.env` file
5252
so that your API Key is not stored in source control.
5353

54+
### Streaming Helpers
55+
56+
The SDK also includes helpers to process streams and handle the incoming events.
57+
58+
```python
59+
with client.beta.threads.runs.create_and_stream(
60+
thread_id=thread.id,
61+
assistant_id=assistant.id,
62+
instructions="Please address the user as Jane Doe. The user has a premium account.",
63+
) as stream:
64+
for event in stream:
65+
# Print the text from text delta events
66+
if event.type == "thread.message.delta" and event.data.delta.content:
67+
print(event.data.delta.content[0].text)
68+
```
69+
70+
More information on streaming helpers can be found in the dedicated documentation: [helpers.md](helpers.md)
71+
5472
## Async usage
5573

5674
Simply import `AsyncOpenAI` instead of `OpenAI` and use `await` with each API call:

helpers.md

+70-16
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ You can subscribe to events by creating an event handler class and overloading t
1515

1616
```python
1717
from typing_extensions import override
18-
from openai import AssistantEventHandler
18+
from openai import AssistantEventHandler, OpenAI
19+
from openai.types.beta.threads import Text, TextDelta
20+
from openai.types.beta.threads.runs import ToolCall, ToolCallDelta
21+
22+
client = openai.OpenAI()
1923

2024
# First, we create a EventHandler class to define
2125
# how we want to handle the events in the response stream.
2226

2327
class EventHandler(AssistantEventHandler):
2428
@override
25-
def on_text_created(self, text) -> None:
29+
def on_text_created(self, text: Text) -> None:
2630
print(f"\nassistant > ", end="", flush=True)
2731

2832
@override
29-
def on_text_delta(self, delta, snapshot):
33+
def on_text_delta(self, delta: TextDelta, snapshot: Text):
3034
print(delta.value, end="", flush=True)
3135

32-
def on_tool_call_created(self, tool_call):
36+
def on_tool_call_created(self, tool_call: ToolCall):
3337
print(f"\nassistant > {tool_call.type}\n", flush=True)
3438

35-
def on_tool_call_delta(self, delta, snapshot):
39+
def on_tool_call_delta(self, delta: ToolCallDelta, snapshot: ToolCall):
3640
if delta.type == 'code_interpreter':
3741
if delta.code_interpreter.input:
3842
print(delta.code_interpreter.input, end="", flush=True)
@@ -47,14 +51,64 @@ class EventHandler(AssistantEventHandler):
4751
# and stream the response.
4852

4953
with client.beta.threads.runs.create_and_stream(
50-
thread_id=thread.id,
51-
assistant_id=assistant.id,
52-
instructions="Please address the user as Jane Doe. The user has a premium account.",
54+
thread_id="thread_id",
55+
assistant_id="assistant_id",
5356
event_handler=EventHandler(),
5457
) as stream:
5558
stream.until_done()
5659
```
5760

61+
#### An example of iterating over events
62+
63+
You can also iterate over all the streamed events.
64+
65+
```python
66+
with client.beta.threads.runs.create_and_stream(
67+
thread_id=thread.id,
68+
assistant_id=assistant.id
69+
) as stream:
70+
for event in stream:
71+
# Print the text from text delta events
72+
if event.type == "thread.message.delta" and event.data.delta.content:
73+
print(event.data.delta.content[0].text)
74+
```
75+
76+
#### An example of iterating over text
77+
78+
You can also iterate over just the text deltas received
79+
80+
```python
81+
with client.beta.threads.runs.create_and_stream(
82+
thread_id=thread.id,
83+
assistant_id=assistant.id
84+
) as stream:
85+
for text in stream.text_deltas:
86+
print(text)
87+
```
88+
89+
### Creating Streams
90+
91+
There are three helper methods for creating streams:
92+
93+
```python
94+
client.beta.threads.runs.create_and_stream()
95+
```
96+
97+
This method can be used to start and stream the response to an existing run with an associated thread
98+
that is already populated with messages.
99+
100+
```python
101+
client.beta.threads.create_and_run_stream()
102+
```
103+
104+
This method can be used to add a message to a thread, start a run and then stream the response.
105+
106+
```python
107+
client.beta.threads.runs.submit_tool_outputs_stream()
108+
```
109+
110+
This method can be used to submit a tool output to a run waiting on the output and start a stream.
111+
58112
### Assistant Events
59113

60114
The assistant API provides events you can subscribe to for the following events.
@@ -139,22 +193,22 @@ This event is triggered if an exception occurs during streaming.
139193
The assistant streaming object also provides a few methods for convenience:
140194

141195
```python
142-
def current_event()
143-
def current_run()
144-
def current_message_snapshot()
145-
def current_run_step_snapshot()
196+
def current_event() -> AssistantStreamEvent | None
197+
def current_run() -> Run | None
198+
def current_message_snapshot() -> Message | None
199+
def current_run_step_snapshot() -> RunStep | None
146200
```
147201

148202
These methods are provided to allow you to access additional context from within event handlers. In many cases
149203
the handlers should include all the information you need for processing, but if additional context is required it
150204
can be accessed.
151205

152-
Note: There is not always a relevant context in certain situations (these will be undefined in those cases).
206+
Note: There is not always a relevant context in certain situations (these will be `None` in those cases).
153207

154208
```python
155-
def get_final_run(self)
156-
def get_final_run_steps(self)
157-
def get_final_messages(self)
209+
def get_final_run(self) -> Run
210+
def get_final_run_steps(self) -> List[RunStep]
211+
def get_final_messages(self) -> List[Message]
158212
```
159213

160214
These methods are provided for convenience to collect information at the end of a stream. Calling these events

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "openai"
3-
version = "1.14.1"
3+
version = "1.14.2"
44
description = "The official Python library for the openai API"
55
readme = "README.md"
66
license = "Apache-2.0"

src/openai/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/_constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
import httpx
44

src/openai/_exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/_models.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, cast
55
from datetime import date, datetime
6+
from functools import lru_cache
67
from typing_extensions import (
78
Unpack,
89
Literal,
@@ -533,7 +534,12 @@ class GenericModel(BaseGenericModel, BaseModel):
533534

534535

535536
if PYDANTIC_V2:
536-
from pydantic import TypeAdapter
537+
if TYPE_CHECKING:
538+
from pydantic import TypeAdapter
539+
else:
540+
from pydantic import TypeAdapter as _TypeAdapter
541+
542+
TypeAdapter = lru_cache(_TypeAdapter)
537543

538544
def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
539545
return TypeAdapter(type_).validate_python(value)

src/openai/_module_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from typing_extensions import override
44

src/openai/_resource.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/_version.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
__title__ = "openai"
4-
__version__ = "1.14.1" # x-release-please-version
4+
__version__ = "1.14.2" # x-release-please-version

src/openai/pagination.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from typing import Any, List, Generic, TypeVar, Optional, cast
44
from typing_extensions import Protocol, override, runtime_checkable

src/openai/resources/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .beta import (
44
Beta,

src/openai/resources/audio/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .audio import (
44
Audio,

src/openai/resources/audio/audio.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/audio/speech.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/audio/transcriptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/audio/translations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .beta import (
44
Beta,

src/openai/resources/beta/assistants/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .files import (
44
Files,

src/openai/resources/beta/assistants/assistants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/assistants/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/beta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/threads/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .runs import (
44
Runs,

src/openai/resources/beta/threads/messages/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .files import (
44
Files,

src/openai/resources/beta/threads/messages/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/threads/messages/messages.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/threads/runs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .runs import (
44
Runs,

src/openai/resources/beta/threads/runs/runs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/threads/runs/steps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/beta/threads/threads.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from __future__ import annotations
44

src/openai/resources/chat/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File generated from our OpenAPI spec by Stainless.
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
from .chat import (
44
Chat,

0 commit comments

Comments
 (0)