diff --git a/agent-ag2/requirements.txt b/agent-ag2/requirements.txt index 4b39fa289a..38e0aab92e 100644 --- a/agent-ag2/requirements.txt +++ b/agent-ag2/requirements.txt @@ -1,4 +1,6 @@ -ag2[ag-ui,anthropic,openai] +# 1.3.2 is the first release this harness is checked on with both OpenAICompletionsConfig and +# OpenAIResponsesConfig, which src/main.py names explicitly. +ag2[ag-ui,anthropic,openai]>=1.3.2 fastapi python-multipart uvicorn[standard] diff --git a/agent-ag2/src/main.py b/agent-ag2/src/main.py index 455dcbf1e8..6e0c181d26 100644 --- a/agent-ag2/src/main.py +++ b/agent-ag2/src/main.py @@ -8,22 +8,28 @@ # The spec file every language in the box reads: one level above this Bot in the repository, and # one level above /app/src in the image the Dockerfile builds. sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared")) -from model_providers import bot_settings +from model_providers import bot_settings, openai_api from ag2 import Agent from ag2.ag_ui import AGUIStream -from ag2.config import AnthropicConfig, OpenAIConfig +from ag2.config import AnthropicConfig, OpenAICompletionsConfig, OpenAIResponsesConfig from fastapi import FastAPI, Request from fastapi.responses import JSONResponse TOKEN_HEADER = "x-openbot-agent-token" -def _config() -> AnthropicConfig | OpenAIConfig: +def _config() -> AnthropicConfig | OpenAICompletionsConfig | OpenAIResponsesConfig: """The provider the model screen chose, which AG2 reaches through a config of its own. `BOT_PROVIDER` is `anthropic` for an Anthropic key and `openai` otherwise, an OpenAI-compatible endpoint included. Each SDK reads its own key from the environment. + + An OpenAI model takes AG2's Responses config when it takes function tools only there + (`gpt-5.6` and later, the `-pro` models: "To use function tools, use /v1/responses"), and the + Chat Completions config otherwise, which every OpenAI-compatible endpoint serves. Both are + named explicitly: AG2 1.4.0 turns the bare `OpenAIConfig` into the Responses config, which + would move every compatible endpoint onto an API most of them do not serve. """ settings = bot_settings("agent-ag2") provider = settings.provider @@ -35,7 +41,9 @@ def _config() -> AnthropicConfig | OpenAIConfig: # The same "" for OPENAI_BASE_URL, which Compose writes whenever the model screen chose a plain # OpenAI key. The SDK only defaults an absent URL; given "", every request fails to connect. base_url = (os.environ.get("OPENAI_BASE_URL") or "").strip() or "https://api.openai.com/v1" - return OpenAIConfig(model=model, base_url=base_url) + if openai_api(model) == "responses": + return OpenAIResponsesConfig(model=model, base_url=base_url) + return OpenAICompletionsConfig(model=model, base_url=base_url) agent = Agent( diff --git a/agent-ag2/tests/test_responses_api.py b/agent-ag2/tests/test_responses_api.py new file mode 100644 index 0000000000..b991ee2a89 --- /dev/null +++ b/agent-ag2/tests/test_responses_api.py @@ -0,0 +1,297 @@ +"""OpenAI models that take function tools only on the Responses API get it, tools and results alike. + +OpenAI's own words for `gpt-5.6` and later on Chat Completions: "Function tools with +reasoning_effort are not supported for this model in /v1/chat/completions. To use function tools, +use /v1/responses". The `-pro` models are not served there at all: "This is not a chat model and +thus not supported in the v1/chat/completions endpoint." The real AG2 and OpenAI clients run here; +only the provider at the other end of OPENAI_BASE_URL is local, and it answers the way OpenAI +does, refusals included. +""" + +import importlib +import json +import socket +import sys +import threading +import time +from pathlib import Path +from uuid import uuid4 + +import pytest +import uvicorn +from fastapi import FastAPI, Request +from fastapi.responses import JSONResponse +from fastapi.testclient import TestClient + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +TOKEN = "responses-token" +TOOL = { + "name": "look_up_order", + "description": "Look up the status of an order by its number.", + "parameters": { + "type": "object", + "properties": {"order": {"type": "string", "description": "The order number."}}, + "required": ["order"], + }, +} +ARGUMENTS = {"order": "A-1001"} + +# What the picker lists that OpenAI serves function tools for only on /v1/responses. +RESPONSES_MODELS = ["gpt-5.6-luna", "gpt-6-sol", "gpt-6.1-terra", "gpt-6.2-astra", "gpt-5.5-pro", "o3-pro"] +# Reasoning and non-reasoning models that still take tools on Chat Completions. +CHAT_MODELS = ["gpt-5.5", "o3", "gpt-4.1-mini"] +REASONING = {"gpt-5.5", "o3", *RESPONSES_MODELS} + + +def _provider_app(seen): + app = FastAPI() + + def refuse(message, status=400): + return JSONResponse( + {"error": {"message": message, "type": "invalid_request_error", "param": None, "code": None}}, + status_code=status, + ) + + @app.post("/v1/chat/completions") + async def chat(request: Request): + body = await request.json() + seen.append(("/v1/chat/completions", body)) + model = body["model"] + if model.endswith("-pro"): + return refuse("This is not a chat model and thus not supported in the v1/chat/completions endpoint.", 404) + if model in RESPONSES_MODELS and body.get("tools"): + return refuse( + f"Function tools with reasoning_effort are not supported for {model} in " + "/v1/chat/completions. To use function tools, use /v1/responses or set " + "reasoning_effort to 'none'." + ) + if model in REASONING and "temperature" in body: + return refuse("Unsupported value: 'temperature' does not support 0.2 with this model.") + if model == "o3" and "parallel_tool_calls" in body: + return refuse("Unsupported parameter: 'parallel_tool_calls' is not supported with this model.") + # AG2 follows a tool result with an empty user turn on this API, so look for the result. + results = [m for m in body["messages"] if m["role"] == "tool"] + if results: + message = {"role": "assistant", "content": "Observed: " + results[-1]["content"]} + finish = "stop" + else: + message = { + "role": "assistant", + "content": None, + "tool_calls": [ + { + "id": "call-chat", + "type": "function", + "function": {"name": TOOL["name"], "arguments": json.dumps(ARGUMENTS)}, + } + ], + } + finish = "tool_calls" + return JSONResponse( + { + "id": "chat", + "object": "chat.completion", + "created": 0, + "model": model, + "choices": [{"index": 0, "message": message, "finish_reason": finish}], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + } + ) + + @app.post("/v1/responses") + async def responses(request: Request): + body = await request.json() + seen.append(("/v1/responses", body)) + if body["model"] in REASONING and "temperature" in body: + return refuse("Unsupported parameter: 'temperature' is not supported with this model.") + results = [item for item in body["input"] if item.get("type") == "function_call_output"] + if results: + output = [ + { + "id": "msg", + "type": "message", + "role": "assistant", + "status": "completed", + "content": [ + {"type": "output_text", "text": "Observed: " + results[-1]["output"], "annotations": []} + ], + } + ] + else: + output = [ + { + "id": "fc", + "type": "function_call", + "status": "completed", + "call_id": "call-responses", + "name": body["tools"][0]["name"], + "arguments": json.dumps(ARGUMENTS), + } + ] + return JSONResponse( + { + "id": "resp-" + uuid4().hex, + "object": "response", + "created_at": 0, + "model": body["model"], + "status": "completed", + "output": output, + "parallel_tool_calls": body.get("parallel_tool_calls", True), + "tool_choice": "auto", + "tools": body.get("tools", []), + # The usage OpenAI reports, details included: AG2 reads them. + "usage": { + "input_tokens": 1, + "input_tokens_details": {"cached_tokens": 0, "cache_write_tokens": 0}, + "output_tokens": 1, + "output_tokens_details": {"reasoning_tokens": 0}, + "total_tokens": 2, + }, + } + ) + + return app + + +@pytest.fixture +def provider(): + seen = [] + with socket.socket() as probe: + probe.bind(("127.0.0.1", 0)) + port = probe.getsockname()[1] + server = uvicorn.Server(uvicorn.Config(_provider_app(seen), host="127.0.0.1", port=port, log_level="error")) + thread = threading.Thread(target=server.run, daemon=True) + thread.start() + deadline = time.monotonic() + 10 + while not server.started and time.monotonic() < deadline: + time.sleep(0.01) + yield f"http://127.0.0.1:{port}", seen + server.should_exit = True + thread.join(timeout=10) + + +def _app(monkeypatch, base, model, api=None): + monkeypatch.delenv("OPENAI_API_BASE", raising=False) + if api is None: + monkeypatch.delenv("COPILOTKIT_OPENAI_API", raising=False) + else: + monkeypatch.setenv("COPILOTKIT_OPENAI_API", api) + monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN) + monkeypatch.setenv("BOT_PROVIDER", "openai") + monkeypatch.setenv("BOT_MODEL", model) + monkeypatch.setenv("OPENAI_API_KEY", "test-key") + monkeypatch.setenv("OPENAI_BASE_URL", f"{base}/v1") + monkeypatch.setenv("ANTHROPIC_API_KEY", "") + from src import main + + return TestClient(importlib.reload(main).app) + + +def _body(messages, thread): + return { + "threadId": thread, + "runId": str(uuid4()), + "state": {}, + "messages": messages, + "tools": [TOOL], + "context": [], + "forwardedProps": {}, + } + + +def _run(client, body): + response = client.post("/", json=body, headers={"x-openbot-agent-token": TOKEN}) + assert response.status_code == 200 + events = [json.loads(line[5:]) for line in response.text.splitlines() if line.startswith("data:")] + assert not [e for e in events if e["type"] == "RUN_ERROR"], events + assert events[-1]["type"] == "RUN_FINISHED", events + return events + + +def _tool_call_then_result(client): + """One run that calls the tool, then the run the browser sends back with the tool's result.""" + thread = str(uuid4()) + question = [{"id": "u1", "role": "user", "content": "Where is order A-1001?"}] + events = _run(client, _body(question, thread)) + # AG2 sends a call as AG-UI's TOOL_CALL_CHUNK, which a client expands to start, args and end. + start = next((e for e in events if e["type"] in ("TOOL_CALL_START", "TOOL_CALL_CHUNK")), None) + assert start is not None, events + arguments = json.loads( + "".join(e.get("delta") or "" for e in events if e["type"] in ("TOOL_CALL_ARGS", "TOOL_CALL_CHUNK")) + ) + assert start["toolCallName"] == TOOL["name"] + assert arguments == ARGUMENTS + assert any(e["type"] == "RUN_FINISHED" and start["toolCallId"] in json.dumps(e) for e in events) + result = "shipped-" + uuid4().hex + history = [ + *question, + { + "id": start.get("parentMessageId") or "a1", + "role": "assistant", + "toolCalls": [ + { + "id": start["toolCallId"], + "type": "function", + "function": {"name": TOOL["name"], "arguments": json.dumps(arguments)}, + } + ], + }, + {"id": "t1", "role": "tool", "toolCallId": start["toolCallId"], "content": result}, + ] + events = _run(client, _body(history, thread)) + text = "".join(e.get("delta", "") for e in events if e["type"] in ("TEXT_MESSAGE_CONTENT", "TEXT_MESSAGE_CHUNK")) + assert "Observed: " + result in text + return start, result + + +@pytest.mark.parametrize("model", RESPONSES_MODELS) +def test_a_responses_only_model_calls_tools_and_reads_results_through_the_responses_api( + monkeypatch, provider, model +): + base, seen = provider + client = _app(monkeypatch, base, model) + + start, result = _tool_call_then_result(client) + + assert [path for path, _ in seen] == ["/v1/responses", "/v1/responses"] + first, second = (body for _, body in seen) + assert first["model"] == model + tool = next(t for t in first["tools"] if t["name"] == TOOL["name"]) + assert tool["type"] == "function" + assert tool["parameters"]["properties"] == TOOL["parameters"]["properties"] + assert "temperature" not in first + assert { + "type": "function_call_output", + "call_id": start["toolCallId"], + "output": result, + }.items() <= next(i for i in second["input"] if i.get("type") == "function_call_output").items() + + +@pytest.mark.parametrize("model", CHAT_MODELS) +def test_a_model_that_takes_tools_on_chat_completions_stays_there_without_refused_fields( + monkeypatch, provider, model +): + base, seen = provider + client = _app(monkeypatch, base, model) + + _tool_call_then_result(client) + + assert [path for path, _ in seen] == ["/v1/chat/completions", "/v1/chat/completions"] + for _, body in seen: + assert "temperature" not in body + if model == "o3": + assert "parallel_tool_calls" not in body + + +@pytest.mark.parametrize(("api", "model", "path"), [ + ("chat", "gpt-5.5", "/v1/chat/completions"), + ("responses", "gpt-4.1-mini", "/v1/responses"), +]) +def test_an_operator_choice_of_openai_api_wins(monkeypatch, provider, api, model, path): + base, seen = provider + client = _app(monkeypatch, base, model, api=api) + + _tool_call_then_result(client) + + assert [p for p, _ in seen] == [path, path] diff --git a/agent-langroid/requirements.txt b/agent-langroid/requirements.txt index 309b26d7b1..6b303632b7 100644 --- a/agent-langroid/requirements.txt +++ b/agent-langroid/requirements.txt @@ -9,4 +9,9 @@ uvicorn[standard] # 'mcp.types'` where an older mcp is pinned by another dependency, and `No module named 'mcp'` # where there is none at all. 1.105.1 does not reach for it. Lift this when litellm stops # requiring mcp, or declares it. -litellm<1.106.0 +# +# 1.105.7 is the release this harness is checked on for OpenAI's newer models: its model map knows +# which fields each one refuses (`temperature` on gpt-5.5, `parallel_tool_calls` on o3), and its +# `responses/` route carries a chat with tools to /v1/responses for gpt-5.6 and later and the -pro +# models. src/main.py relies on both. +litellm>=1.105.7,<1.106.0 diff --git a/agent-langroid/src/main.py b/agent-langroid/src/main.py index bc6d1638c0..72c1c168ed 100644 --- a/agent-langroid/src/main.py +++ b/agent-langroid/src/main.py @@ -8,8 +8,9 @@ # The spec file every language in the box reads: one level above this Bot in the repository, and # one level above /app/src in the image the Dockerfile builds. sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared")) -from model_providers import bot_settings +from model_providers import bot_settings, openai_api +import litellm from ag_ui_langroid import create_langroid_app from fastapi import Request from fastapi.responses import JSONResponse @@ -21,18 +22,47 @@ TOKEN_HEADER = "x-openbot-agent-token" +def _litellm_knows(model: str) -> bool: + """Whether litellm's model map has this OpenAI model, and so knows what its requests may carry.""" + try: + litellm.get_model_info(model=model, custom_llm_provider="openai") + except Exception: + return False + return True + + def _model_id() -> str: - """Langroid names an OpenAI model bare and everything else through litellm. + """The model id Langroid is given: through litellm, or bare to Langroid's own OpenAI client. + + Every provider but OpenAI goes through litellm, under its provider's prefix. + + An OpenAI model goes through litellm too when litellm knows it. Langroid's own client sends + `temperature` to every model its table does not list, and its table stops at `gpt-5.2`, so + `gpt-5.5` was refused ("'temperature' does not support 0.2 with this model"), and it sends + `parallel_tool_calls` whatever the model, which `o3` refuses. litellm reads each model's row and + leaves out what the model does not take (Langroid turns on litellm's `drop_params` for that). + + A model that takes function tools only on the Responses API (`gpt-5.6` and later, the `-pro` + models) gets litellm's `responses/` route, which sends the same chat to `/v1/responses` and + hands the answer back as a chat completion. That is decided by the model, as every Bot in the box + decides it, so a gateway in front of OpenAI gets the Responses API for those models too. - `openai/gpt-4o-mini` is rejected by its OpenAI client as an invalid model id, so the prefix goes - on only when the provider is somebody else. + A model litellm does not know (one served by an OpenAI-compatible endpoint on someone's own + hardware) stays bare, on Langroid's own OpenAI client, which needs no key for it. + `openai/gpt-4o-mini` would be rejected by that client as an invalid model id. """ settings = bot_settings("agent-langroid") provider = settings.provider model = settings.model - if "/" in model or provider == "openai": + if provider != "openai": + return model if "/" in model else f"litellm/{provider}/{model}" + if model.startswith("litellm/"): return model - return f"litellm/{provider}/{model}" + if openai_api(model) == "responses": + return f"litellm/openai/responses/{model}" + if _litellm_knows(model): + return f"litellm/openai/{model}" + return model # An empty `OPENAI_API_KEY` is no key. Compose writes the keys a model choice does not use as empty diff --git a/agent-langroid/tests/test_responses_api.py b/agent-langroid/tests/test_responses_api.py new file mode 100644 index 0000000000..2fe7558132 --- /dev/null +++ b/agent-langroid/tests/test_responses_api.py @@ -0,0 +1,278 @@ +"""Every OpenAI model the picker lists takes this Bot's tools, in the request shape it accepts. + +OpenAI's own words, as its API answered this Bot before: + +- `gpt-5.6` and later on Chat Completions: "Function tools with reasoning_effort are not supported + for this model in /v1/chat/completions. To use function tools, use /v1/responses". +- The `-pro` models: "This is not a chat model and thus not supported in the v1/chat/completions + endpoint." +- `gpt-5.5`: "Unsupported value: 'temperature' does not support 0.2 with this model." +- `o3`: "Unsupported parameter: 'parallel_tool_calls' is not supported with this model." + +The real Langroid, litellm and AG-UI stack runs here; only the provider at OPENAI_BASE_URL is local, +and it refuses what OpenAI refuses, in OpenAI's words. +""" + +import importlib +import json +import sys +import threading +from copy import deepcopy +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from uuid import uuid4 + +import pytest +from fastapi.testclient import TestClient + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +TOOL = { + "name": "look_up_order", + "description": "Look up the status of an order by its number.", + "parameters": { + "type": "object", + "properties": {"order": {"type": "string", "description": "The order number."}}, + "required": ["order"], + }, +} +ARGUMENTS = {"order": "A-1001"} + +RESPONSES_MODELS = ["gpt-5.6-luna", "gpt-6-sol", "gpt-6.1-terra", "gpt-6.2-astra", "gpt-5.5-pro", "o3-pro"] +CHAT_MODELS = ["gpt-5.5", "o3", "gpt-4.1-mini"] +REASONING = {"gpt-5.5", "o3", *RESPONSES_MODELS} + + +def _refusal(model, path, body): + """OpenAI's refusal for this request, or None when OpenAI takes it.""" + if path == "/v1/chat/completions": + if model.endswith("-pro"): + return 404, "This is not a chat model and thus not supported in the v1/chat/completions endpoint." + if model in RESPONSES_MODELS and body.get("tools"): + return 400, ( + f"Function tools with reasoning_effort are not supported for {model} in " + "/v1/chat/completions. To use function tools, use /v1/responses or set " + "reasoning_effort to 'none'." + ) + if model in REASONING and "temperature" in body: + return 400, f"Unsupported value: 'temperature' does not support {body['temperature']} with this model." + if model == "o3" and "parallel_tool_calls" in body: + return 400, "Unsupported parameter: 'parallel_tool_calls' is not supported with this model." + return None + + +def _chat(body): + results = [m for m in body["messages"] if m["role"] == "tool"] + if results: + message, finish = {"role": "assistant", "content": "Observed: " + results[-1]["content"]}, "stop" + else: + message = { + "role": "assistant", + "tool_calls": [{"id": "call-chat", "type": "function", "function": { + "name": body["tools"][0]["function"]["name"], "arguments": json.dumps(ARGUMENTS), + }}], + } + finish = "tool_calls" + base = {"id": "chat", "created": 0, "model": body["model"]} + if not body.get("stream"): + return "application/json", json.dumps({ + **base, "object": "chat.completion", + "choices": [{"index": 0, "message": message, "finish_reason": finish}], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + }) + delta = deepcopy(message) + for call in delta.get("tool_calls", []): + call["index"] = 0 + chunks = [ + {**base, "object": "chat.completion.chunk", "choices": [{"index": 0, "delta": delta, "finish_reason": None}]}, + {**base, "object": "chat.completion.chunk", "choices": [{"index": 0, "delta": {}, "finish_reason": finish}]}, + ] + return "text/event-stream", "".join("data: " + json.dumps(c) + "\n\n" for c in chunks) + "data: [DONE]\n\n" + + +def _responses(body): + results = [i for i in body["input"] if i.get("type") == "function_call_output"] + if results: + # A result is a string or a list of content parts; OpenAI takes both. + output = results[-1]["output"] + if isinstance(output, list): + output = "".join(part.get("text", "") for part in output) + item = {"id": "msg", "type": "message", "role": "assistant", "status": "completed", + "content": [{"type": "output_text", "text": "Observed: " + output, "annotations": []}]} + else: + item = {"id": "fc", "type": "function_call", "status": "completed", "call_id": "call-responses", + "name": body["tools"][0]["name"], "arguments": json.dumps(ARGUMENTS)} + response = { + "id": "resp-" + uuid4().hex, "object": "response", "created_at": 0, "model": body["model"], + "status": "completed", "output": [item], "parallel_tool_calls": body.get("parallel_tool_calls", True), + "tool_choice": "auto", "tools": body.get("tools", []), + "usage": {"input_tokens": 1, "input_tokens_details": {"cached_tokens": 0}, "output_tokens": 1, + "output_tokens_details": {"reasoning_tokens": 0}, "total_tokens": 2}, + } + if not body.get("stream"): + return "application/json", json.dumps(response) + if item["type"] == "message": + text = item["content"][0]["text"] + events = [ + {"type": "response.output_item.added", "output_index": 0, "item": {**item, "status": "in_progress", "content": []}}, + {"type": "response.content_part.added", "output_index": 0, "content_index": 0, "item_id": "msg", + "part": {"type": "output_text", "text": "", "annotations": []}}, + {"type": "response.output_text.delta", "output_index": 0, "content_index": 0, "item_id": "msg", "delta": text}, + {"type": "response.output_text.done", "output_index": 0, "content_index": 0, "item_id": "msg", "text": text}, + {"type": "response.content_part.done", "output_index": 0, "content_index": 0, "item_id": "msg", + "part": item["content"][0]}, + ] + else: + events = [ + {"type": "response.output_item.added", "output_index": 0, "item": {**item, "status": "in_progress", "arguments": ""}}, + {"type": "response.function_call_arguments.delta", "output_index": 0, "item_id": "fc", "delta": item["arguments"]}, + {"type": "response.function_call_arguments.done", "output_index": 0, "item_id": "fc", "arguments": item["arguments"]}, + ] + events = [ + {"type": "response.created", "response": {**response, "status": "in_progress", "output": []}}, + *events, + {"type": "response.output_item.done", "output_index": 0, "item": item}, + {"type": "response.completed", "response": response}, + ] + return "text/event-stream", "".join( + f"event: {e['type']}\ndata: {json.dumps({**e, 'sequence_number': n})}\n\n" for n, e in enumerate(events) + ) + + +@pytest.fixture +def provider(monkeypatch): + seen = [] + + class Provider(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def do_POST(self): + body = json.loads(self.rfile.read(int(self.headers["content-length"]))) + seen.append((self.path, body)) + refused = _refusal(body["model"], self.path, body) + if refused: + status, message = refused + content_type, data = "application/json", json.dumps( + {"error": {"message": message, "type": "invalid_request_error", "param": None, "code": None}} + ) + else: + status = 200 + content_type, data = (_responses if self.path == "/v1/responses" else _chat)(body) + data = data.encode() + self.send_response(status) + self.send_header("Content-Type", content_type) + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + + server = ThreadingHTTPServer(("127.0.0.1", 0), Provider) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + monkeypatch.setenv("OPENAI_API_KEY", "synthetic-key") + monkeypatch.setenv("BOT_PROVIDER", "openai") + monkeypatch.setenv("OPENAI_USE_CACHED_CLIENT", "false") + monkeypatch.setenv("OPENAI_BASE_URL", f"http://127.0.0.1:{server.server_port}/v1") + monkeypatch.setenv("MANAGED_AGENT_TOKEN", "test-token") + monkeypatch.delenv("OPENAI_API_BASE", raising=False) + monkeypatch.delenv("COPILOTKIT_OPENAI_API", raising=False) + try: + yield seen + finally: + server.shutdown() + server.server_close() + thread.join(timeout=5) + + +def _client(monkeypatch, model): + monkeypatch.setenv("BOT_MODEL", model) + from src import main + + return TestClient(importlib.reload(main).app) + + +def _body(messages, thread): + return {"threadId": thread, "runId": str(uuid4()), "state": {}, "messages": messages, + "tools": [deepcopy(TOOL)], "context": [], "forwardedProps": {}} + + +def _run(client, body): + response = client.post("/", json=body, headers={"x-openbot-agent-token": "test-token"}) + assert response.status_code == 200 + events = [json.loads(line[5:]) for line in response.text.splitlines() if line.startswith("data:")] + assert not [e for e in events if e["type"] == "RUN_ERROR"], events + assert events[-1]["type"] == "RUN_FINISHED", events + return events + + +def _tool_call_then_result(client): + """A run that streams a tool call, then the run the browser sends back with its result.""" + thread = str(uuid4()) + question = [{"id": "u1", "role": "user", "content": "Where is order A-1001?"}] + events = _run(client, _body(question, thread)) + start = next((e for e in events if e["type"] == "TOOL_CALL_START"), None) + assert start is not None, events + assert start["toolCallName"] == TOOL["name"] + assert json.loads("".join(e["delta"] for e in events if e["type"] == "TOOL_CALL_ARGS")) == ARGUMENTS + assert any(e["type"] == "TOOL_CALL_END" for e in events) + result = "shipped-" + uuid4().hex + history = [*question, { + "id": start["parentMessageId"], "role": "assistant", "toolCalls": [{ + "id": start["toolCallId"], "type": "function", + "function": {"name": TOOL["name"], "arguments": json.dumps(ARGUMENTS)}, + }], + }, {"id": "t1", "role": "tool", "toolCallId": start["toolCallId"], "content": result}] + events = _run(client, _body(history, thread)) + text = "".join(e.get("delta", "") for e in events if e["type"] == "TEXT_MESSAGE_CONTENT") + assert "Observed: " + result in text + return start, result + + +@pytest.mark.parametrize("model", RESPONSES_MODELS) +def test_a_responses_only_model_calls_tools_and_reads_results_through_the_responses_api( + monkeypatch, provider, model +): + client = _client(monkeypatch, model) + + start, result = _tool_call_then_result(client) + + assert [path for path, _ in provider] == ["/v1/responses", "/v1/responses"] + first, second = (body for _, body in provider) + assert first["model"] == model + tool = next(t for t in first["tools"] if t["name"] == TOOL["name"]) + assert tool["type"] == "function" + assert tool["parameters"]["properties"] == TOOL["parameters"]["properties"] + assert "temperature" not in first + output = next(i for i in second["input"] if i.get("type") == "function_call_output") + assert output["call_id"] == start["toolCallId"] + assert result in json.dumps(output["output"]) + + +@pytest.mark.parametrize("model", CHAT_MODELS) +def test_a_model_that_takes_tools_on_chat_completions_gets_only_the_fields_it_accepts( + monkeypatch, provider, model +): + client = _client(monkeypatch, model) + + _tool_call_then_result(client) + + assert [path for path, _ in provider] == ["/v1/chat/completions", "/v1/chat/completions"] + for _, body in provider: + if model in REASONING: + assert "temperature" not in body + if model == "o3": + assert "parallel_tool_calls" not in body + else: + # One call per turn, as Langroid's AG-UI bridge hands calls back one at a time. + assert body["parallel_tool_calls"] is False + + +def test_a_model_litellm_does_not_know_stays_on_langroids_own_client(monkeypatch, provider): + # A model on someone's own hardware, behind an OpenAI-compatible endpoint, and with no key. + monkeypatch.delenv("OPENAI_API_KEY", raising=False) + client = _client(monkeypatch, "qwen3-8b-local") + + _tool_call_then_result(client) + + assert [path for path, _ in provider] == ["/v1/chat/completions", "/v1/chat/completions"] + assert provider[0][1]["parallel_tool_calls"] is False diff --git a/shared/model_providers.py b/shared/model_providers.py index 7345a0b5fe..6ec21655c7 100644 --- a/shared/model_providers.py +++ b/shared/model_providers.py @@ -29,6 +29,7 @@ import json import os +import re from dataclasses import dataclass from pathlib import Path from typing import Any, Mapping @@ -177,3 +178,34 @@ def bot_settings( ) return BotSettings(provider=provider, model=model) + + +# `requiresResponsesApi` in shared/model-providers.ts, the rule every Bot in the box decides by: +# `gpt-5.6-*` and every `gpt-6*` take function tools only on the Responses API, and the `-pro` and +# `-codex` models are not served on Chat Completions at all. +_RESPONSES_API_MODEL = re.compile( + r"^gpt-5\.[6-9]|^gpt-[6-9]|^(?:gpt-[\d.]+|o\d+)-pro\b|^gpt-[\d.]+-codex\b" +) + + +def requires_responses_api(model: str) -> bool: + """Whether this OpenAI model has to be driven through the Responses API. + + A gateway's namespaced name (`openai/gpt-5.6-terra`) is the model it names. + """ + return bool(_RESPONSES_API_MODEL.search(model.strip().split("/")[-1])) + + +def openai_api(model: str, env: Mapping[str, str] | None = None) -> str: + """`responses` or `chat`: which OpenAI API a Bot calls for this model. + + THE MODEL DECIDES, NOT THE ADDRESS, as `chooseOpenAIApi` on the server does: a gateway or a + proxy in front of OpenAI is an `OPENAI_BASE_URL` too, and the address cannot say which API a + model takes. `COPILOTKIT_OPENAI_API=responses` or `chat` is an operator's choice for every + model, and wins. + """ + environ = os.environ if env is None else env + chosen = (environ.get("COPILOTKIT_OPENAI_API") or "").strip().lower() + if chosen in ("responses", "chat"): + return chosen + return "responses" if requires_responses_api(model) else "chat"