Skip to content

Python: Prefer an injected Azure OpenAI deployment over environment model aliases - #9987

Open
ZedingZhang (ZedingZhang) wants to merge 2 commits into
microsoft:mainfrom
ZedingZhang:fix/azure-openai-injected-deployment-9949
Open

ZedingZhang (ZedingZhang) wants to merge 2 commits into
microsoft:mainfrom
ZedingZhang:fix/azure-openai-injected-deployment-9949

Conversation

@ZedingZhang

Copy link
Copy Markdown
Contributor

Motivation & Context

An injected AsyncAzureOpenAI client already has a deployment, but the shared OpenAI settings resolver reads AZURE_OPENAI_* model variables first. The wrapper then calls a different deployment than the SDK client the caller supplied. Chat, chat-completions, and embedding clients all go through this path.

Description & Review Guide

  • What are the major changes? In load_openai_service_settings, resolve the Azure model as explicit model, then the injected AsyncAzureOpenAI deployment, then the environment aliases. Document that order and add regression tests for the three public clients.
  • What is the impact of these changes? Injected Azure clients keep the caller's deployment when model is omitted. An explicit model still wins. A plain AsyncOpenAI client still uses the OpenAI model variables. Creating a client without an injected client is unchanged.
  • What do you want reviewers to focus on? The precedence change is only in the Azure branch of load_openai_service_settings. Hosts that previously let an ambient Azure model variable override an injected client's deployment will now follow the client.

Related Issue

Fixes #9949. The issue has the ready-for-implementation label; no competing open PR was found before submission.

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution.

AI assistance details: Cursor's coding agent assisted with the implementation, regression tests, docstring and README updates, and this PR description. I reviewed the change and accept responsibility for it.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible — new injected-client tests plus the existing Azure unit tests: 49 passed, 37 integration tests deselected. Ruff and Pyright report no issues on the changed files.
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

…ases

An injected AsyncAzureOpenAI client was losing its configured deployment
to AZURE_OPENAI_* model variables. Use an explicit model first, then that
deployment, then the environment.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new inline SDK-client factory violates the required test-helper layout and must be moved to a callable factory fixture or an imported helper module.

1 open issue
What changed in this PR

Fixes #9949 by making the shared Python OpenAI settings resolver prefer an injected Azure client's deployment over ambient model variables.

Changes:

  • Sets Azure model precedence to explicit model, injected deployment, then environment aliases.
  • Adds regression tests across chat, chat-completions, and embedding clients.
  • Documents the precedence while preserving plain OpenAI and no-client behavior.
File Description
python/​packages/​openai/​tests/​openai/​test_injected_client_model.py Tests injected-client precedence, explicit overrides, and environment fallback.
python/​packages/​openai/​README.md Explains injected deployment precedence and client ownership.
python/​packages/​openai/​AGENTS.md Documents request-model resolution order.
python/​packages/​openai/​agent_framework_openai/​_shared.py Resolves injected Azure deployments before environment aliases.
python/​packages/​openai/​agent_framework_openai/​_embedding_client.py Updates raw and layered embedding constructor docstrings.
python/​packages/​openai/​agent_framework_openai/​_chat_completion_client.py Updates raw and layered chat-completion constructor docstrings.
python/​packages/​openai/​agent_framework_openai/​_chat_client.py Updates raw and layered chat constructor docstrings.

Review effort: Balanced

]


def _azure_sdk_client(deployment: str | None) -> AsyncAzureOpenAI:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved _azure_sdk_client to the azure_sdk_client_factory fixture in tests/openai/conftest.py. The Azure tests now receive it through pytest injection, matching this package's fixture layout and CODING_STANDARD (callable factory fixture for parameterized setup). There was no _openai_sdk_client helper to move. Coverage is unchanged: injected Azure deployment preferred over env aliases, explicit model wins, and plain AsyncOpenAI still uses the OpenAI env vars.

Parameterized reusable setup belongs in a callable factory fixture, not an
inline test helper.

Co-authored-by: ZedingZhang <me@zedingzhang.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the Azure model-precedence update.

1 open issue

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — 1cc89875 Deployed Nov 2, 2026 by cursor[bot] via add_label #26881
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Azure OpenAI environment model overrides an injected client's deployment

2 participants