openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
23.08k stars 3.25k forks source link

Pydantic-V1 "warnings" error #1882

Closed Konnor-Young closed 1 hour ago

Konnor-Young commented 18 hours ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

I am getting this Error:

File "/Users/konnoryoung/Documents/projects/contract-ai-poc/.errors/lib/python3.9/site-packages/openai/lib/streaming/_assistants.py", line 943, in accumulate_event
block = current_message_snapshot.content[content_delta.index]
IndexError: list index out of range

Which comes from the try/except block in the "accumulate_event" function:

try:
    block = current_message_snapshot.content[content_delta.index]
except IndexError:
    current_message_snapshot.content.insert(
    content_delta.index,
    cast(
        MessageContent,
        construct_type(
            # mypy doesn't allow Content for some reason
            type_=cast(Any, MessageContent),
            value=model_dump(content_delta, exclude_unset=True, warnings=False),
            ),
        ),
    )
    new_content.append(content_delta)

That wouldn't stop my code from running except that it raises another exception:

File "/Users/konnoryoung/Documents/projects/contract-ai-poc/.errors/lib/python3.9/site-packages/openai/_models.py", line 313, in model_dump
raise ValueError("warnings is only supported in Pydantic v2")
ValueError: warnings is only supported in Pydantic v2

Which comes from the if statement:

if warnings != True:
    raise ValueError("warnings is only supported in Pydantic v2")

In the if not PYDANTIC_V2 version of the model_dump method of the BaseModel class in the _model.py file.

To Reproduce

1- Pydantic < 2 (I have v1.9.0) 2- Create an assistant 3- Create a thread 4- Attempt to run an Async Stream (my code is below)

Code snippets

from __future__ import annotations

import asyncio

import openai

client = openai.AsyncOpenAI()

assistant_id= """ GetID OR Create an Assistant """
thread_id= """  GetID OR Create a Thread """
async def main() -> None:
    async with client.beta.threads.runs.stream(
        thread_id=thread_id,
        assistant_id=assistant_id,
        ) as stream:
            async for event in stream:
                print()

asyncio.run(main())

OS

masOS

Python version

Python 3.9.6

Library version

openai v1.55.0

RobertCraigie commented 2 hours ago

Thanks for the example, I can reproduce the issue. Working on a fix.

RobertCraigie commented 1 hour ago

@Konnor-Young this will be fixed in the next release! https://github.com/openai/openai-python/pull/1886