Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@RobertCraigie Sorry, there are still some issue #1994

Open
CharlieChiu5 opened this issue Jan 8, 2025 · 0 comments
Open

@RobertCraigie Sorry, there are still some issue #1994

CharlieChiu5 opened this issue Jan 8, 2025 · 0 comments

Comments

@CharlieChiu5
Copy link

          @RobertCraigie Sorry, there are still some issue
client = OpenAI()

class EventHandler(AssistantEventHandler):
    def __init__(self) -> None:
        super().__init__()

    @override
    def on_event(self, event: AssistantStreamEvent) -> None:
        if isinstance(event, ThreadRunStepCompleted):
            if event.data.type == 'tool_calls':
                for tool_call in event.data.step_details.tool_calls:
                    # type(tool_call) is <class 'dict'>
                    if tool_call.type == 'code_interpreter':
                        pass
                    elif tool_call.type == 'file_search':
                        pass

    @override
    def _emit_sse_event(self, event: AssistantStreamEvent) -> None:
        if event.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",
)

with client.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()
) as stream:
    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

Originally posted by @kunerzzz in #1574 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant