Skip to content

Python: Fix OpenAIEmbeddingClient to use AsyncOpenAI for /openai/v1 endpoints#5137

Open
chetantoshniwal wants to merge 4 commits intomicrosoft:mainfrom
chetantoshniwal:agent/fix-5068-1
Open

Python: Fix OpenAIEmbeddingClient to use AsyncOpenAI for /openai/v1 endpoints#5137
chetantoshniwal wants to merge 4 commits intomicrosoft:mainfrom
chetantoshniwal:agent/fix-5068-1

Conversation

@chetantoshniwal
Copy link
Copy Markdown
Contributor

Motivation and Context

When using the Azure /openai/v1 endpoint, OpenAIEmbeddingClient routed requests through AsyncAzureOpenAI, which rewrites request paths by inserting /deployments/{model}/. This produced 404 errors because the /openai/v1 endpoint expects standard OpenAI-compatible paths.

Fixes #5068

Description

The root cause is that AsyncAzureOpenAI modifies request URLs (e.g., /embeddings/deployments/{model}/embeddings), which is incompatible with the /openai/v1 API surface. The fix detects when the resolved base_url ends with /openai/v1 (and the caller is not using the Responses API, whose /responses path is not rewritten) and constructs an AsyncOpenAI client instead, preserving request URLs as-is. A helper _ensure_async_token_provider wraps potentially synchronous Azure token providers into awaitables as required by AsyncOpenAI. Tests verify that both credential-based and API-key-based configurations correctly produce an AsyncOpenAI client for /openai/v1 URLs, while standard Azure endpoints continue using AsyncAzureOpenAI.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Note: PR autogenerated by chetantoshniwal's agent

MAF Dashboard Bot and others added 2 commits April 7, 2026 05:23
When base_url ends with /openai/v1/ and a credential is provided,
load_openai_service_settings was creating an AsyncAzureOpenAI client.
The Azure SDK rewrites deployment-based endpoints (including /embeddings)
by inserting /deployments/{model}/ into the URL, producing 404s on the
OpenAI-compatible /openai/v1 endpoint.

Use AsyncOpenAI instead of AsyncAzureOpenAI when the resolved base_url
targets /openai/v1, converting the Azure token provider to an async
api_key callable. The responses_mode path is unaffected because the
Responses API (/responses) is not in the SDK's rewrite list.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 7, 2026 05:38
@chetantoshniwal chetantoshniwal self-assigned this Apr 7, 2026
@moonbox3 moonbox3 added the python label Apr 7, 2026
Copy link
Copy Markdown
Contributor Author

@chetantoshniwal chetantoshniwal left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 97% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by chetantoshniwal's agents

@moonbox3
Copy link
Copy Markdown
Contributor

moonbox3 commented Apr 7, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/openai/agent_framework_openai
   _shared.py1501391%239–241, 253, 263, 275, 281, 337–338, 357, 376–377, 379
TOTAL27072318788% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5378 20 💤 0 ❌ 0 🔥 1m 25s ⏱️

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Azure /openai/v1 compatibility for OpenAIEmbeddingClient by avoiding AsyncAzureOpenAI path rewriting that incorrectly inserts /deployments/{model}/... for OpenAI-compatible endpoints.

Changes:

  • Route Azure /openai/v1 embedding requests through AsyncOpenAI (instead of AsyncAzureOpenAI) to preserve OpenAI-compatible request paths.
  • Add _ensure_async_token_provider to normalize Azure token providers for use as AsyncOpenAI api_key callables.
  • Add unit tests covering /openai/v1 initialization (credential + API key) and ensuring standard Azure endpoints still use AsyncAzureOpenAI.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
python/packages/openai/agent_framework_openai/_shared.py Detects Azure /openai/v1 base URLs (non-Responses mode) and constructs an AsyncOpenAI client to avoid Azure SDK path rewriting; adds token-provider wrapper helper.
python/packages/openai/tests/openai/test_openai_embedding_client_azure.py Adds unit tests verifying OpenAIEmbeddingClient selects AsyncOpenAI for /openai/v1 base URLs and keeps AsyncAzureOpenAI for standard Azure endpoints.
python/packages/a2a/tests/test_a2a_agent.py Minor test formatting change (list literal formatting for .extend).

MAF Dashboard Bot and others added 2 commits April 7, 2026 05:59
…hanges

- Revert unrelated formatting change in test_a2a_agent.py
- Fix test_init_with_openai_v1_base_url_and_api_key_uses_openai_client to
  exercise the Azure settings path (via AZURE_OPENAI_BASE_URL env var)
  instead of the plain OpenAI path, covering the elif api_key branch
- Add _ensure_async_token_provider unit tests for both sync and async
  token providers

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…eddingClient` does not work with `/openai/v1` endpoint
Copy link
Copy Markdown
Contributor Author

@chetantoshniwal chetantoshniwal left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 97% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by chetantoshniwal's agents

Copy link
Copy Markdown

@Hopeduardo Hopeduardo left a comment

Choose a reason for hiding this comment

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

Txs

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: OpenAIEmbeddingClient does not work with /openai/v1 endpoint

4 participants