Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: use centralized URL normalization across entire codebase

LearningCircuit05efbc4b50b8
diff --git a/docs/ollama-url-parsing-fix.md b/docs/ollama-url-parsing-fix.md
new file mode 100644
index 0000000000..648f73cd77
--- /dev/null
+++ b/docs/ollama-url-parsing-fix.md
@@ -0,0 +1,83 @@
+# Ollama URL Parsing Fix
+
+## Overview
+
+This document describes the fix for Ollama server URL parsing issues that were causing connection failures when URLs lacked proper scheme prefixes.
+
+## Problem
+
+When users configured Ollama server URLs without explicit schemes (e.g., `localhost:11434` instead of `http://localhost:11434`), the application would fail to connect to the Ollama server. This was due to inconsistent URL parsing across different parts of the codebase.
+
+## Solution
+
+The fix implements a centralized URL normalization utility function that handles various URL formats:
+
+1. **Created `url_utils.py`**: A new utility module containing the `normalize_ollama_url()` function
+2. **Consistent URL handling**: The function properly handles:
+ - URLs without schemes (`localhost:11434` → `http://localhost:11434`)
+ - Malformed URLs (`http:localhost` → `http://localhost`)
+ - URLs with double slashes (`//example.com` → `https://example.com`)
+ - Already well-formed URLs (unchanged)
+
+## Implementation Details
+
+### URL Normalization Logic
+
+The `normalize_ollama_url()` function:
+- Adds `http://` to localhost addresses (localhost, 127.0.0.1, [::1], 0.0.0.0)
+- Adds `https://` to external hostnames
+- Fixes malformed URLs like `http:hostname` to `http://hostname`
+- Preserves already well-formed URLs
+
+### Files Modified
+
+1. **New file**: `src/local_deep_research/utilities/url_utils.py`
+2. **Updated**: `src/local_deep_research/config/llm_config.py`
+3. **Updated**: `src/local_deep_research/web/routes/api_routes.py`
+4. **Updated**: `src/local_deep_research/web/routes/settings_routes.py`
+5. **Updated**: `src/local_deep_research/web_search_engines/engines/search_engine_local.py`
+
+### Usage Example
+
+```python
+from local_deep_research.utilities.url_utils import normalize_ollama_url
+
+# Before normalization
+raw_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
+
+# After normalization
+normalized_url = normalize_ollama_url(raw_url)
+```
+
+## Testing
+
+A comprehensive test suite was added to verify URL normalization:
+- `tests/test_url_utils.py`: Full test suite with pytest
+- `tests/test_url_utils_simple.py`: Simple test without external dependencies
+- `tests/test_url_utils_debug.py`: Debug test for manual verification
+
+All tests pass, confirming correct handling of various URL formats.
+
+## Benefits
+
+1. **Robustness**: Handles a variety of URL formats users might enter
+2. **Consistency**: Single source of truth for URL normalization
+3. **User-friendly**: No need for users to remember exact URL formats
+4. **Maintainability**: Centralized logic is easier to update and test
+
+## Migration Guide
+
+For developers extending the codebase:
+
+1. Import the utility function:
+ ```python
+ from ...utilities.url_utils import normalize_ollama_url
+ ```
+
+2. Apply normalization before using Ollama URLs:
+ ```python
+ raw_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
+ normalized_url = normalize_ollama_url(raw_url)
+ ```
+
+This ensures consistent URL handling throughout the application.
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 5bea9c3ad8..2b376ed1d1 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -4,7 +4,6 @@ import os
import platform
import subprocess
from typing import Any, Optional, Tuple
-from urllib.parse import urlparse # urlunparse removed if not used elsewhere
import requests
from flask import (
@@ -20,6 +19,7 @@ from flask_wtf.csrf import generate_csrf
from sqlalchemy.orm import Session
from ...utilities.db_utils import get_db_setting
+from ...utilities.url_utils import normalize_ollama_url
from ..database.models import Setting, SettingType
from ..services.settings_service import (
create_or_update_setting,
@@ -668,27 +668,10 @@ def api_get_available_models():
try:
current_app.logger.info("Attempting to connect to Ollama API")
- raw_base_url = get_db_setting("llm.ollama.url", "http://localhost:11434")
- parsed_url = urlparse(raw_base_url)
- if parsed_url.scheme and not parsed_url.netloc and parsed_url.path:
- # Corrects "scheme:hostname" to "scheme://hostname"
- scheme_to_use = parsed_url.scheme if parsed_url.scheme in ("http", "https") else "https"
- base_url = f"{scheme_to_use}://{parsed_url.path}"
- elif not parsed_url.scheme:
- # Handles "hostname" or "//hostname"
- temp_host_path = raw_base_url
- if raw_base_url.startswith("//"):
- temp_host_path = raw_base_url[2:]
-
- is_localhost = temp_host_path.startswith("localhost") or temp_host_path.startswith("127.0.0.1")
-
- if is_localhost:
- base_url = "http://" + temp_host_path
- else:
- base_url = "https://" + temp_host_path
- else:
- # Assumed to be well-formed (e.g., "http://hostname", "https://hostname") or other scheme.
- base_url = raw_base_url
+ raw_base_url = get_db_setting(
+ "llm.ollama.url", "http://localhost:11434"
+ )
+ base_url = normalize_ollama_url(raw_base_url)
ollama_response = requests.get(f"{base_url}/api/tags", timeout=5)
@@ -1294,28 +1277,11 @@ def check_ollama_status():
# Set a shorter timeout for the request
raw_base_url = os.getenv(
"OLLAMA_BASE_URL",
- get_db_setting("llm.ollama.url", "http://localhost:11434"), # Use db setting as fallback
+ get_db_setting(
+ "llm.ollama.url", "http://localhost:11434"
+ ), # Use db setting as fallback
)
- parsed_url = urlparse(raw_base_url)
- if parsed_url.scheme and not parsed_url.netloc and parsed_url.path:
- # Corrects "scheme:hostname" to "scheme://hostname"
- scheme_to_use = parsed_url.scheme if parsed_url.scheme in ("http", "https") else "https"
- base_url = f"{scheme_to_use}://{parsed_url.path}"
- elif not parsed_url.scheme:
- # Handles "hostname" or "//hostname"
- temp_host_path = raw_base_url
- if raw_base_url.startswith("//"):
- temp_host_path = raw_base_url[2:]
-
- is_localhost = temp_host_path.startswith("localhost") or temp_host_path.startswith("127.0.0.1")
-
- if is_localhost:
- base_url = "http://" + temp_host_path
- else:
- base_url = "https://" + temp_host_path
- else:
- # Assumed to be well-formed (e.g., "http://hostname", "https://hostname") or other scheme.
- base_url = raw_base_url
+ base_url = normalize_ollama_url(raw_base_url)
response = requests.get(f"{base_url}/api/version", timeout=2.0)
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_local.py b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
index 54f345f986..e52edc5e3d 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_local.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
@@ -1,5 +1,4 @@
import hashlib
-import hashlib
import json
import logging
import os
@@ -9,7 +8,6 @@ from concurrent.futures import ProcessPoolExecutor
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional
-from urllib.parse import urlparse
from faiss import IndexFlatL2
from langchain_community.docstore.in_memory import InMemoryDocstore
@@ -34,6 +32,7 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
from ...config import search_config
from ...utilities.db_utils import get_db_setting
+from ...utilities.url_utils import normalize_ollama_url
from ..search_engine_base import BaseSearchEngine
# Setup logging
@@ -174,17 +173,10 @@ class LocalEmbeddingManager:
raw_ollama_base_url = get_db_setting(
"llm.ollama.url", "http://localhost:11434"
)
- parsed_url = urlparse(raw_ollama_base_url)
- if not parsed_url.scheme:
- self.ollama_base_url = urlunparse(parsed_url._replace(scheme="https"))
- else:
- self.ollama_base_url = raw_ollama_base_url
+ self.ollama_base_url = normalize_ollama_url(raw_ollama_base_url)
else:
# Ensure scheme is present if ollama_base_url was passed in constructor
- parsed_url = urlparse(self.ollama_base_url)
- if not parsed_url.scheme:
- self.ollama_base_url = urlunparse(parsed_url._replace(scheme="https"))
-
+ self.ollama_base_url = normalize_ollama_url(self.ollama_base_url)
logger.info(
f"Initializing Ollama embeddings with model {self.embedding_model} and base_url {self.ollama_base_url}"
@@ -576,7 +568,7 @@ class LocalEmbeddingManager:
str(index_path),
self.embeddings,
allow_dangerous_deserialization=True,
- nomalize_L2=True,
+ normalize_L2=True,
)
except Exception as e:
logger.error(f"Error loading index for {folder_path}: {e}")
diff --git a/tests/test_url_utils.py b/tests/test_url_utils.py
new file mode 100644
index 0000000000..c53e409e1d
--- /dev/null
+++ b/tests/test_url_utils.py
@@ -0,0 +1,68 @@
+"""Test URL utility functions."""
+
+import pytest
+
+from local_deep_research.utilities.url_utils import normalize_ollama_url
+
+
+class TestNormalizeOllamaUrl:
+ """Test cases for the normalize_ollama_url function."""
+
+ def test_localhost_without_scheme(self):
+ """Test that localhost addresses get http:// prefix."""
+ assert normalize_ollama_url("localhost:11434") == "http://localhost:11434"
+ assert normalize_ollama_url("127.0.0.1:11434") == "http://127.0.0.1:11434"
+ assert normalize_ollama_url("[::1]:11434") == "http://[::1]:11434"
+ assert normalize_ollama_url("0.0.0.0:11434") == "http://0.0.0.0:11434"
+
+ def test_external_host_without_scheme(self):
+ """Test that external hosts get https:// prefix."""
+ assert normalize_ollama_url("example.com:11434") == "https://example.com:11434"
+ assert normalize_ollama_url("api.example.com") == "https://api.example.com"
+
+ def test_malformed_url_with_scheme(self):
+ """Test correction of malformed URLs like 'http:hostname'."""
+ assert normalize_ollama_url("http:localhost:11434") == "http://localhost:11434"
+ assert (
+ normalize_ollama_url("https:example.com:11434")
+ == "https://example.com:11434"
+ )
+
+ def test_well_formed_urls(self):
+ """Test that well-formed URLs are unchanged."""
+ assert (
+ normalize_ollama_url("http://localhost:11434") == "http://localhost:11434"
+ )
+ assert (
+ normalize_ollama_url("https://example.com:11434")
+ == "https://example.com:11434"
+ )
+ assert (
+ normalize_ollama_url("http://192.168.1.100:11434")
+ == "http://192.168.1.100:11434"
+ )
+
+ def test_urls_with_double_slash_prefix(self):
+ """Test URLs that start with //."""
+ assert normalize_ollama_url("//localhost:11434") == "http://localhost:11434"
+ assert (
+ normalize_ollama_url("//example.com:11434") == "https://example.com:11434"
+ )
+
+ def test_empty_or_none_url(self):
+ """Test handling of empty or None URLs."""
+ assert normalize_ollama_url("") == "http://localhost:11434"
+ assert normalize_ollama_url(None) == "http://localhost:11434"
+
+ def test_url_with_path(self):
+ """Test URLs with paths."""
+ assert (
+ normalize_ollama_url("localhost:11434/api") == "http://localhost:11434/api"
+ )
+ assert (
+ normalize_ollama_url("example.com/api/v1") == "https://example.com/api/v1"
+ )
+
+
+if __name__ == "__main__":
+ pytest.main([__file__, "-v"])
diff --git a/tests/test_url_utils_debug.py b/tests/test_url_utils_debug.py
new file mode 100644
index 0000000000..80d92d8696
--- /dev/null
+++ b/tests/test_url_utils_debug.py
@@ -0,0 +1,33 @@
+"""Debug test for URL utility functions."""
+
+from local_deep_research.utilities.url_utils import normalize_ollama_url
+
+
+def test_url_normalization():
+ """Test the normalize_ollama_url function."""
+
+ test_cases = [
+ ("localhost:11434", "http://localhost:11434"),
+ ("127.0.0.1:11434", "http://127.0.0.1:11434"),
+ ("example.com:11434", "https://example.com:11434"),
+ ("http:localhost:11434", "http://localhost:11434"),
+ ("https:example.com:11434", "https://example.com:11434"),
+ ("http://localhost:11434", "http://localhost:11434"),
+ ("https://example.com:11434", "https://example.com:11434"),
+ ("//localhost:11434", "http://localhost:11434"),
+ ("//example.com:11434", "https://example.com:11434"),
+ ("", "http://localhost:11434"),
+ ]
+
+ for input_url, expected in test_cases:
+ result = normalize_ollama_url(input_url)
+ if result != expected:
+ print(f"FAIL: Input: {input_url!r}")
+ print(f" Expected: {expected!r}")
+ print(f" Got: {result!r}")
+ else:
+ print(f"PASS: {input_url!r} -> {result!r}")
+
+
+if __name__ == "__main__":
+ test_url_normalization()
diff --git a/tests/test_url_utils_simple.py b/tests/test_url_utils_simple.py
new file mode 100644
index 0000000000..4da44c041b
--- /dev/null
+++ b/tests/test_url_utils_simple.py
@@ -0,0 +1,39 @@
+"""Simple test for URL utility functions without pytest."""
+
+from local_deep_research.utilities.url_utils import normalize_ollama_url
+
+
+def test_url_normalization():
+ """Test the normalize_ollama_url function."""
+
+ # Test localhost without scheme
+ assert normalize_ollama_url("localhost:11434") == "http://localhost:11434"
+ assert normalize_ollama_url("127.0.0.1:11434") == "http://127.0.0.1:11434"
+
+ # Test external host without scheme
+ assert normalize_ollama_url("example.com:11434") == "https://example.com:11434"
+
+ # Test malformed URL with scheme
+ assert normalize_ollama_url("http:localhost:11434") == "http://localhost:11434"
+ assert (
+ normalize_ollama_url("https:example.com:11434") == "https://example.com:11434"
+ )
+
+ # Test well-formed URLs
+ assert normalize_ollama_url("http://localhost:11434") == "http://localhost:11434"
+ assert (
+ normalize_ollama_url("https://example.com:11434") == "https://example.com:11434"
+ )
+
+ # Test URLs with double slash prefix
+ assert normalize_ollama_url("//localhost:11434") == "http://localhost:11434"
+ assert normalize_ollama_url("//example.com:11434") == "https://example.com:11434"
+
+ # Test empty URL
+ assert normalize_ollama_url("") == "http://localhost:11434"
+
+ print("All tests passed!")
+
+
+if __name__ == "__main__":
+ test_url_normalization()