Skip to content

Commit 2afaadb

Browse files
authored
Answer on a plain OpenAI key from the AG2 Bot (#661)
Compose writes OPENAI_BASE_URL empty when the model screen chose a plain OpenAI key, and the OpenAI SDK only defaults an absent URL, so the Bot's client was given "" as its address and every run failed. It now falls back to https://api.openai.com/v1 for an empty value, as its Anthropic branch already does for ANTHROPIC_BASE_URL.
1 parent 72bec0c commit 2afaadb

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ Anthropic's OpenAI-compatible endpoint, which may ignore the request for bare JS
8989
reply containing multiple objects fell back to offering every tool; replies with no valid `skills`
9090
list still do.
9191

92+
### The AG2 Bot answers on a plain OpenAI key
93+
94+
Picked with an OpenAI key, the AG2 Bot failed every run. Compose writes `OPENAI_BASE_URL` empty when
95+
the choice is a plain OpenAI key, and the OpenAI SDK only defaults an absent URL, so it was given ""
96+
as the address. The Bot now falls back to `https://api.openai.com/v1` for an empty value, as its
97+
Anthropic branch already did for `ANTHROPIC_BASE_URL`. An OpenAI-compatible endpoint is unchanged.
98+
9299
### Dictate messages and talk to a coworker in a live voice call
93100

94101
Deployments can configure transcription separately from their Bots' models, with a waveform composer

‎agent-ag2/src/main.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def _config() -> AnthropicConfig | OpenAIConfig:
2323
# Compose exports missing overrides as ""; the SDK only defaults an absent URL.
2424
base_url = (os.environ.get("ANTHROPIC_BASE_URL") or "").strip() or "https://api.anthropic.com"
2525
return AnthropicConfig(model=model, base_url=base_url)
26-
return OpenAIConfig(model=model)
26+
# The same "" for OPENAI_BASE_URL, which Compose writes whenever the model screen chose a plain
27+
# OpenAI key. The SDK only defaults an absent URL; given "", every request fails to connect.
28+
base_url = (os.environ.get("OPENAI_BASE_URL") or "").strip() or "https://api.openai.com/v1"
29+
return OpenAIConfig(model=model, base_url=base_url)
2730

2831

2932
agent = Agent(

‎agent-ag2/tests/test_main.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,40 @@ async def respond(transport, request):
229229
assert '"RUN_FINISHED"' in response.text
230230
assert '"RUN_ERROR"' not in response.text
231231
assert "hello" in response.text
232+
233+
234+
def test_an_openai_key_uses_the_official_endpoint_when_compose_sets_a_blank_url(monkeypatch):
235+
# The same "" Compose writes for ANTHROPIC_BASE_URL above, on the other provider: the model screen
236+
# sets only the key for a plain OpenAI key, and the SDK treats "" as an address, not as absent.
237+
seen = []
238+
provider_seen = []
239+
provider_app = _provider_app(provider_seen)
240+
241+
async def respond(transport, request):
242+
seen.append((str(request.url), request.headers.get("authorization")))
243+
async with httpx2.ASGITransport(app=provider_app) as local_provider:
244+
return await local_provider.handle_async_request(request)
245+
246+
# Keep the real AG2 and OpenAI clients; replace only the network transport.
247+
monkeypatch.setattr(httpx2.AsyncHTTPTransport, "handle_async_request", respond)
248+
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
249+
monkeypatch.setenv("BOT_PROVIDER", "openai")
250+
monkeypatch.setenv("BOT_MODEL", "gpt-5.5")
251+
monkeypatch.setenv("OPENAI_API_KEY", "test-key")
252+
monkeypatch.setenv("OPENAI_BASE_URL", "")
253+
monkeypatch.setenv("ANTHROPIC_API_KEY", "")
254+
monkeypatch.setenv("ANTHROPIC_BASE_URL", "")
255+
256+
from src import main
257+
258+
main = importlib.reload(main)
259+
response = TestClient(main.app).post(
260+
"/", json=RUN, headers={"x-openbot-agent-token": TOKEN}
261+
)
262+
263+
assert seen == [("https://api.openai.com/v1/chat/completions", "Bearer test-key")]
264+
assert provider_seen == [("openai", "gpt-5.5")]
265+
assert response.status_code == 200
266+
assert '"RUN_FINISHED"' in response.text
267+
assert '"RUN_ERROR"' not in response.text
268+
assert "hello" in response.text

0 commit comments

Comments
 (0)