Skip to content

Python: Read AZURE_OPENAI_API_VERSION before applying the Azure client default - #9098

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Aditya-XR:fix/azure-openai-api-version-env
Oct 12, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Aditya-XR:fix/azure-openai-api-version-env

Conversation

@Aditya-XR

Copy link
Copy Markdown
Contributor

Motivation & Context

On the Azure route, OpenAIChatCompletionClient, OpenAIChatClient and OpenAIEmbeddingClient ignore AZURE_OPENAI_API_VERSION from the environment or a .env file and always send their built-in default api-version, although their docstrings and the package README say the variable is read. load_openai_service_settings passed api_version or default_azure_api_version to load_settings, and an explicit value there always beats .env files and environment variables.

Description & Review Guide

  • What are the major changes?

    load_openai_service_settings passes only the explicit api_version to load_settings, then applies the client default when neither the argument, the .env file nor the environment set a version:

    -        api_version=api_version or default_azure_api_version,
    +        api_version=api_version,
             ...
    +    if not azure_settings.get("api_version"):
    +        azure_settings["api_version"] = default_azure_api_version

    New tests in test_openai_shared.py:

    • test_azure_api_version_precedence checks default, environment over default, and explicit over environment, both in the loaded settings and in the api-version query parameter of a built request.
    • test_azure_api_version_from_env_file covers the .env path.
    • test_azure_clients_read_api_version_from_environment runs the three public clients with the variable set to a value that differs from every default.

    The environment and .env cases fail without the change (5 of the 7 new cases) and pass with it.

  • What is the impact of these changes?

    The documented precedence is restored: explicit api_version, then AZURE_OPENAI_API_VERSION, then the client default. For the repro in the linked issue:

    OpenAIChatCompletionClient  api_version=2025-04-01-preview
    OpenAIChatClient            api_version=2025-04-01-preview
    OpenAIEmbeddingClient       api_version=2025-04-01-preview
    

    An explicit api_version and the defaults are unchanged, and nothing changes when the variable is unset.

  • What do you want reviewers to focus on?

    OpenAIChatClient (Responses) uses the /openai/v1/ path with the default preview. With this change, a dated AZURE_OPENAI_API_VERSION set for Chat Completions is also used by the Responses client, which matches its docstring and the behavior before [BREAKING] Python: fix OpenAI Azure routing and provider samples #4925. If the Responses client shouldn't read the shared variable, I can keep it on its default and update its docstring instead.

Related Issue

Fixes #9097

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • 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.

load_openai_service_settings passed api_version or the client default
to load_settings. Explicit values there always beat .env files and
environment variables, so AZURE_OPENAI_API_VERSION was never read and
every Azure request used the hard-coded default. Pass only the explicit
value, and apply the default after loading when nothing set a version.

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 6, 2026
@eavanvalkenburg

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot 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.

MAF Automated Review — Iteration 1

Result: No findings
Scope: full PR (1 commit(s)): 3d13171b52ac
Model: gpt-5.6-sol

Overview

The change restores the documented Azure API-version precedence by allowing explicit .env and process-environment values to resolve before applying each client's default. The shared loader preserves routing behavior, explicit overrides, and client-specific defaults, while tests cover the resolved settings and outgoing request query for all precedence layers. No publishable Critical, High, or Medium defect was established.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.

Merged via the queue into microsoft:main with commit f64e4c0 Oct 12, 2026
53 of 54 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 3d13171b Deployed Oct 6, 2026 by Aditya-XR via team_check #6097
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: AZURE_OPENAI_API_VERSION is ignored by the OpenAI clients on the Azure route

3 participants