Open
Description
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
- Configure Azure OpenAI client with api_version="2024-12-01-preview"
- Create an OpenAIModel using the Azure client
- Use Agent.run_stream() with the model
- 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