{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com" } }}const override = overrides.get(provider);if (!override) return models;
// Apply baseUrl/headers override to all models of this providerconst resolvedHeaders = resolveHeaders(override.headers);return models.map((m) => ({ ...m, baseUrl: override.baseUrl ?? m.baseUrl, headers: resolvedHeaders ? { ...m.headers, ...resolvedHeaders } : m.headers,}));return new OpenAI({ apiKey, baseURL: model.baseUrl, dangerouslyAllowBrowser: true, defaultHeaders: headers,});{ "providers": { "anthropic": { "baseUrl": "https://my-proxy.example.com/v1" } }}{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com" } }}{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com/v1" } }}{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com" } }}return new OpenAI({ apiKey, baseURL: model.baseUrl, dangerouslyAllowBrowser: true, defaultHeaders: headers,});```
The issue is clear:
1. User sets `baseUrl: "https://us.api.openai.com"` (missing `/v1`)2. The OpenAI SDK hits `https://us.api.openai.com/chat/completions` (or similar) which returns HTML (404 or landing page)3. The SDK tries to parse this as SSE data, resulting in garbage output
## Summary of Analysis
**Issue**: OpenAI has regional endpoints (`us.api.openai.com`) for US data residency. Users with US data residency API keys get 401 errors on the default global endpoint.
**User's attempted fix**: Setting `baseUrl: "https://us.api.openai.com"` in models.json, which is **missing the `/v1` suffix**.
**Result**: The request hits an HTML page instead of the API, producing garbage output.
**Root cause**: User error (missing `/v1`), but the documentation doesn't explicitly mention OpenAI regional endpoints or the requirement for the `/v1` suffix when overriding OpenAI's baseUrl.
## Proposed Fix
1. **Documentation update** in `docs/models.md`: Add a specific example for OpenAI regional endpoints showing the correct URL format (`https://us.api.openai.com/v1`).
2. **(Optional) Add `OPENAI_BASE_URL` environment variable support**: This is the standard way the official OpenAI SDK supports custom base URLs. Users could then just set `OPENAI_BASE_URL=https://us.api.openai.com/v1` without needing models.json.
3. I could also add validation to detect HTML responses—if the response starts with `<!DOCTYPE` or `<html`, throw a descriptive error message pointing users to verify their baseUrl includes `/v1`. This would catch the issue immediately rather than producing garbled output.
## Files to Modify
For the minimal fix (documentation only):- `packages/coding-agent/docs/models.md`: Add OpenAI regional endpoint example
For a more complete solution:- `packages/ai/src/providers/openai-responses.ts`: Check `OPENAI_BASE_URL` env var and add HTML detection- `packages/ai/src/providers/openai-completions.ts`: Apply the same changes- `packages/coding-agent/src/cli/args.ts`: Document the `OPENAI_BASE_URL` variable- `packages/ai/README.md`: Add documentation for `OPENAI_BASE_URL`- `packages/coding-agent/docs/models.md`: Include regional endpoint examples{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com" } }}{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com/v1" } }}return new OpenAI({ apiKey, baseURL: model.baseUrl || process.env.OPENAI_BASE_URL, dangerouslyAllowBrowser: true, defaultHeaders: headers,});const baseURL = model.provider === "openai" && process.env.OPENAI_BASE_URL ? process.env.OPENAI_BASE_URL : model.baseUrl;### OpenAI Regional Endpoints
For OpenAI projects with data residency requirements (US, EU), override the base URL.Note: The `/v1` path suffix is required.
```json{ "providers": { "openai": { "baseUrl": "https://us.api.openai.com/v1" } }}