Skip to content

Commit 0dfd87e

Browse files
authored
fix(ollama): skip local checks for hosted web search (#129833)
* fix(ollama): skip local checks for hosted web search * fix(ollama): explain hosted search API key requirements * docs(ollama): separate hosted and local search setup
1 parent 079f819 commit 0dfd87e

4 files changed

Lines changed: 136 additions & 29 deletions

File tree

‎docs/help/faq.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ First-run Q&A - install, onboard, auth routes, subscriptions, initial failures -
608608
| Grok | No (xAI OAuth or key) | `XAI_API_KEY` |
609609
| Kimi | No | `KIMI_API_KEY` or `MOONSHOT_API_KEY` |
610610
| MiniMax Search | No | `MINIMAX_CODE_PLAN_KEY`, `MINIMAX_CODING_API_KEY`, or `MINIMAX_API_KEY` |
611-
| Ollama Web Search | Yes (needs `ollama signin`) | - |
611+
| Ollama Web Search | Local: yes (needs `ollama signin`); hosted: no | Hosted: `OLLAMA_API_KEY` |
612612
| Perplexity | No | `PERPLEXITY_API_KEY` or `OPENROUTER_API_KEY` |
613613
| SearXNG | Yes (self-hosted) | `SEARXNG_BASE_URL` |
614614
| Tavily | No | `TAVILY_API_KEY` |

‎docs/tools/ollama-search.md‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ Ollama host plus `ollama signin`. Direct hosted search (no local Ollama) needs
1717

1818
## Setup
1919

20+
If you already use Ollama for models, Ollama Web Search reuses the same
21+
configured host.
22+
23+
### Local Ollama
24+
2025
<Steps>
2126
<Step title="Start Ollama">
2227
Make sure Ollama is installed and running.
@@ -36,8 +41,15 @@ Ollama host plus `ollama signin`. Direct hosted search (no local Ollama) needs
3641
</Step>
3742
</Steps>
3843

39-
If you already use Ollama for models, Ollama Web Search reuses the same
40-
configured host.
44+
### Hosted Ollama
45+
46+
1. Create an [Ollama API key](https://docs.ollama.com/api/authentication#api-keys)
47+
and set `OLLAMA_API_KEY` in the Gateway environment.
48+
2. Set `models.providers.ollama.baseUrl` to `https://ollama.com`; see
49+
[Config](#config).
50+
3. Run `openclaw configure --section web` and select **Ollama Web Search**.
51+
52+
Hosted search does not require a local Ollama daemon or `ollama signin`.
4153

4254
<Note>
4355
OpenClaw never auto-selects Ollama Web Search over a higher-priority
@@ -134,8 +146,9 @@ Direct hosted Ollama Web Search (no local Ollama):
134146
and `OLLAMA_API_KEY` is set, it retries once against
135147
`https://ollama.com/api/web_search` with that key — without sending it to
136148
the local host.
137-
- OpenClaw warns during setup if Ollama is unreachable or not signed in, but
138-
does not block selecting the provider.
149+
- OpenClaw warns during setup if a local Ollama host is unreachable or not
150+
signed in, or if hosted search has no API key. These warnings do not block
151+
selecting the provider.
139152

140153
## Related
141154

‎extensions/ollama/src/web-search-provider.runtime.ts‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { coerceSecretRef } from "openclaw/plugin-sdk/secret-input";
2323
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
2424
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
25-
import { OLLAMA_DEFAULT_BASE_URL } from "./defaults.js";
25+
import { OLLAMA_CLOUD_BASE_URL, OLLAMA_DEFAULT_BASE_URL } from "./defaults.js";
2626
import { readProviderBaseUrl } from "./provider-base-url.js";
2727
import {
2828
buildOllamaBaseUrlSsrFPolicy,
@@ -37,10 +37,11 @@ import {
3737

3838
const OLLAMA_HOSTED_WEB_SEARCH_PATH = "/api/web_search";
3939
const OLLAMA_LOCAL_WEB_SEARCH_PROXY_PATH = "/api/experimental/web_search";
40-
const OLLAMA_CLOUD_BASE_URL = "https://ollama.com";
4140
const DEFAULT_OLLAMA_WEB_SEARCH_COUNT = 5;
4241
const DEFAULT_OLLAMA_WEB_SEARCH_TIMEOUT_MS = 15_000;
4342
const OLLAMA_WEB_SEARCH_SNIPPET_MAX_CHARS = 300;
43+
const OLLAMA_CLOUD_WEB_SEARCH_AUTH_ERROR =
44+
"Hosted Ollama Web Search requires an API key. Set OLLAMA_API_KEY or configure models.providers.ollama.apiKey.";
4445

4546
type OllamaWebSearchResult = {
4647
title?: string;
@@ -220,7 +221,11 @@ async function runOllamaWebSearch(params: {
220221

221222
try {
222223
if (response.status === 401) {
223-
throw new Error("Ollama web search authentication failed. Run `ollama signin`.");
224+
throw new Error(
225+
isOllamaCloudBaseUrl(attempt.baseUrl)
226+
? OLLAMA_CLOUD_WEB_SEARCH_AUTH_ERROR
227+
: "Ollama web search authentication failed. Run `ollama signin`.",
228+
);
224229
}
225230
if (response.status === 403) {
226231
throw new Error(
@@ -243,11 +248,7 @@ async function runOllamaWebSearch(params: {
243248
params.signal?.throwIfAborted();
244249
break;
245250
} catch (error) {
246-
if (error instanceof Error) {
247-
lastError = error;
248-
} else {
249-
lastError = new Error(String(error));
250-
}
251+
lastError = error instanceof Error ? error : new Error(String(error));
251252
throw lastError;
252253
} finally {
253254
// The 401/403 branches throw before the stream is touched, leaving release
@@ -301,14 +302,20 @@ async function warnOllamaWebSearchPrereqs(params: {
301302
};
302303
}): Promise<OpenClawConfig> {
303304
const baseUrl = resolveOllamaWebSearchBaseUrl(params.config);
305+
if (isOllamaCloudBaseUrl(baseUrl)) {
306+
if (
307+
!resolveConfiguredOllamaWebSearchApiKey(params.config) &&
308+
!resolveEnvOllamaWebSearchApiKey()
309+
) {
310+
await params.prompter.note(OLLAMA_CLOUD_WEB_SEARCH_AUTH_ERROR, "Ollama Web Search");
311+
}
312+
return params.config;
313+
}
314+
304315
const { reachable } = await fetchOllamaModels(baseUrl);
305316
if (!reachable) {
306317
await params.prompter.note(
307-
[
308-
"Ollama Web Search requires Ollama to be running.",
309-
`Expected host: ${baseUrl}`,
310-
"Start Ollama before using this provider.",
311-
].join("\n"),
318+
`Ollama Web Search requires Ollama to be running.\nExpected host: ${baseUrl}\nStart Ollama before using this provider.`,
312319
"Ollama Web Search",
313320
);
314321
return params.config;
@@ -318,10 +325,7 @@ async function warnOllamaWebSearchPrereqs(params: {
318325
const auth = await checkOllamaCloudAuth(baseUrl);
319326
if (!auth.signedIn) {
320327
await params.prompter.note(
321-
[
322-
"Ollama Web Search requires `ollama signin`.",
323-
...(auth.signinUrl ? [auth.signinUrl] : ["Run `ollama signin`."]),
324-
].join("\n"),
328+
`Ollama Web Search requires \`ollama signin\`.\n${auth.signinUrl ?? "Run `ollama signin`."}`,
325329
"Ollama Web Search",
326330
);
327331
}
@@ -345,11 +349,7 @@ export function createOllamaWebSearchProvider(): WebSearchProviderPlugin {
345349
getCredentialValue: () => undefined,
346350
setCredentialValue: () => {},
347351
applySelectionConfig: (config) => enablePluginInConfig(config, "ollama").config,
348-
runSetup: async (ctx) =>
349-
await warnOllamaWebSearchPrereqs({
350-
config: ctx.config,
351-
prompter: ctx.prompter,
352-
}),
352+
runSetup: async (ctx) => await warnOllamaWebSearchPrereqs(ctx),
353353
createTool: (ctx) => ({
354354
description: OLLAMA_WEB_SEARCH_TOOL_DESCRIPTION,
355355
parameters: OLLAMA_WEB_SEARCH_TOOL_PARAMETERS,

‎extensions/ollama/src/web-search-provider.test.ts‎

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { withEnvAsync } from "openclaw/plugin-sdk/test-env";
55
import { beforeEach, describe, expect, it, vi } from "vitest";
66
import { createStreamingResponse } from "../../test-support/streaming-error-response.js";
77
import { createOllamaWebSearchProvider as createContractOllamaWebSearchProvider } from "../web-search-contract-api.js";
8+
import { createLazyOllamaWebSearchProvider } from "./web-search-provider-registration.js";
89
import { createOllamaWebSearchProvider } from "./web-search-provider.js";
910

1011
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
@@ -40,8 +41,10 @@ function createOllamaConfig(provider: OllamaProviderConfigOverride = {}): OpenCl
4041
};
4142
}
4243

43-
async function runOllamaWebSearchSetup(config: OpenClawConfig) {
44-
const provider = createOllamaWebSearchProvider();
44+
async function runOllamaWebSearchSetup(
45+
config: OpenClawConfig,
46+
provider = createOllamaWebSearchProvider(),
47+
) {
4548
if (!provider.runSetup) {
4649
throw new Error("Expected Ollama web search setup");
4750
}
@@ -460,6 +463,14 @@ describe("ollama web search provider", () => {
460463
);
461464
});
462465

466+
it("surfaces API-key guidance for hosted Ollama 401 responses", async () => {
467+
fetchWithSsrFGuardMock.mockResolvedValue(guardedResponse("", { status: 401 }));
468+
469+
await expect(
470+
runOllamaWebSearch(createOllamaConfig({ baseUrl: "https://ollama.com" })),
471+
).rejects.toThrow("Set OLLAMA_API_KEY or configure models.providers.ollama.apiKey");
472+
});
473+
463474
it("reports malformed Ollama web search JSON with a stable provider error", async () => {
464475
fetchWithSsrFGuardMock.mockResolvedValueOnce(guardedResponse("{ nope"));
465476

@@ -503,6 +514,89 @@ describe("ollama web search provider", () => {
503514
);
504515
});
505516

517+
it.each([
518+
{
519+
name: "the configured model provider",
520+
config: createOllamaConfig({ baseUrl: "https://ollama.com", apiKey: "hosted-test-key" }),
521+
},
522+
{
523+
name: "the plugin's higher-priority web search override",
524+
config: {
525+
...createOllamaConfig({ apiKey: "hosted-test-key" }),
526+
plugins: {
527+
entries: {
528+
ollama: { config: { webSearch: { baseUrl: "https://ollama.com/v1" } } },
529+
},
530+
},
531+
},
532+
},
533+
])("does not require a local daemon when $name selects hosted search", async ({ config }) => {
534+
fetchWithSsrFGuardMock
535+
.mockResolvedValueOnce(guardedResponse({ models: [] }))
536+
.mockResolvedValueOnce(guardedResponse({ error: "not signed in" }, { status: 401 }));
537+
538+
const { next, notes } = await runOllamaWebSearchSetup(
539+
config,
540+
createLazyOllamaWebSearchProvider(),
541+
);
542+
543+
expect(next).toBe(config);
544+
expect(notes).toEqual([]);
545+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
546+
});
547+
548+
it.each([
549+
{
550+
name: "the configured model provider",
551+
config: createOllamaConfig({ baseUrl: "https://ollama.com" }),
552+
},
553+
{
554+
name: "the plugin's higher-priority web search override",
555+
config: {
556+
...createOllamaConfig(),
557+
plugins: {
558+
entries: {
559+
ollama: { config: { webSearch: { baseUrl: "https://ollama.com/v1" } } },
560+
},
561+
},
562+
},
563+
},
564+
])("warns when $name selects hosted search without an API key", async ({ config }) => {
565+
await withEnvAsync({ OLLAMA_API_KEY: undefined }, async () => {
566+
const { next, notes } = await runOllamaWebSearchSetup(
567+
config,
568+
createLazyOllamaWebSearchProvider(),
569+
);
570+
571+
expect(next).toBe(config);
572+
expect(notes).toEqual([
573+
{
574+
title: "Ollama Web Search",
575+
message:
576+
"Hosted Ollama Web Search requires an API key. Set OLLAMA_API_KEY or configure models.providers.ollama.apiKey.",
577+
},
578+
]);
579+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
580+
});
581+
});
582+
583+
it("accepts the ambient API key for hosted Ollama search setup", async () => {
584+
await withEnvAsync({ OLLAMA_API_KEY: "hosted-env-key" }, async () => {
585+
const config = createOllamaConfig({
586+
baseUrl: "https://ollama.com",
587+
apiKey: "OLLAMA_API_KEY",
588+
});
589+
const { next, notes } = await runOllamaWebSearchSetup(
590+
config,
591+
createLazyOllamaWebSearchProvider(),
592+
);
593+
594+
expect(next).toBe(config);
595+
expect(notes).toEqual([]);
596+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
597+
});
598+
});
599+
506600
it("resolves env var when config apiKey is a marker string", async () => {
507601
await withEnvAsync({ OLLAMA_API_KEY: "real-secret-from-env" }, async () => {
508602
mockSuccessfulSearchResponse();

0 commit comments

Comments
 (0)