-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
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
From the docs at https://github.com/openai/openai-python/blob/main/helpers.md:
from typing_extensions import override
from openai import AssistantEventHandler, OpenAI
from openai.types.beta.threads import Text, TextDelta
from openai.types.beta.threads.runs import ToolCall, ToolCallDelta
client = OpenAI() # NOTE: this is an edit to the original code: `client = openai.OpenAI()`
class EventHandler(AssistantEventHandler):
@override
def on_text_created(self, text: Text) -> None:
print(f"\nassistant > ", end="", flush=True)
@override
def on_text_delta(self, delta: TextDelta, snapshot: Text):
print(delta.value, end="", flush=True)
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: ToolCallDelta, snapshot: ToolCall):
if delta.type == 'code_interpreter':
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)
with client.beta.threads.runs.stream(
thread_id=thread.id, # using real thread ID
assistant_id=assistant.id # using real assistant ID
) as stream:
for event in stream:
if event.type == "thread.message.delta" and event.data.delta.content:
print(event.data.delta.content[0].text)
throws the following error:
File "/workspaces/ChatGPT-Arc/tmp/sandbox.py", line 108, in main
stream_run(client, thread, assistant)
File "/workspaces/ChatGPT-Arc/tmp/sandbox.py", line 80, in stream_run
if event.type == "thread.message.delta" and event.data.delta.content:
File "/usr/local/lib/python3.9/site-packages/pydantic/main.py", line 792, in __getattr__
raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'ThreadRunCreated' object has no attribute 'type'
It appears that the docs are out-of-date.
Using event.data.object
instead of event.type
works for me:
for event in stream:
if event.data.object == "thread.run.step.delta" and event.data.delta.content:
print(event.data.delta.content[0].text)
Note also that client = openai.OpenAI()
should be client = OpenAI()
in the example code.
To Reproduce
See above
Code snippets
No response
OS
macOS
Python version
3.9.19
Library version
1.21.2
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working