Skip to content

Commit fa7175a

Browse files
committed
Default Microsoft Anthropic requests when the endpoint override is blank
1 parent 7f07a23 commit fa7175a

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎agent-microsoft/src/main.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ def _client() -> AnthropicClient | OpenAIChatClient:
1515
"""The provider the model screen chose, through Agent Framework's own client for it.
1616
1717
`BOT_PROVIDER` is `anthropic` for an Anthropic key and `openai` otherwise, an OpenAI-compatible
18-
endpoint included. Each client reads its own key and base URL from the environment.
18+
endpoint included. Each client reads its own key from the environment.
1919
"""
2020
provider = (os.environ.get("BOT_PROVIDER") or "openai").strip()
2121
model = (os.environ.get("BOT_MODEL") or "gpt-4o-mini").strip()
2222
if provider == "anthropic":
23-
return AnthropicClient(model=model)
23+
# Compose exports missing overrides as ""; the SDK only defaults an absent URL.
24+
base_url = (os.environ.get("ANTHROPIC_BASE_URL") or "").strip() or "https://api.anthropic.com"
25+
return AnthropicClient(model=model, base_url=base_url)
2426
return OpenAIChatClient(model)
2527

2628

‎agent-microsoft/tests/test_main.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
from pathlib import Path
88

9+
import httpx
910
import pytest
1011
import uvicorn
1112
from fastapi import FastAPI, Request
@@ -178,3 +179,41 @@ def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, c
178179
assert '"RUN_ERROR"' not in response.text
179180
assert "hello" in response.text
180181
assert seen == [expected]
182+
183+
184+
def test_an_anthropic_key_uses_the_official_endpoint_when_compose_sets_a_blank_url(monkeypatch):
185+
seen = []
186+
provider_seen = []
187+
provider_app = _provider_app(provider_seen)
188+
189+
async def respond(transport, request):
190+
seen.append(
191+
(request.url.scheme, request.url.host, request.url.path, request.headers.get("x-api-key"))
192+
)
193+
async with httpx.ASGITransport(app=provider_app) as local_provider:
194+
return await local_provider.handle_async_request(request)
195+
196+
# Keep the real framework and Anthropic clients; replace only the network transport.
197+
monkeypatch.setattr(httpx.AsyncHTTPTransport, "handle_async_request", respond)
198+
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
199+
monkeypatch.setenv("BOT_PROVIDER", "anthropic")
200+
monkeypatch.setenv("BOT_MODEL", "claude-sonnet-4-5")
201+
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key")
202+
monkeypatch.setenv("ANTHROPIC_BASE_URL", "")
203+
monkeypatch.setenv("OPENAI_API_KEY", "")
204+
monkeypatch.setenv("OPENAI_BASE_URL", "")
205+
monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
206+
207+
from src import main
208+
209+
main = importlib.reload(main)
210+
response = TestClient(main.app).post(
211+
"/", json=RUN, headers={"x-openbot-agent-token": TOKEN}
212+
)
213+
214+
assert seen == [("https", "api.anthropic.com", "/v1/messages", "test-key")]
215+
assert provider_seen == [("anthropic", "claude-sonnet-4-5")]
216+
assert response.status_code == 200
217+
assert '"RUN_FINISHED"' in response.text
218+
assert '"RUN_ERROR"' not in response.text
219+
assert "hello" in response.text

0 commit comments

Comments
 (0)