Skip to content

Commit 330c3d4

Browse files
committed
feat(ollama): add diagnostic logging for debugging request hangs
Adds comprehensive logging to help diagnose cases where Ollama requests hang indefinitely, as reported in #11049. The logging includes: - Client creation with base URL - Request start timestamps - Model fetch timing and cache status - Warning when model is not in tool-capable list - Chat request details (URL, model, message count, tool count) - Stream start timing Also adds DEFAULT_OLLAMA_TIMEOUT_MS constant (5 minutes) to accommodate slow model loading times (Ollama models can take 30-60+ seconds to load into memory on first use). This logging will help users identify: 1. If the request is reaching Ollama at all 2. Where in the request lifecycle things are hanging 3. If the model is being filtered due to missing tool capabilities Fixes #11049
1 parent fe722da commit 330c3d4

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/api/providers/native-ollama.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ interface OllamaChatOptions {
1414
num_ctx?: number
1515
}
1616

17+
// Default timeout for Ollama requests (5 minutes to accommodate slow model loading)
18+
// Ollama models can take 30-60+ seconds to load into memory on first use
19+
const DEFAULT_OLLAMA_TIMEOUT_MS = 300_000 // 5 minutes
20+
1721
function convertToOllamaMessages(anthropicMessages: Anthropic.Messages.MessageParam[]): Message[] {
1822
const ollamaMessages: Message[] = []
1923

@@ -158,9 +162,11 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
158162
private ensureClient(): Ollama {
159163
if (!this.client) {
160164
try {
165+
const baseUrl = this.options.ollamaBaseUrl || "http://localhost:11434"
166+
console.log(`[Ollama] Creating client for host: ${baseUrl}`)
167+
161168
const clientOptions: OllamaOptions = {
162-
host: this.options.ollamaBaseUrl || "http://localhost:11434",
163-
// Note: The ollama npm package handles timeouts internally
169+
host: baseUrl,
164170
}
165171

166172
// Add API key if provided (for Ollama cloud or authenticated instances)
@@ -172,6 +178,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
172178

173179
this.client = new Ollama(clientOptions)
174180
} catch (error: any) {
181+
console.error(`[Ollama] Error creating client: ${error.message}`)
175182
throw new Error(`Error creating Ollama client: ${error.message}`)
176183
}
177184
}
@@ -205,8 +212,29 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
205212
messages: Anthropic.Messages.MessageParam[],
206213
metadata?: ApiHandlerCreateMessageMetadata,
207214
): ApiStream {
215+
const baseUrl = this.options.ollamaBaseUrl || "http://localhost:11434"
216+
const requestStartTime = Date.now()
217+
218+
console.log(`[Ollama] createMessage: Starting request at ${new Date().toISOString()}`)
219+
208220
const client = this.ensureClient()
209-
const { id: modelId } = await this.fetchModel()
221+
222+
console.log(`[Ollama] createMessage: Fetching model info...`)
223+
const { id: modelId, info: modelInfo } = await this.fetchModel()
224+
console.log(
225+
`[Ollama] createMessage: Model '${modelId}' fetched in ${Date.now() - requestStartTime}ms, ` +
226+
`found in cache: ${!!this.models[modelId]}`,
227+
)
228+
229+
// Warn if model wasn't found in the tool-capable models list
230+
if (!this.models[modelId]) {
231+
console.warn(
232+
`[Ollama] Warning: Model '${modelId}' was not found in the list of tool-capable models. ` +
233+
`This may indicate the model does not support native tool calling, or your Ollama version ` +
234+
`does not report capabilities. Check with: ollama show ${modelId}`,
235+
)
236+
}
237+
210238
const useR1Format = modelId.toLowerCase().includes("deepseek-r1")
211239

212240
const ollamaMessages: Message[] = [
@@ -234,15 +262,25 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio
234262
chatOptions.num_ctx = this.options.ollamaNumCtx
235263
}
236264

265+
const toolsToSend = this.convertToolsToOllama(metadata?.tools)
266+
console.log(
267+
`[Ollama] createMessage: Sending chat request to ${baseUrl}/api/chat with model '${modelId}', ` +
268+
`${ollamaMessages.length} messages, ${toolsToSend?.length ?? 0} tools`,
269+
)
270+
271+
const chatStartTime = Date.now()
272+
237273
// Create the actual API request promise
238274
const stream = await client.chat({
239275
model: modelId,
240276
messages: ollamaMessages,
241277
stream: true,
242278
options: chatOptions,
243-
tools: this.convertToolsToOllama(metadata?.tools),
279+
tools: toolsToSend,
244280
})
245281

282+
console.log(`[Ollama] createMessage: Stream started after ${Date.now() - chatStartTime}ms`)
283+
246284
let totalInputTokens = 0
247285
let totalOutputTokens = 0
248286
// Track tool calls across chunks (Ollama may send complete tool_calls in final chunk)

0 commit comments

Comments
 (0)