fix(foundry): don't treat an env-derived resource as conflicting with base_url#1743
Open
sean-kim05 wants to merge 1 commit into
Open
fix(foundry): don't treat an env-derived resource as conflicting with base_url#1743sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
… base_url `AnthropicFoundry.copy()` / `with_options()` (and the async equivalents) raised `ValueError: base_url and resource are mutually exclusive` whenever `ANTHROPIC_FOUNDRY_RESOURCE` was set in the environment. `copy()` always forwards `base_url` (derived from the live client) and never forwards `resource`. In `__init__`, `resource` then falls back to `ANTHROPIC_FOUNDRY_RESOURCE`, so with that env var set both `base_url` and `resource` end up non-None and the mutual-exclusion guard fires — even though the caller never asked for a conflicting configuration. Any deployment that configures Foundry purely through env vars and calls the public `copy()` / `with_options()` API hit this. An env-derived `resource` is only a fallback for building `base_url`, so it should not conflict with a `base_url` that was supplied. Track whether `resource` was passed explicitly and only raise on a genuine caller-supplied conflict; a supplied `base_url` otherwise wins over the resource env var. Applied to both the sync and async clients.
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.
Summary
AnthropicFoundry.copy()/with_options()(and their async equivalents) raisewhenever
ANTHROPIC_FOUNDRY_RESOURCEis set in the environment.Root cause
copy()reconstructs the client and always forwardsbase_url(derived from the live client) while never forwardingresource:__init__then falls back to the env var forresource:So when
ANTHROPIC_FOUNDRY_RESOURCEis set, the forwardedbase_urland the env-derivedresourceare both non-None and the guard fires — even though the caller never supplied a conflicting configuration. Any deployment that configures Foundry purely through env vars (ANTHROPIC_FOUNDRY_RESOURCE+ANTHROPIC_FOUNDRY_API_KEY) and then calls the publiccopy()/with_options()API hits this.Reproduction
Fix
An env-derived
resourceis only a fallback for buildingbase_url, so it must not conflict with abase_urlthat was actually supplied. Track whetherresourcewas passed explicitly and only raise on a genuine caller-supplied conflict; otherwise a suppliedbase_urlwins over the resource env var (matching normal explicit-arg-over-env precedence). Applied symmetrically to the sync and async clients.Explicitly passing both
base_urlandresourceis still rejected (and remains a static type error via the existing overloads).Testing
Added to
tests/lib/test_azure.py:test_copy_with_resource_from_environment(sync + async) —copy()/with_options()succeed withANTHROPIC_FOUNDRY_RESOURCEset (fails before this change).test_explicit_base_url_and_resource_conflict— the runtime guard still rejects an explicitbase_url+resourcepair.Verified RED → GREEN. Full
tests/lib/test_azure.pypasses (21 tests);./scripts/lintclean (ruff + pyright + mypy).