Getting a very weird error:
AttributeError: ‘ModelField’ object has no attribute ‘annotation’
My code is very simple:
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I want to see trends between amount and supplier",
attachments=[
{
"file_id": file.id,
"tools": [{"type": "code_interpreter"}]
}
]
)
with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
event_handler=EventHandler(assistant.name)
) as stream:
stream.until_done()
Any advice is greatly appreciated!
What does EventHandler function look like? That ‘annotation’ term usually means the citations processing in the EventHandler is expecting an attribute in the message content that is not present.
Oh okay, this is my EventHandler:
class EventHandler(AsyncAssistantEventHandler):
def __init__(self, assistant_name: str) -> None:
super().__init__()
self.current_message: cl.Message = None
self.current_step: cl.Step = None
self.current_tool_call = None
self.assistant_name = assistant_name
async def on_text_created(self, text) -> None:
self.current_message = await cl.Message(author=self.assistant_name, content="").send()
async def on_text_delta(self, delta, snapshot):
await self.current_message.stream_token(delta.value)
async def on_text_done(self, text):
await self.current_message.update()
async def on_tool_call_created(self, tool_call):
self.current_tool_call = tool_call.id
self.current_step = cl.Step(name=tool_call.type, type="tool")
self.current_step.language = "python"
self.current_step.created_at = utc_now()
await self.current_step.send()
async def on_tool_call_delta(self, delta, snapshot):
if snapshot.id != self.current_tool_call:
self.current_tool_call = snapshot.id
self.current_step = cl.Step(name=delta.type, type="tool")
self.current_step.language = "python"
self.current_step.start = utc_now()
await self.current_step.send()
if delta.type == "code_interpreter":
if delta.code_interpreter.outputs:
for output in delta.code_interpreter.outputs:
if output.type == "logs":
error_step = cl.Step(
name=delta.type,
type="tool"
)
error_step.is_error = True
error_step.output = output.logs
error_step.language = "markdown"
error_step.start = self.current_step.start
error_step.end = utc_now()
await error_step.send()
else:
if delta.code_interpreter.input:
await self.current_step.stream_token(delta.code_interpreter.input)
async def on_tool_call_done(self, tool_call):
self.current_step.end = utc_now()
await self.current_step.update()
async def on_image_file_done(self, image_file):
image_id = image_file.file_id
response = await async_openai_client.files.with_raw_response.content(image_id)
image_element = cl.Image(
name=image_id,
content=response.content,
display="inline",
size="large"
)
if not self.current_message.elements:
self.current_message.elements = []
self.current_message.elements.append(image_element)
await self.current_message.update()
I got it from a guide online, and in the video their code ran smoothly with no errors. I also tried calling the stream without the EventHandler to debug, and I actually got the same error still:
with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
) as stream:
stream.until_done()
If you search for ModelField in all your code and nothing shows up, then that is very weird.
Also, what import statements do you have?
I Ctrl F and didn’t find ModelField at all, I assumed it was some random object in a source code file. As for my imports, here they are:
from openai import AzureOpenAI, __version__
from openai import AssistantEventHandler
from typing_extensions import override
import json
import pandas as pd
import time
import io
from IPython.display import display, Markdown, clear_output # Used for displaying content in Jupyter Notebooks
import asyncio
import random
It’s probably this line:
from openai import AssistantEventHandler
Someone else needs to explain how that works as I haven’t used that import and don’t have documentation for it.
1 Like
I also just encountered this same error being thrown by the openai python sdk. I solved it by upgrading to pydantic==2.8.2 (i was running 1.9.0).
Looking into the python sdk, it was doing a check for if PYDANTIC_V2:
and so upgrading to v2 seemed to bypass the section of code that was throwing the error.