Skip to content

Commit

Permalink
release: 1.14.2 (openai#1244)
Browse files Browse the repository at this point in the history
* 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 <947001731@qq.com>
  • Loading branch information
stainless-bot and vvanglro authored Mar 19, 2024
1 parent e8e5a0d commit 5cfb125
Show file tree
Hide file tree
Showing 219 changed files with 331 additions and 233 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "1.14.1"
".": "1.14.2"
}
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Changelog

## 1.14.2 (2024-03-19)

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

### Performance Improvements

* cache TypeAdapters ([#1114](https://github.com/openai/openai-python/issues/1114)) ([41b6fee](https://github.com/openai/openai-python/commit/41b6feec70d3f203e36ba9a92205389bafce930c))
* cache TypeAdapters ([#1243](https://github.com/openai/openai-python/issues/1243)) ([2005076](https://github.com/openai/openai-python/commit/2005076f500bef6e0a6cc8f935b9cc9fef65ab5b))


### Chores

* **internal:** update generated pragma comment ([#1247](https://github.com/openai/openai-python/issues/1247)) ([3eeb9b3](https://github.com/openai/openai-python/commit/3eeb9b3a71e01c2593be443a97a353371466d01a))


### Documentation

* assistant improvements ([#1249](https://github.com/openai/openai-python/issues/1249)) ([e7a3176](https://github.com/openai/openai-python/commit/e7a3176b7606822bd5ad8f7fece87de6aad1e5b6))
* fix typo in CONTRIBUTING.md ([#1245](https://github.com/openai/openai-python/issues/1245)) ([adef57a](https://github.com/openai/openai-python/commit/adef57ae5c71734873ba49bccd92fa7f28068d28))

## 1.14.1 (2024-03-15)

Full Changelog: [v1.14.0...v1.14.1](https://github.com/openai/openai-python/compare/v1.14.0...v1.14.1)
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Most tests require you to [set up a mock server](https://github.com/stoplightio/

```bash
# you will need npm installed
npx prism path/to/your/openapi.yml
npx prism mock path/to/your/openapi.yml
```

```bash
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
to add `OPENAI_API_KEY="My API Key"` to your `.env` file
so that your API Key is not stored in source control.

### Streaming Helpers

The SDK also includes helpers to process streams and handle the incoming events.

```python
with client.beta.threads.runs.create_and_stream(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account.",
) as stream:
for event in stream:
# Print the text from text delta events
if event.type == "thread.message.delta" and event.data.delta.content:
print(event.data.delta.content[0].text)
```

More information on streaming helpers can be found in the dedicated documentation: [helpers.md](helpers.md)

## Async usage

Simply import `AsyncOpenAI` instead of `OpenAI` and use `await` with each API call:
Expand Down
86 changes: 70 additions & 16 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ You can subscribe to events by creating an event handler class and overloading t

```python
from typing_extensions import override
from openai import AssistantEventHandler
from openai import AssistantEventHandler, OpenAI
from openai.types.beta.threads import Text, TextDelta
from openai.types.beta.threads.runs import ToolCall, ToolCallDelta

client = openai.OpenAI()

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

class EventHandler(AssistantEventHandler):
@override
def on_text_created(self, text) -> None:
def on_text_created(self, text: Text) -> None:
print(f"\nassistant > ", end="", flush=True)

@override
def on_text_delta(self, delta, snapshot):
def on_text_delta(self, delta: TextDelta, snapshot: Text):
print(delta.value, end="", flush=True)

def on_tool_call_created(self, tool_call):
def on_tool_call_created(self, tool_call: ToolCall):
print(f"\nassistant > {tool_call.type}\n", flush=True)

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

with client.beta.threads.runs.create_and_stream(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account.",
thread_id="thread_id",
assistant_id="assistant_id",
event_handler=EventHandler(),
) as stream:
stream.until_done()
```

#### An example of iterating over events

You can also iterate over all the streamed events.

```python
with client.beta.threads.runs.create_and_stream(
thread_id=thread.id,
assistant_id=assistant.id
) as stream:
for event in stream:
# Print the text from text delta events
if event.type == "thread.message.delta" and event.data.delta.content:
print(event.data.delta.content[0].text)
```

#### An example of iterating over text

You can also iterate over just the text deltas received

```python
with client.beta.threads.runs.create_and_stream(
thread_id=thread.id,
assistant_id=assistant.id
) as stream:
for text in stream.text_deltas:
print(text)
```

### Creating Streams

There are three helper methods for creating streams:

```python
client.beta.threads.runs.create_and_stream()
```

This method can be used to start and stream the response to an existing run with an associated thread
that is already populated with messages.

```python
client.beta.threads.create_and_run_stream()
```

This method can be used to add a message to a thread, start a run and then stream the response.

```python
client.beta.threads.runs.submit_tool_outputs_stream()
```

This method can be used to submit a tool output to a run waiting on the output and start a stream.

### Assistant Events

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

```python
def current_event()
def current_run()
def current_message_snapshot()
def current_run_step_snapshot()
def current_event() -> AssistantStreamEvent | None
def current_run() -> Run | None
def current_message_snapshot() -> Message | None
def current_run_step_snapshot() -> RunStep | None
```

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

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

```python
def get_final_run(self)
def get_final_run_steps(self)
def get_final_messages(self)
def get_final_run(self) -> Run
def get_final_run_steps(self) -> List[RunStep]
def get_final_messages(self) -> List[Message]
```

These methods are provided for convenience to collect information at the end of a stream. Calling these events
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 = "1.14.1"
version = "1.14.2"
description = "The official Python library for the openai API"
readme = "README.md"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/openai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/_constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import httpx

Expand Down
2 changes: 1 addition & 1 deletion src/openai/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
8 changes: 7 additions & 1 deletion src/openai/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
from typing import TYPE_CHECKING, Any, Type, Union, Generic, TypeVar, Callable, cast
from datetime import date, datetime
from functools import lru_cache
from typing_extensions import (
Unpack,
Literal,
Expand Down Expand Up @@ -533,7 +534,12 @@ class GenericModel(BaseGenericModel, BaseModel):


if PYDANTIC_V2:
from pydantic import TypeAdapter
if TYPE_CHECKING:
from pydantic import TypeAdapter
else:
from pydantic import TypeAdapter as _TypeAdapter

TypeAdapter = lru_cache(_TypeAdapter)

def _validate_non_model_type(*, type_: type[_T], value: object) -> _T:
return TypeAdapter(type_).validate_python(value)
Expand Down
2 changes: 1 addition & 1 deletion src/openai/_module_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from typing_extensions import override

Expand Down
2 changes: 1 addition & 1 deletion src/openai/_resource.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
4 changes: 2 additions & 2 deletions src/openai/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "openai"
__version__ = "1.14.1" # x-release-please-version
__version__ = "1.14.2" # x-release-please-version
2 changes: 1 addition & 1 deletion src/openai/pagination.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from typing import Any, List, Generic, TypeVar, Optional, cast
from typing_extensions import Protocol, override, runtime_checkable
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .beta import (
Beta,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/audio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .audio import (
Audio,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/audio/audio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/audio/speech.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/audio/transcriptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/audio/translations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .beta import (
Beta,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/assistants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .files import (
Files,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/assistants/assistants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/assistants/files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/beta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .runs import (
Runs,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .files import (
Files,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/messages/files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/messages/messages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/runs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .runs import (
Runs,
Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/runs/runs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/runs/steps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/beta/threads/threads.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion src/openai/resources/chat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless.
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from .chat import (
Chat,
Expand Down
Loading

0 comments on commit 5cfb125

Please sign in to comment.