Skip to content

Commit e547c2b

Browse files
committed
Default AG2 Anthropic requests when the endpoint override is blank
1 parent 6ef2968 commit e547c2b

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎agent-ag2/src/main.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ def _config() -> AnthropicConfig | OpenAIConfig:
1515
"""The provider the model screen chose, which AG2 reaches through a config of its own.
1616
1717
`BOT_PROVIDER` is `anthropic` for an Anthropic key and `openai` otherwise, an OpenAI-compatible
18-
endpoint included. Each SDK reads its own key and base URL from the environment.
18+
endpoint included. Each SDK 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 AnthropicConfig(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 AnthropicConfig(model=model, base_url=base_url)
2426
return OpenAIConfig(model=model)
2527

2628

‎agent-ag2/tests/test_main.py‎

Lines changed: 47 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 httpx2
910
import pytest
1011
import uvicorn
1112
from fastapi import FastAPI, Request
@@ -182,3 +183,49 @@ def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, c
182183
assert '"RUN_FINISHED"' in response.text
183184
assert '"RUN_ERROR"' not in response.text
184185
assert seen == [expected]
186+
187+
188+
def test_an_anthropic_key_uses_the_official_endpoint_when_compose_sets_a_blank_url(monkeypatch):
189+
seen = []
190+
191+
async def respond(transport, request):
192+
body = json.loads(request.content)
193+
seen.append((str(request.url), request.headers.get("x-api-key"), body["model"]))
194+
return httpx2.Response(
195+
200,
196+
request=request,
197+
json={
198+
"id": "msg",
199+
"type": "message",
200+
"role": "assistant",
201+
"model": body["model"],
202+
"content": [{"type": "text", "text": "hello"}],
203+
"stop_reason": "end_turn",
204+
"stop_sequence": None,
205+
"usage": {"input_tokens": 1, "output_tokens": 1},
206+
},
207+
)
208+
209+
# Keep the real AG2 and Anthropic clients; replace only the network transport.
210+
monkeypatch.setattr(httpx2.AsyncHTTPTransport, "handle_async_request", respond)
211+
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
212+
monkeypatch.setenv("BOT_PROVIDER", "anthropic")
213+
monkeypatch.setenv("BOT_MODEL", "claude-sonnet-4-5")
214+
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-key")
215+
monkeypatch.setenv("ANTHROPIC_BASE_URL", "")
216+
monkeypatch.setenv("OPENAI_API_KEY", "")
217+
monkeypatch.setenv("OPENAI_BASE_URL", "")
218+
monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
219+
220+
from src import main
221+
222+
main = importlib.reload(main)
223+
response = TestClient(main.app).post(
224+
"/", json=RUN, headers={"x-openbot-agent-token": TOKEN}
225+
)
226+
227+
assert seen == [("https://api.anthropic.com/v1/messages", "test-key", "claude-sonnet-4-5")]
228+
assert response.status_code == 200
229+
assert '"RUN_FINISHED"' in response.text
230+
assert '"RUN_ERROR"' not in response.text
231+
assert "hello" in response.text

0 commit comments

Comments
 (0)