Skip to content

Azure OpenAI API Streaming Response Causes AttributeError in pydantic-ai #797

Open
@dabrodev

Description

@dabrodev

When using pydantic-ai with Azure OpenAI's streaming API, an AttributeError occurs due to the API returning deltas without content fields.

Steps to Reproduce

  1. Configure Azure OpenAI client with api_version="2024-12-01-preview"
  2. Create an OpenAIModel using the Azure client
  3. Use Agent.run_stream() with the model
  4. Attempt to process streaming response

Error:

Traceback (most recent call last):
  File ".../pydantic_ai/models/openai.py", line 306, in _get_event_iterator
    content = choice.delta.content
             ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'content'

Analysis
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, resulting in choice.delta.content being None.

Expected Behavior
The streaming implementation should handle deltas without content fields gracefully, possibly by:

  • Checking if delta.content exists before accessing it
  • Skipping deltas without content
  • Or handling special message types separately

Current Workaround

Using API version "2023-05-15" works but has limitations:

  • Streaming chunks are much larger (several sentences long)
  • Less granular streaming experience compared to standard OpenAI API
  • Makes the streaming appear more "blocky" rather than word-by-word

Environment:

  • pydantic-ai: 0.0.20
  • OpenAI Python package version: 1.60.2
  • Azure OpenAI API version tested: 2024-12-01-preview

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions