Repository navigation
Python: Prefer an injected Azure OpenAI deployment over environment model aliases - #9987
ZedingZhang (ZedingZhang) wants to merge 2 commits into
Conversation
…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.
There was a problem hiding this comment.
🟡 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: |
There was a problem hiding this comment.
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>

Motivation & Context
An injected
AsyncAzureOpenAIclient already has a deployment, but the shared OpenAI settings resolver readsAZURE_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
load_openai_service_settings, resolve the Azure model as explicitmodel, then the injectedAsyncAzureOpenAIdeployment, then the environment aliases. Document that order and add regression tests for the three public clients.modelis omitted. An explicitmodelstill wins. A plainAsyncOpenAIclient still uses the OpenAI model variables. Creating a client without an injected client is unchanged.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-implementationlabel; no competing open PR was found before submission.AI Assistance
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
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.