You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
client=OpenAI()
classEventHandler(AssistantEventHandler):
def__init__(self) ->None:
super().__init__()
@overridedefon_event(self, event: AssistantStreamEvent) ->None:
ifisinstance(event, ThreadRunStepCompleted):
ifevent.data.type=='tool_calls':
fortool_callinevent.data.step_details.tool_calls:
# type(tool_call) is <class 'dict'>iftool_call.type=='code_interpreter':
passeliftool_call.type=='file_search':
pass@overridedef_emit_sse_event(self, event: AssistantStreamEvent) ->None:
ifevent.event=="thread.run.step.delta":
print(f'{type(event)}{type(event.data)}{type(event.data.delta)}{str(event.data.delta)}')
super()._emit_sse_event(event)
client=OpenAI()
assistant=client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-4o-mini",
)
withclient.beta.threads.create_and_run_stream(
assistant_id=assistant.id,
model="gpt-4o-mini",
instructions="Use browser tool first to answer the user's question",
thread={"messages": [{"role": "user", "content": "Who is Tom"}]},
tool_choice="required",
event_handler=EventHandler()
) asstream:
stream.until_done()
Output:
<class 'openai.types.beta.assistant_stream_event.ThreadRunStepDelta'> <class 'openai.types.beta.threads.runs.run_step_delta_event.RunStepDeltaEvent'> <class 'openai.types.beta.threads.runs.run_step_delta.RunStepDelta'>
RunStepDelta(step_details=ToolCallDeltaObject(type='tool_calls', tool_calls=[CodeInterpreterToolCallDelta(index=0, type='browser', id='call_6GeU3til5JdXMxUCRmjLZK0L', code_interpreter=None, browser={})]))
/lib/python3.12/site-packages/pydantic/main.py:308: UserWarning: Pydantic serializer warnings:
Expected `Union[RunStepDeltaMessageDelta, ToolCallDeltaObject]` but got `ToolCallDeltaObject` - serialized value may not be as expected
Expected `Union[CodeInterpreterToolCallDelta, FileSearchToolCallDelta, FunctionToolCallDelta]` but got `CodeInterpreterToolCallDelta` - serialized value may not be as expected
return self.__pydantic_serializer__.to_python(
Traceback (most recent call last):
File "demo.py", line 51, in <module>
stream.until_done()
File "/lib/python3.12/site-packages/openai/lib/streaming/_assistants.py", line 102, in until_done
consume_sync_iterator(self)
File "/lib/python3.12/site-packages/openai/_utils/_streams.py", line 6, in consume_sync_iterator
for _ in iterator:
File "/lib/python3.12/site-packages/openai/lib/streaming/_assistants.py", line 69, in __iter__
for item in self._iterator:
File "/lib/python3.12/site-packages/openai/lib/streaming/_assistants.py", line 406, in __stream__
self._emit_sse_event(event)
File "demo.py", line 31, in _emit_sse_event
super()._emit_sse_event(event)
File "/lib/python3.12/site-packages/openai/lib/streaming/_assistants.py", line 256, in _emit_sse_event
self.on_event(event)
File "demo.py", line 22, in on_event
if tool_call.type == 'code_interpreter':
^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'type'
New environment:
> python -m pip list | grep -E 'openai|pydantic'
openai 1.36.0
pydantic 2.8.2
pydantic_core 2.20.1
> python --version
Python 3.12.2
> uname -a
Darwin Kernel Version 23.3.0
Output:
New environment:
Originally posted by @kunerzzz in #1574 (comment)
The text was updated successfully, but these errors were encountered: