Skip to content

Conversation

@frednijsvrt
Copy link
Contributor

@frednijsvrt frednijsvrt commented Sep 2, 2025

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

@frednijsvrt
Copy link
Contributor Author

frednijsvrt commented Sep 2, 2025

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

class Choice(BaseModel):
    delta: ChoiceDelta
    """A chat completion delta generated by streamed model responses."""

As stated in issue #797 delta can effectively be None.

# 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:
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@DouweM
Copy link
Collaborator

DouweM commented Sep 2, 2025

@frednijsvrt If necessary, you can hide the linting issue with a # pyright: ignore[reportUnnecessaryComparison] comment. Also please add a test for this scenario!

@frednijsvrt
Copy link
Contributor Author

@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 frednijsvrt requested a review from DouweM September 3, 2025 08:58
@DouweM DouweM changed the title Fix bug related to Azure OpenAI model streaming response Fix Azure OpenAI streaming when async content filter is enabled Sep 3, 2025
@DouweM DouweM enabled auto-merge (squash) September 3, 2025 14:32
@DouweM
Copy link
Collaborator

DouweM commented Sep 3, 2025

@frednijsvrt Thanks Fred!

@DouweM DouweM merged commit e30c444 into pydantic:main Sep 3, 2025
82 of 87 checks passed
@frednijsvrt frednijsvrt deleted the fix/azure-openai-streaming-response branch September 4, 2025 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Azure OpenAI API Streaming Response Causes AttributeError in pydantic-ai

2 participants