Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Answer on a plain OpenAI key from the Langroid Bot (#678)

Asem ahmed1e4b6105391d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c382f593..fdc2848c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -257,6 +257,14 @@ Anthropic key), and Pydantic AI builds each provider's client from the environme
given "" as the address. The Bot now removes an empty value before building the model, as
`agent-langgraph-agui` already does, so the SDK uses its own endpoint. A real endpoint is unchanged.
+### The Langroid Bot answers on a plain OpenAI key
+
+Picked with an OpenAI key, the Langroid Bot failed every run with "Connection error.". Compose
+writes `OPENAI_BASE_URL` empty when the choice is a plain OpenAI key, and the OpenAI SDK only
+defaults an absent URL, so it was given "" as the address. The Bot now drops an empty
+`OPENAI_BASE_URL` before it builds its client, as it already does for an empty `OPENAI_API_KEY`.
+An OpenAI-compatible endpoint is unchanged.
+
### Find older conversations and keep chat preferences across devices
The sidebar loads older conversations as the person scrolls. Settings save the choice to emphasize
diff --git a/agent-langroid/src/main.py b/agent-langroid/src/main.py
index dd2c2215..bc6d1638 100644
--- a/agent-langroid/src/main.py
+++ b/agent-langroid/src/main.py
@@ -42,6 +42,13 @@ def _model_id() -> str:
if not os.environ.get("OPENAI_API_KEY"):
os.environ.pop("OPENAI_API_KEY", None)
+# And an empty `OPENAI_BASE_URL` is no address. Compose writes it empty when the model screen chose a
+# plain OpenAI key. Langroid builds its OpenAI client with no `base_url`, so the SDK reads this
+# variable itself, and it falls back to `https://api.openai.com/v1` only when the variable is absent:
+# given "", every request was built with no host and failed to connect. A real endpoint is left alone.
+if not os.environ.get("OPENAI_BASE_URL", "").strip():
+ os.environ.pop("OPENAI_BASE_URL", None)
+
agent = ChatAgent(
ChatAgentConfig(
llm=OpenAIGPTConfig(chat_model=_model_id(), parallel_tool_calls=False),
diff --git a/agent-langroid/tests/test_main.py b/agent-langroid/tests/test_main.py
index eb89f386..9f5c4496 100644
--- a/agent-langroid/tests/test_main.py
+++ b/agent-langroid/tests/test_main.py
@@ -6,6 +6,8 @@ import threading
import time
from pathlib import Path
+import httpx
+import httpx2
import pytest
import uvicorn
from fastapi import FastAPI, Request
@@ -182,3 +184,62 @@ def test_a_run_reaches_the_model_the_setup_screen_chose(monkeypatch, provider, c
assert '"RUN_FINISHED"' in response.text
assert '"RUN_ERROR"' not in response.text
assert seen == [expected]
+
+
+def test_an_openai_key_uses_the_official_endpoint_when_compose_sets_a_blank_url(monkeypatch, provider):
+ # The "an OpenAI key" choice above points OPENAI_BASE_URL at the fake provider, which is how the
+ # empty value Compose actually writes for a plain OpenAI key went untested: the OpenAI SDK only
+ # defaults an absent URL, so "" left every request without a host.
+ base, provider_seen = provider
+ port = int(base.rsplit(":", 1)[1])
+ destinations = []
+
+ def reroute(transport_class, method_name):
+ original = getattr(transport_class, method_name)
+
+ if method_name.startswith("handle_async"):
+
+ async def forward(transport, request):
+ destinations.append((request.url.scheme, request.url.host, request.url.path))
+ request.url = request.url.copy_with(scheme="http", host="127.0.0.1", port=port)
+ return await original(transport, request)
+
+ else:
+
+ def forward(transport, request):
+ destinations.append((request.url.scheme, request.url.host, request.url.path))
+ request.url = request.url.copy_with(scheme="http", host="127.0.0.1", port=port)
+ return original(transport, request)
+
+ monkeypatch.setattr(transport_class, method_name, forward)
+
+ # Keep the real Langroid and OpenAI clients and send what they build to the fake provider, noting
+ # where it was addressed. Both HTTP stacks and both client kinds, because the OpenAI SDK sends
+ # through httpx2 rather than httpx and Langroid may use either the sync or the async client.
+ for stack in (httpx, httpx2):
+ reroute(stack.HTTPTransport, "handle_request")
+ reroute(stack.AsyncHTTPTransport, "handle_async_request")
+ monkeypatch.delenv("OPENAI_API_BASE", raising=False)
+ monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
+ monkeypatch.setenv("BOT_PROVIDER", "")
+ monkeypatch.setenv("BOT_MODEL", "gpt-5.5")
+ # A key no other test uses: Langroid caches the OpenAI client it builds by API key, so with the
+ # "test-key" above this test would be handed that test's client, already pointed at the fake.
+ monkeypatch.setenv("OPENAI_API_KEY", "blank-url-test-key")
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ monkeypatch.setenv("ANTHROPIC_API_KEY", "")
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", "")
+
+ from src import main
+
+ main = importlib.reload(main)
+ response = TestClient(main.app).post(
+ "/", json=RUN, headers={"x-openbot-agent-token": TOKEN}
+ )
+
+ assert destinations == [("https", "api.openai.com", "/v1/chat/completions")]
+ assert provider_seen == [("openai", "gpt-5.5")]
+ assert response.status_code == 200
+ assert '"RUN_FINISHED"' in response.text
+ assert '"RUN_ERROR"' not in response.text
+ assert "hello" in response.text