-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix Azure OpenAI streaming when async content filter is enabled #2763
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
Fix Azure OpenAI streaming when async content filter is enabled #2763
Conversation
|
The liniting issue is happening because of how the Choice model is defined in the openai Python SDK: https://github.com/openai/openai-python/blob/3154a78ac8cb404d64707d63cdfe72d3db8a45be/src/openai/types/chat/chat_completion_chunk.py#L87 As stated in issue #797 delta can effectively be |
| # Handle the text part of the response | ||
| content = choice.delta.content | ||
| if content is not None: | ||
| if (delta := choice.delta) is not None and (content := delta.content) is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will already have failed on the previous line won't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are completely right. Sorry about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this to continue to the next chunk when choice.delta is None.
|
@frednijsvrt If necessary, you can hide the linting issue with a |
|
@DouweM I added a test and did a change to continue to the next chunk when the delta is None since all further processing was done on the delta. |
|
@frednijsvrt Thanks Fred! |
Fixes #797
This PR is an identical fix as the PR #932 and fixes an issue happening with Azure OpenAI streaming Response causing the raise of an AttributeError.
As stated on the #797, the error occurs in pydantic-ai's OpenAI model implementation where it assumes every delta in the streaming response contains a content field. However, with Azure OpenAI's API, some deltas (like role initialization) may not include content when an async content filter is enabled, resulting in choice.delta.content being None.
More info on async content filters: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter?tabs=warning%2Cuser-prompt%2Cpython-new#asynchronous-filter