Skip to content
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

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

Open
dabrodev opened this issue Jan 28, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@dabrodev
Copy link

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
@sydney-runkle sydney-runkle added the bug Something isn't working label Jan 29, 2025
@sydney-runkle
Copy link
Member

Thanks for the report @dabrodev, I think we could probably work around this as you've suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants