Repository navigation
fix(agents): custom providers with local base URLs rejected with "No API key found" after onboarding - #43118
fix(agents): custom providers with local base URLs rejected with "No API key found" after onboarding#43118xinhuagu wants to merge 618 commits into
Conversation
Greptile SummaryThis PR fixes a UX regression where custom providers (llama.cpp, vLLM, LocalAI, etc.) configured with a local Key changes:
Observations:
Confidence Score: 4/5
Last reviewed commit: 8a3cbe7 |
| it("does not synthesize auth for remote custom providers without apiKey", async () => { | ||
| await expect( | ||
| resolveApiKeyForProvider({ | ||
| provider: "my-remote", | ||
| cfg: { | ||
| models: { | ||
| providers: { | ||
| "my-remote": { | ||
| baseUrl: "https://api.example.com/v1", | ||
| api: "openai-completions", | ||
| models: [{ id: "gpt-5", name: "GPT-5" }], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).rejects.toThrow("No API key found"); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
No test coverage for ::1 (IPv6 loopback) and 0.0.0.0
The PR description lists ::1 and 0.0.0.0 as supported local addresses in isLocalBaseUrl, but neither is exercised by a test. Given the slightly unusual URL parsing for IPv6 (http://[::1]:8080 → hostname === "::1") it's worth having at least one test to confirm the parsing works correctly.
it("synthesizes a local auth marker for IPv6 loopback custom providers", async () => {
const auth = await resolveApiKeyForProvider({
provider: "my-ipv6-local",
cfg: {
models: {
providers: {
"my-ipv6-local": {
baseUrl: "http://[::1]:11434/v1",
api: "openai-completions",
models: [{ id: "llama3", name: "Llama 3" }],
},
},
},
},
});
expect(auth.apiKey).toBe(CUSTOM_LOCAL_AUTH_MARKER);
});Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/model-auth.test.ts
Line: 266-284
Comment:
**No test coverage for `::1` (IPv6 loopback) and `0.0.0.0`**
The PR description lists `::1` and `0.0.0.0` as supported local addresses in `isLocalBaseUrl`, but neither is exercised by a test. Given the slightly unusual URL parsing for IPv6 (`http://[::1]:8080` → `hostname === "::1"`) it's worth having at least one test to confirm the parsing works correctly.
```typescript
it("synthesizes a local auth marker for IPv6 loopback custom providers", async () => {
const auth = await resolveApiKeyForProvider({
provider: "my-ipv6-local",
cfg: {
models: {
providers: {
"my-ipv6-local": {
baseUrl: "http://[::1]:11434/v1",
api: "openai-completions",
models: [{ id: "llama3", name: "Llama 3" }],
},
},
},
},
});
expect(auth.apiKey).toBe(CUSTOM_LOCAL_AUTH_MARKER);
});
```
How can I resolve this? If you propose a fix, please make it concise.| if (providerConfig.baseUrl && isLocalBaseUrl(providerConfig.baseUrl)) { | ||
| return { | ||
| apiKey: CUSTOM_LOCAL_AUTH_MARKER, | ||
| source: `models.providers.${params.provider} (synthetic local key)`, | ||
| mode: "api-key", | ||
| }; | ||
| } |
There was a problem hiding this comment.
Implicit reliance on call-site ordering for apiKey guard
resolveSyntheticLocalProviderAuth assumes it is only invoked after resolveUsableCustomProviderApiKey has already ruled out any configured key. If the resolution order in resolveApiKeyForProvider is ever rearranged, a provider with a stored-but-broken env-var marker (env var configured but not set) whose baseUrl is local would silently receive the synthetic marker rather than a clear "credentials not found" error.
Adding an explicit guard inside the function would make the precondition self-enforcing and easier to reason about independently:
if (normalizeOptionalSecretInput(providerConfig.apiKey)) {
return null;
}
This also matches the documented condition from the PR description: "No explicit apiKey is set in config".
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/model-auth.ts
Line: 167-173
Comment:
**Implicit reliance on call-site ordering for `apiKey` guard**
`resolveSyntheticLocalProviderAuth` assumes it is only invoked after `resolveUsableCustomProviderApiKey` has already ruled out any configured key. If the resolution order in `resolveApiKeyForProvider` is ever rearranged, a provider with a stored-but-broken env-var marker (env var configured but not set) whose `baseUrl` is local would silently receive the synthetic marker rather than a clear "credentials not found" error.
Adding an explicit guard inside the function would make the precondition self-enforcing and easier to reason about independently:
```
if (normalizeOptionalSecretInput(providerConfig.apiKey)) {
return null;
}
```
This also matches the documented condition from the PR description: "No explicit `apiKey` is set in config".
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8a3cbe7e67
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| host === "localhost" || | ||
| host === "127.0.0.1" || | ||
| host === "0.0.0.0" || | ||
| host === "::1" || |
There was a problem hiding this comment.
Handle bracketed IPv6 loopback in local URL check
isLocalBaseUrl compares the parsed hostname to "::1", but WHATWG URL.hostname returns bracketed IPv6 literals (for example, new URL("http://[::1]:11434").hostname is "[::1]"). As a result, custom providers configured with IPv6 loopback base URLs are treated as non-local and still throw No API key found when API key is intentionally left blank.
Useful? React with 👍 / 👎.
ademczuk
left a comment
There was a problem hiding this comment.
Clean implementation. Extending the existing Ollama synthetic auth pattern to cover local custom providers makes sense - llama.cpp, vLLM, and LocalAI users shouldn't need to wrestle with auth when their server is on localhost.
A few notes:
-
.localdomain scope -isLocalBaseUrlincludeshost.endsWith(".local"), which covers mDNS/Bonjour addresses. These resolve to LAN machines, not necessarily the local host itself. Since the user explicitly configured the provider and left the API key blank, this is probably fine in practice, but worth noting thatmyserver.localcould be a different machine on the network. If maintainers want a tighter scope, limiting to justlocalhost/127.0.0.1/::1/0.0.0.0would be the conservative path. -
IPv4-mapped IPv6 -
http://[::ffff:127.0.0.1]:8080/v1is a valid loopback URL whose hostname resolves to::ffff:127.0.0.1. The current check wouldn't catch this. Very unlikely in practice, but ahost.startsWith("::ffff:127.")guard would cover it if completeness matters. -
Test coverage is solid - three tests covering 127.0.0.1, localhost, and remote rejection. Good that the negative case (remote URL still rejects) is included.
-
Marker registration -
CUSTOM_LOCAL_AUTH_MARKERis properly registered inisNonSecretApiKeyMarker, so it won't leak into credential stores. Good.
Overall the approach is sound and the refactoring of resolveSyntheticLocalProviderAuth (moving the ollama check after the baseUrl extraction) is cleaner than the original.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f16846db05
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Custom providers pointing at a local server (e.g. llama.cpp, vLLM, LocalAI) | ||
| // typically don't require auth. Synthesize a local key so the auth resolver | ||
| // doesn't reject them when the user left the API key blank during onboarding. | ||
| if (providerConfig.baseUrl && isLocalBaseUrl(providerConfig.baseUrl)) { |
There was a problem hiding this comment.
Skip synthetic local auth when apiKey is explicitly configured
This fallback currently triggers for any local baseUrl even when the provider has an explicit apiKey configured as an env marker (for example OPENAI_API_KEY) that is temporarily unset. In that case resolveUsableCustomProviderApiKey returns null, this branch synthesizes custom-local, and we lose the clear "missing API key" error in favor of downstream auth failures (e.g. 401s) for local servers that do enforce auth. The synthetic marker should only be used when no apiKey is configured at all.
Useful? React with 👍 / 👎.
|
Thanks for the thorough review @ademczuk! Both points addressed in the latest commit (a3ac194):
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a3ac1940cc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| host === "localhost" || | ||
| host === "127.0.0.1" || | ||
| host === "0.0.0.0" || | ||
| host === "::1" || | ||
| host === "[::1]" || |
There was a problem hiding this comment.
Treat .local hostnames as local provider URLs
isLocalBaseUrl currently hard-codes a few literal loopback hosts, so a custom endpoint like http://llm-box.local:11434/v1 is still classified as non-local and resolveApiKeyForProvider falls through to No API key found when users intentionally leave the API key blank. That breaks the intended onboarding path for LAN-local servers that are commonly addressed via mDNS; this helper should include .local host suffixes in its local-address check.
Useful? React with 👍 / 👎.
a3ac194 to
fcd285d
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fcd285db07
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (providerConfig.baseUrl && isLocalBaseUrl(providerConfig.baseUrl)) { | ||
| return { | ||
| apiKey: CUSTOM_LOCAL_AUTH_MARKER, | ||
| source: `models.providers.${params.provider} (synthetic local key)`, | ||
| mode: "api-key", |
There was a problem hiding this comment.
Respect auth override before synthesizing local API key
resolveSyntheticLocalProviderAuth now returns CUSTOM_LOCAL_AUTH_MARKER for local base URLs without checking models.providers.<id>.auth, so a provider explicitly configured with auth: "token" or auth: "oauth" can be silently treated as mode: "api-key" whenever token/oauth credentials are missing. In that configuration-specific path, we lose the expected missing-credential failure and instead send a synthetic bearer key, which produces confusing downstream auth failures and bypasses the caller’s explicit auth mode choice.
Useful? React with 👍 / 👎.
fcd285d to
ec3951f
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ec3951f0f9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (providerConfig.baseUrl && isLocalBaseUrl(providerConfig.baseUrl)) { | ||
| return { | ||
| apiKey: CUSTOM_LOCAL_AUTH_MARKER, | ||
| source: `models.providers.${params.provider} (synthetic local key)`, | ||
| mode: "api-key", |
There was a problem hiding this comment.
Restrict synthetic local key fallback to custom providers
resolveSyntheticLocalProviderAuth now returns CUSTOM_LOCAL_AUTH_MARKER for any provider whose configured baseUrl is local, not just custom providers. That changes auth mode for built-ins: for example, amazon-bedrock with a local baseUrl and no explicit auth now exits via this branch as mode: "api-key", so resolveApiKeyForProvider never reaches its Bedrock aws-sdk fallback and will send a synthetic bearer token instead of AWS credential-chain auth.
Useful? React with 👍 / 👎.
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
14 similar comments
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
6 similar comments
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
2 similar comments
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
|
Closing this PR because it looks dirty (too many unrelated or unexpected changes). This usually happens when a branch picks up unrelated commits or a merge went sideways. Please recreate the PR from a clean branch. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a2b104c520
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| typeof providerConfig.api === "string" && | ||
| providerConfig.api.trim().length > 0 && |
There was a problem hiding this comment.
Allow synthetic local auth when provider api is implicit
ModelProviderConfig.api is optional, and model resolution falls back to "openai-responses" when it is omitted (src/agents/pi-embedded-runner/model.ts:241). The new isCustomLocalProviderConfig gate now requires providerConfig.api to be a non-empty string, so local custom providers that rely on the implicit default adapter never enter synthetic-local auth and still fail with No API key found even though they are valid no-auth local configs.
Useful? React with 👍 / 👎.
|
Recreated this as clean replacement PR #45631 from current The replacement contains the intended local-custom-provider auth fix plus the follow-up guard/runtime-header correction that came out of review, without the unrelated branch drift that triggered the |
Summary
Custom providers pointing at local servers (llama.cpp, vLLM, LocalAI, etc.) fail with "No API key found" when the user follows the onboarding prompt and leaves the API key blank.
Problem
The onboarding TUI says "API Key (leave blank if not required)", but the auth resolver only synthesizes a local auth marker for Ollama providers. Custom providers with local base URLs (127.0.0.1, localhost, ::1) are rejected even though their servers typically do not require authentication.
Fixes #42890
Solution
Extend
resolveSyntheticLocalProviderAuthto also cover custom providers whosebaseUrlpoints to a local address. A newCUSTOM_LOCAL_AUTH_MARKER("custom-local") is synthesized for these providers, matching the existing pattern used for Ollama ("ollama-local").The marker is only applied when:
baseUrlapiKeyis set in config or environmentRemote providers without an API key still fail as expected.
Changes
model-auth-markers.ts— addCUSTOM_LOCAL_AUTH_MARKERconstant and register it as a non-secret markermodel-auth.ts— addisLocalBaseUrlhelper; extendresolveSyntheticLocalProviderAuthto handle local custom providersmodel-auth.test.ts— 3 new tests: 127.0.0.1 succeeds, localhost succeeds, remote URL still rejects