Repository navigation
feat: add Azure OpenAI support to openAIAgent - #1270
Conversation
- Import AzureOpenAI class from openai package - Add apiVersion to OpenAIConfig type - Auto-detect Azure endpoints by checking if hostname ends with .openai.azure.com - Use AzureOpenAI client when Azure endpoint is detected - Add apiVersion to inputs and params schema - Add run_azure.ts test for Azure OpenAI Co-Authored-By: Claude Opus 4.5 <[email protected]>
📝 WalkthroughWalkthroughThe changes add Azure OpenAI support to the OpenAI agent. An optional Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@llm_agents/openai_agent/src/openai_agent.ts`:
- Around line 175-191: The AzureOpenAI client instantiation inside the openai
IIFE omits the dangerouslyAllowBrowser flag causing browser runs (when forWeb is
truthy) to be rejected; update the AzureOpenAI constructor call in the openai
constant to include dangerouslyAllowBrowser: !!forWeb (matching the OpenAI call)
so both paths pass the same browser-allowing option (refer to AzureOpenAI,
OpenAI, openai, baseURL, and forWeb).
🧹 Nitpick comments (4)
llm_agents/openai_agent/tests/run_azure.ts (4)
12-15: Consider usingtest.skipinstead ofprocess.exit(1).Using
process.exit(1)terminates the entire test runner, which prevents other test files from executing. Consider using conditional test skipping ordescribe.skipfor a more test-runner-friendly approach.Proposed alternative approach
-if (!baseURL || !apiKey) { - console.error("AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY are required"); - process.exit(1); -} +const skipTests = !baseURL || !apiKey; + +if (skipTests) { + console.warn("Skipping Azure tests: AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY are required"); +}Then wrap each test:
test("test azure openai", { skip: skipTests }, async () => { // test body });
25-25: Tests use no-op assertions.
assert.deepStrictEqual(true, true)always passes and doesn't validate the API response. Consider adding basic assertions to verify the response structure.Proposed improvement
- assert.deepStrictEqual(true, true); + assert.ok(res, "Response should not be null"); + assert.ok(res.choices?.[0]?.message?.content, "Response should contain message content");
22-24: Unsafe nested property access.The
if (res)check doesn't guard against missing nested properties. Consider using optional chaining for safer access.Proposed fix
if (res) { - console.log(res.choices[0].message["content"]); + console.log(res.choices?.[0]?.message?.content); }
46-50: Callback type may not match all possible values.The
streamTokenCallbackis typed as(token: string), but whendataStreammode is used, the callback receives objects. While this test only usesstream: true, using a more flexible type orunknownwith type narrowing would be more robust.Proposed improvement
filterParams: { - streamTokenCallback: (token: string) => { - process.stdout.write(token); + streamTokenCallback: (token: unknown) => { + if (typeof token === "string") { + process.stdout.write(token); + } }, },
| const openai = (() => { | ||
| if (baseURL) { | ||
| try { | ||
| const url = new URL(baseURL); | ||
| if (url.hostname.endsWith(".openai.azure.com")) { | ||
| return new AzureOpenAI({ | ||
| apiKey, | ||
| endpoint: baseURL, | ||
| apiVersion: apiVersion ?? "2025-04-01-preview", | ||
| }); | ||
| } | ||
| } catch { | ||
| // Invalid URL, fall through to standard OpenAI | ||
| } | ||
| } | ||
| return new OpenAI({ apiKey, baseURL, dangerouslyAllowBrowser: !!forWeb }); | ||
| })(); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# Find the file and check imports/type definitions
fd openai_agent.ts -type f | head -5Repository: receptron/graphai
Length of output: 231
🏁 Script executed:
# Search for AzureOpenAI imports and usage patterns
rg "AzureOpenAI|dangerouslyAllowBrowser" --type ts --context 2Repository: receptron/graphai
Length of output: 2428
🏁 Script executed:
# Check if there's any type definition or interface information visible
rg "new AzureOpenAI" -A 10 -B 2Repository: receptron/graphai
Length of output: 1063
🌐 Web query:
Azure OpenAI API versions 2025 official documentation
💡 Result:
Official Microsoft documentation pages that cover Azure OpenAI API versions in 2025:
- Azure OpenAI REST API preview reference (date-based
api-versionlike2025-04-01-preview) — includes the versioning scheme and calls out the2025-04-01-previewdata plane inference spec, plus a table showing control plane latest GA2025-06-01and latest preview2025-07-01-preview. [1] - Azure OpenAI “v1” REST API reference (path-based
/openai/v1/..., optionalapi-version=v1) — documents the GA v1 endpoints such asPOST {endpoint}/openai/v1/chat/completionsand notesapi-versiondefaults tov1if omitted. [2] - Azure OpenAI Responses API docs — describes the Responses API and states that v1 API is required for latest features. [3]
Sources: [1] [2] [3]
🏁 Script executed:
# Search for any type definitions or interfaces in the codebase
rg "interface.*AzureOpenAI|type.*AzureOpenAI" -A 5Repository: receptron/graphai
Length of output: 43
🏁 Script executed:
# Look for node_modules or package.json to understand dependencies
fd package.json | head -5 | xargs catRepository: receptron/graphai
Length of output: 5106
🏁 Script executed:
# Search for any azure-related type hints or configuration
rg "AzureOpenAI" -B 5 -A 15 --max-count 3Repository: receptron/graphai
Length of output: 2769
🌐 Web query:
AzureOpenAI constructor options dangerouslyAllowBrowser openai library
💡 Result:
In the official JavaScript/TypeScript openai library (openai-node), dangerouslyAllowBrowser is a constructor option (default false) that disables running the client in “browser-like” environments to avoid exposing secret credentials in client-side code. If the SDK detects a browser-like runtime and dangerouslyAllowBrowser is not true, it throws an error telling you to enable it only if you understand the risk. [1]
For Azure, the AzureOpenAI constructor accepts dangerouslyAllowBrowser too (along with Azure-specific options). The Azure constructor signature includes: baseURL, apiKey, apiVersion, endpoint, deployment, azureADTokenProvider, dangerouslyAllowBrowser, plus other shared client options. [2]
Azure-specific options are documented under AzureClientOptions (notably endpoint, deployment, apiVersion, and apiKey defaults like process.env['AZURE_OPENAI_API_KEY']). [3] The repo README also directs using AzureOpenAI for Azure usage. [4]
Example (AzureOpenAI in a browser — not recommended):
import { AzureOpenAI } from "openai";
const client = new AzureOpenAI({
endpoint: "https://YOUR-RESOURCE.azure.openai.com/",
apiKey: "YOUR_AZURE_OPENAI_KEY",
apiVersion: "2024-10-01-preview",
deployment: "YOUR_DEPLOYMENT",
dangerouslyAllowBrowser: true, // bypass browser guard (risk: key exposure)
});Strong recommendation: don’t use dangerouslyAllowBrowser with any long-lived secret key; proxy requests through your server instead. [1]
Sources: [1] [2] [3] [4]
Add missing dangerouslyAllowBrowser option to AzureOpenAI client.
The AzureOpenAI client is not configured with dangerouslyAllowBrowser: !!forWeb, while the standard OpenAI client receives this option (line 190). This inconsistency can cause runtime errors when the agent runs in browser environments with forWeb enabled, since AzureOpenAI will reject browser-like execution contexts by default.
Proposed fix
if (url.hostname.endsWith(".openai.azure.com")) {
return new AzureOpenAI({
apiKey,
endpoint: baseURL,
apiVersion: apiVersion ?? "2025-04-01-preview",
+ dangerouslyAllowBrowser: !!forWeb,
});
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const openai = (() => { | |
| if (baseURL) { | |
| try { | |
| const url = new URL(baseURL); | |
| if (url.hostname.endsWith(".openai.azure.com")) { | |
| return new AzureOpenAI({ | |
| apiKey, | |
| endpoint: baseURL, | |
| apiVersion: apiVersion ?? "2025-04-01-preview", | |
| }); | |
| } | |
| } catch { | |
| // Invalid URL, fall through to standard OpenAI | |
| } | |
| } | |
| return new OpenAI({ apiKey, baseURL, dangerouslyAllowBrowser: !!forWeb }); | |
| })(); | |
| const openai = (() => { | |
| if (baseURL) { | |
| try { | |
| const url = new URL(baseURL); | |
| if (url.hostname.endsWith(".openai.azure.com")) { | |
| return new AzureOpenAI({ | |
| apiKey, | |
| endpoint: baseURL, | |
| apiVersion: apiVersion ?? "2025-04-01-preview", | |
| dangerouslyAllowBrowser: !!forWeb, | |
| }); | |
| } | |
| } catch { | |
| // Invalid URL, fall through to standard OpenAI | |
| } | |
| } | |
| return new OpenAI({ apiKey, baseURL, dangerouslyAllowBrowser: !!forWeb }); | |
| })(); |
🤖 Prompt for AI Agents
In `@llm_agents/openai_agent/src/openai_agent.ts` around lines 175 - 191, The
AzureOpenAI client instantiation inside the openai IIFE omits the
dangerouslyAllowBrowser flag causing browser runs (when forWeb is truthy) to be
rejected; update the AzureOpenAI constructor call in the openai constant to
include dangerouslyAllowBrowser: !!forWeb (matching the OpenAI call) so both
paths pass the same browser-allowing option (refer to AzureOpenAI, OpenAI,
openai, baseURL, and forWeb).
Summary
openAIAgentby auto-detecting Azure endpointsbaseURLcontains.openai.azure.com, automatically useAzureOpenAIclientapiVersionconfig option for Azure API version (defaults to2025-04-01-preview)Changes
AzureOpenAIclass from openai packageapiVersiontoOpenAIConfigtype.openai.azure.comapiVersionto inputs and params schemarun_azure.tstest for Azure OpenAIUsage
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.