From 6468e0e6f66f4f0071dc4b1e22ca16fab7cefcad Mon Sep 17 00:00:00 2001 From: Vineet Vora Date: Mon, 16 Nov 2026 11:40:23 -0500 Subject: [PATCH] ci(tutorials): route the tutorials CI's OpenAI calls through the internal gateway The tutorials CI now uses a scoped gateway key (TUTORIAL_SMIG_API_KEY) with the gateway URL from TUTORIAL_OPENAI_BASE_URL, and disables OpenAI Agents SDK trace export, which ignores OPENAI_BASE_URL. The web search MCP server in the 010, 020, 040 and 050 tutorials now also gets OPENAI_BASE_URL when it is set. --- .github/workflows/agentex-tutorials-test.yml | 8 +++++++- .../10_async/00_base/040_other_sdks/project/acp.py | 10 +++++++++- .../10_temporal/010_agent_chat/project/workflow.py | 10 +++++++++- .../deep_research/performing_deep_research.py | 13 ++++++++++--- .../050_agent_chat_guardrails/project/workflow.py | 10 +++++++++- 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/.github/workflows/agentex-tutorials-test.yml b/.github/workflows/agentex-tutorials-test.yml index 2d0e23371..6ffca738f 100644 --- a/.github/workflows/agentex-tutorials-test.yml +++ b/.github/workflows/agentex-tutorials-test.yml @@ -162,7 +162,13 @@ jobs: id: run-test working-directory: ./examples/tutorials env: - OPENAI_API_KEY: ${{ secrets.TUTORIAL_OPENAI_API_KEY }} + # OpenAI calls go through an internal gateway that passes the OpenAI API through + # unchanged. The key and the gateway URL are both secrets, scoped to this CI. + OPENAI_API_KEY: ${{ secrets.TUTORIAL_SMIG_API_KEY }} + OPENAI_BASE_URL: ${{ secrets.TUTORIAL_OPENAI_BASE_URL }} + # The OpenAI Agents SDK exports traces to api.openai.com with OPENAI_API_KEY and ignores + # OPENAI_BASE_URL, which would send the gateway key to OpenAI. CI doesn't need those traces. + OPENAI_AGENTS_DISABLE_TRACING: "1" ANTHROPIC_API_KEY: ${{ secrets.TUTORIAL_ANTHROPIC_API_KEY }} # Enable the gated live tests only for the matching subprocess-CLI # harness tutorial (the CLI is installed for it in the step above). diff --git a/examples/tutorials/10_async/00_base/040_other_sdks/project/acp.py b/examples/tutorials/10_async/00_base/040_other_sdks/project/acp.py index a4d9dddea..8d57cd6a1 100644 --- a/examples/tutorials/10_async/00_base/040_other_sdks/project/acp.py +++ b/examples/tutorials/10_async/00_base/040_other_sdks/project/acp.py @@ -48,6 +48,14 @@ class StateModel(BaseModel): turn_number: int +def _websearch_env(): + """Env for the web search MCP server: the OpenAI key, plus OPENAI_BASE_URL when set (for a gateway).""" + env = {"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")} + if os.environ.get("OPENAI_BASE_URL"): + env["OPENAI_BASE_URL"] = os.environ["OPENAI_BASE_URL"] + return env + + MCP_SERVERS = [ StdioServerParameters( command="npx", @@ -59,7 +67,7 @@ class StateModel(BaseModel): # unpinned, so cap it until the server allows a newer mcp. command="uvx", args=["--with", "pydantic<2.14", "openai-websearch-mcp"], - env={"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")}, + env=_websearch_env(), ), ] diff --git a/examples/tutorials/10_async/10_temporal/010_agent_chat/project/workflow.py b/examples/tutorials/10_async/10_temporal/010_agent_chat/project/workflow.py index 7db940733..db4de8895 100644 --- a/examples/tutorials/10_async/10_temporal/010_agent_chat/project/workflow.py +++ b/examples/tutorials/10_async/10_temporal/010_agent_chat/project/workflow.py @@ -48,6 +48,14 @@ class StateModel(BaseModel): turn_number: int +def _websearch_env(): + """Env for the web search MCP server: the OpenAI key, plus OPENAI_BASE_URL when set (for a gateway).""" + env = {"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")} + if os.environ.get("OPENAI_BASE_URL"): + env["OPENAI_BASE_URL"] = os.environ["OPENAI_BASE_URL"] + return env + + MCP_SERVERS = [ # No longer needed due to reasoning # StdioServerParameters( # command="npx", @@ -59,7 +67,7 @@ class StateModel(BaseModel): # pydantic helper that pydantic 2.14 removed; uvx resolves pydantic # unpinned, so cap it until the server allows a newer mcp. args=["--with", "pydantic<2.14", "openai-websearch-mcp"], - env={"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")}, + env=_websearch_env(), ), ] diff --git a/examples/tutorials/10_async/10_temporal/020_state_machine/project/workflows/deep_research/performing_deep_research.py b/examples/tutorials/10_async/10_temporal/020_state_machine/project/workflows/deep_research/performing_deep_research.py index 2060cc9b7..ff724ee1d 100644 --- a/examples/tutorials/10_async/10_temporal/020_state_machine/project/workflows/deep_research/performing_deep_research.py +++ b/examples/tutorials/10_async/10_temporal/020_state_machine/project/workflows/deep_research/performing_deep_research.py @@ -13,6 +13,15 @@ logger = make_logger(__name__) + +def _websearch_env(): + """Env for the web search MCP server: the OpenAI key, plus OPENAI_BASE_URL when set (for a gateway).""" + env = {"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")} + if os.environ.get("OPENAI_BASE_URL"): + env["OPENAI_BASE_URL"] = os.environ["OPENAI_BASE_URL"] + return env + + # These reference MCP servers still import the mcp 1.x API (``McpError``), which # mcp 2.0.0 renamed to ``MCPError``. uvx gives each server its own isolated env and # resolves ``mcp`` unpinned there, ignoring the version this project pins, so without @@ -31,9 +40,7 @@ StdioServerParameters( command="uvx", args=[*_MCP_PIN, "openai-websearch-mcp"], - env={ - "OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "") - } + env=_websearch_env(), ), StdioServerParameters( command="uvx", diff --git a/examples/tutorials/10_async/10_temporal/050_agent_chat_guardrails/project/workflow.py b/examples/tutorials/10_async/10_temporal/050_agent_chat_guardrails/project/workflow.py index b54c8fade..7bb7fa3bd 100644 --- a/examples/tutorials/10_async/10_temporal/050_agent_chat_guardrails/project/workflow.py +++ b/examples/tutorials/10_async/10_temporal/050_agent_chat_guardrails/project/workflow.py @@ -67,6 +67,14 @@ class StateModel(BaseModel): turn_number: int +def _websearch_env(): + """Env for the web search MCP server: the OpenAI key, plus OPENAI_BASE_URL when set (for a gateway).""" + env = {"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")} + if os.environ.get("OPENAI_BASE_URL"): + env["OPENAI_BASE_URL"] = os.environ["OPENAI_BASE_URL"] + return env + + MCP_SERVERS = [ StdioServerParameters( command="npx", @@ -75,7 +83,7 @@ class StateModel(BaseModel): StdioServerParameters( command="uvx", args=["openai-websearch-mcp"], - env={"OPENAI_API_KEY": os.environ.get("OPENAI_API_KEY", "")}, + env=_websearch_env(), ), ]