Skip to content

Azure Open AI Async filter results in None type content part #2313

Open
@martgra

Description

@martgra

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

  • This is an issue with the Python library

Describe the bug

When using the async filter in Azure, stream mode, the SDK returns None types. Unsure whether this is a issue in the API or the SDK but since this is a fundament package for many high level libraries the fix could be implemented here.

Ref other reported issues:
pydantic/pydantic-ai#797
microsoft/semantic-kernel#7250
langchain-ai/langchain#25951

To Reproduce

  1. Set Azure Open AI completion model (GPT-4x) to Async content filter (stream mode)
  2. Run stream with async client
  3. Try accessing content gives unexpected None type.

Error will only occur with async content filter.

Ref error:

  File "/workspace/backend/api/services/domain_service.py", line 50, in get_answer
    async for response in self.message_service.get_grounded_message_as_stream(
  File "/workspace/backend/api/services/message_service.py", line 83, in get_grounded_message_as_stream
    async for chunk in self.chat_consumer.get_message_as_stream(message_history_with_context):
  File "/workspace/backend/api/consumers/chats/azure_openai_consumer.py", line 56, in get_message_as_stream
    content = chunk.choices[0].delta.content
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'content'

Code snippets

async with await client.chat.completions.create(
                messages=messages,
                model="gpt-4o",
                stream=True,
            ) as stream:
                async for chunk in stream:  
                    if not (
                        chunk
                        and chunk.choices
                    ):
                        continue

                    content = chunk.choices[0].delta.content

OS

Debian Bookworm, Mac OS

Python version

Python v3.12

Library version

open ai v.1.69.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions