fix(azure): preserve deployment routing across copy/with_options - #3593
Open
hsusul wants to merge 1 commit into
Open
fix(azure): preserve deployment routing across copy/with_options#3593hsusul wants to merge 1 commit into
hsusul wants to merge 1 commit into
Conversation
AzureOpenAI.copy()/with_options() reconstructs the client from base_url via the base OpenAI.copy(), which does not pass azure_endpoint or azure_deployment. As a result _azure_endpoint and _azure_deployment were reset to None on the copy, so _prepare_url() no longer bypassed the deployment path for non-deployment endpoints. A client built with azure_deployment would then route e.g. /models to /openai/deployments/<deployment>/models instead of /openai/models, producing 404s after a copy. Preserve _azure_endpoint/_azure_deployment on the copied client unless the caller overrides base_url.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes being requested
Fixes a routing regression in
AzureOpenAI/AsyncAzureOpenAIafter.copy()/.with_options().This change is in hand-maintained code —
src/openai/lib/azure.pyhas noFile generated from our OpenAPI spec by Stainlessheader, andcopy()/with_options()there is custom Azure logic, so it is not affected by codegen.Problem
AzureOpenAI.copy()(aliased aswith_options()) delegates to the baseOpenAI.copy(), which reconstructs the client frombase_urland does not passazure_endpoint/azure_deploymentback toAzureOpenAI.__init__. Because_azure_endpointand_azure_deploymentare only set from those constructor arguments, they get reset toNoneon the copied client._prepare_url()uses those attributes to bypass the deployment path for non-deployment endpoints:Once they are
None, that bypass no longer runs, so a client configured withazure_deploymentstarts routing non-deployment endpoints (e.g./models) under the deployment path, which 404s.Reproduction (no network / key required)
Fix
Preserve
_azure_endpoint/_azure_deploymenton the copied client, unless the caller overridesbase_urlin the copy (in which case the old endpoint context is intentionally not carried over). Applied symmetrically to the sync and async clients.Why minimal
copy()in each of the two Azure clients changes; two guarded lines each.base_url/azure_endpointconstructor contract are untouched (the fix deliberately avoids passingazure_endpointalongsidebase_url)./chat/completions) are unaffected —base_urlalready encodes the deployment and_build_requestcontinues to guard on"/deployments" in base_url.path.Tests
Added
test_copy_preserves_deployment_routingintests/lib/test_azure.py(sync + async ×copy/with_options), asserting that after a copy:/models→{endpoint}/openai/models(not nested under/deployments/<name>/), and/chat/completionsstill keeps the deployment path.Fails on
main(wrong/modelsURL), passes with this change.Validation
rye run pytest tests/lib/test_azure.py→ 63 passedrye run pytest tests/lib/→ all pass except a pre-existing, unrelated failure (test_bedrock_auth_conformance.py::test_retry_signing_fixture, which fails identically on cleanmainin this environment)ruff check/ruff formatclean on both filespyrightandmypyclean onsrc/openai/lib/azure.pyCompatibility
No public API change; behavior only changes for the previously-broken post-copy case. Copies that override
base_urlkeep their current behavior.Additional context & links
Discovered by auditing sibling copy/state-preservation logic; no existing issue or PR covers this.
.with_options()is a common pattern (per-request timeouts/headers), so Azure users combining it withazure_deploymentare likely to hit this.