Skip to content

Conversation

@romanlutz
Copy link
Contributor

@romanlutz romanlutz commented Nov 19, 2025

Description

Migrating (back) to the openai SDK since it appears to have matured to a point where we can actually use it. This simplifies some things for us as we don't have to manually craft HTTP messages anymore.

The PR handles this change and adds more structure for error handling. This can be observed in the OpenAITarget abstract base class with _handle_openai_request which is called by each target. In turn, it calls the provided API (this is specific per target, e.g., responses API, or chat completions API), catches any exceptions (e.g., input content filters, bad requests, rate limit error, and others), and if it succeeds runs _check_content_filter (for output filters) followed by _handle_content_filter_response in case of filters. It also validates the response with _validate_response and finally returns the result (which may be an error Message or a successful Message).

Note: this is pending another thorough re-review by myself and then a review from other maintainers.

Note: This does not yet add token provider capabilities instead of the API key. That will require a follow-up PR.

Also done here:

  • renamed Dalle target to Image target
  • renamed Sora target to Video target
  • support new post-GA style Azure URLs that are more aligned with OpenAI to replace our usage of the AzureOpenAI client. Old-style URLs result in a warning but will not be fixed.

Tests and Documentation

  • Unit tests ✅
  • pre-commit ✅
  • integration tests (also added new ones)
    • general for targets ✅
    • content filters ✅
    • ollama (tbd)
    • notebooks ✅

# Use the Azure SDK's async get_bearer_token_provider for proper token management
# This returns an async callable that the OpenAI SDK can await natively
scope = get_default_scope(self._endpoint)
api_key_value = get_async_token_provider_from_default_azure_credential(scope)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing related to #1173 is that api_key can actually be a token provider now! So I suggest we tackle that here as well and redo this portion.

Currently, we have an arg use_entra_auth which adds a bearer token based on the default scope as indicated by the endpoint URL. Scope here means that we get the token for ml.azure.com or cognitiveservices.azure.com. It's possible that there could be other scopes, too, but we just don't support that right now.

My proposal:

  • Remove use_entra_auth
  • If no api_key is provided then attempt to use default scope based on the URL and do Entra auth.
  • If api_key is provided as key, use it that way
  • If api_key is provided as token provider, use that token provider and pass it to the AsyncOpenAI constructor

More customizable ✅
Solves #1173
Removes an input arg to simplify ✅

The other thing going on here is that Azure had its own client class AsyncAzureOpenAI but with the new GA format we just use AsyncOpenAI which greatly simplifies things, too, at least once we can ignore the old-style formats. I suggest we don't stop supporting them until we don't use them anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to punt this to another PR because this is already a lot...

@romanlutz romanlutz changed the title [DRAFT] FEAT migrate to openai SDK for openai API targets FEAT migrate to openai SDK for openai API targets Dec 2, 2025
…small PR suggestions, text fixes, response target notebook tests working
@romanlutz romanlutz changed the title FEAT migrate to openai SDK for openai API targets BREAKING FEAT migrate to openai SDK for openai API targets Dec 2, 2025
Copy link
Contributor

@rlundeen2 rlundeen2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd wait a day for other folks to take a look, but great work addressing all my concerns. It looks great!

self._validate_duration()
self._size = self._validate_resolution(resolution_dimensions=resolution_dimensions)

def _set_openai_env_configuration_vars(self) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _set_openai_env_configuration_vars(self) -> None:
def _set_openai_env_configuration_vars(self) :

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a technically correct type annotation, isn't it (?)

Copy link
Contributor

@hannahwestra25 hannahwestra25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comments but looks good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants