~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
import asyncio
import threading
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
import httpx
import pytest
_SETTINGS = {
"embeddings.openai.base_url": None,
"embeddings.openai.dimensions": None,
}
class _FailingClient(httpx.Client):
def close(self) -> None:
raise RuntimeError("sync close failed")
def _build_owned_embeddings():
from local_deep_research.embeddings.providers.implementations.owned_openai_embeddings import (
OwnedOpenAIEmbeddings,
)
sync_client = httpx.Client()
async_client = httpx.AsyncClient()
embeddings = OwnedOpenAIEmbeddings(
model="text-embedding-3-small",
openai_api_key="test-key",
openai_api_base="http://localhost:1234/v1",
check_embedding_ctx_length=False,
http_client=sync_client,
http_async_client=async_client,
)
return embeddings, sync_client, async_client
class TestOwnedOpenAIEmbeddings:
@pytest.mark.asyncio
@pytest.mark.parametrize("mode", ["sync", "running-loop", "async"])
@pytest.mark.parametrize("failing", ["sync", "async"])
async def test_failed_cleanup_can_be_retried(self, mode, failing):
embeddings, sync_client, async_client = _build_owned_embeddings()
sync_close = sync_client.close
async_close = async_client.aclose
attempts = 0
def flaky_sync_close():
nonlocal attempts
attempts += 1
if attempts == 1:
raise RuntimeError("temporary cleanup failure")
sync_close()
async def flaky_async_close():
nonlocal attempts
attempts += 1
if attempts == 1:
raise RuntimeError("temporary cleanup failure")
await async_close()
async def close():
if mode == "async":
await embeddings.aclose()
elif mode == "sync":
await asyncio.to_thread(embeddings.close)
else:
embeddings.close()
target = sync_client if failing == "sync" else async_client
method = "close" if failing == "sync" else "aclose"
replacement = (
flaky_sync_close if failing == "sync" else flaky_async_close
)
try:
with patch.object(target, method, replacement):
await close()
assert not target.is_closed
await close()
assert sync_client.is_closed
assert async_client.is_closed
assert attempts == 2
finally:
sync_close()
await async_close()
def test_local_embedding_manager_close_closes_owned_clients(self):
from local_deep_research.web_search_engines.engines.local_embedding_manager import (
LocalEmbeddingManager,
)
embeddings, sync_client, async_client = _build_owned_embeddings()
manager = LocalEmbeddingManager()
manager._embeddings = embeddings
manager.close()
assert sync_client.is_closed is True
assert async_client.is_closed is True
@pytest.mark.asyncio
async def test_aclose_is_idempotent(self):
embeddings, sync_client, async_client = _build_owned_embeddings()
await embeddings.aclose()
await embeddings.aclose()
assert sync_client.is_closed is True
assert async_client.is_closed is True
@pytest.mark.asyncio
async def test_close_closes_clients_from_running_event_loop(self):
embeddings, sync_client, async_client = _build_owned_embeddings()
embeddings.close()
assert sync_client.is_closed is True
assert async_client.is_closed is True
@pytest.mark.asyncio
async def test_aclose_logs_client_close_error_and_closes_async_client(
self, loguru_caplog
):
from local_deep_research.embeddings.providers.implementations.owned_openai_embeddings import (
OwnedOpenAIEmbeddings,
)
async_client = httpx.AsyncClient()
embeddings = OwnedOpenAIEmbeddings(
model="text-embedding-3-small",
openai_api_key="test-key",
openai_api_base="http://localhost:1234/v1",
check_embedding_ctx_length=False,
http_client=_FailingClient(),
http_async_client=async_client,
)
with loguru_caplog.at_level("WARNING"):
await embeddings.aclose()
assert async_client.is_closed is True
assert (
"Failed to close OpenAI embeddings sync httpx client"
in loguru_caplog.text
)
@pytest.mark.asyncio
async def test_close_from_running_loop_bounds_cleanup_thread(
self, loguru_caplog
):
embeddings, _, _ = _build_owned_embeddings()
started = threading.Event()
release = threading.Event()
captured = {}
async def slow_aclose(http_client, http_async_client):
started.set()
await asyncio.get_running_loop().run_in_executor(None, release.wait)
class _ShortJoinThread(threading.Thread):
def join(self, timeout=None):
captured["timeout"] = timeout
return super().join(timeout=0.2)
try:
with loguru_caplog.at_level("WARNING"):
with patch(
"local_deep_research.embeddings.providers.implementations."
"owned_openai_embeddings._aclose_http_clients",
slow_aclose,
):
with patch(
"local_deep_research.embeddings.providers.implementations."
"owned_openai_embeddings.threading",
SimpleNamespace(Thread=_ShortJoinThread),
):
embeddings.close()
finally:
release.set()
assert started.is_set()
assert captured["timeout"] == 5
assert (
"OpenAI embeddings async httpx close exceeded 5s"
in loguru_caplog.text
)
await embeddings.aclose()
class TestOpenAIEmbeddingsProviderRollback:
def test_async_client_construction_failure_closes_sync_client(self):
from local_deep_research.embeddings.providers.implementations.openai import (
OpenAIEmbeddingsProvider,
)
sync_client = MagicMock()
with patch("openai.DefaultHttpxClient", return_value=sync_client):
with patch(
"openai.DefaultAsyncHttpxClient",
side_effect=RuntimeError("async client construction failed"),
):
with pytest.raises(
RuntimeError, match="async client construction failed"
):
OpenAIEmbeddingsProvider.create_embeddings(
model="text-embedding-3-small",
api_key="test-key",
base_url="http://localhost:1234/v1",
settings_snapshot=_SETTINGS,
)
sync_client.close.assert_called_once_with()
def test_async_client_failure_preserves_original_error_when_cleanup_fails(
self, loguru_caplog
):
from local_deep_research.embeddings.providers.implementations.openai import (
OpenAIEmbeddingsProvider,
)
sync_client = MagicMock()
sync_client.close.side_effect = RuntimeError("sync cleanup failed")
with loguru_caplog.at_level("WARNING"):
with patch("openai.DefaultHttpxClient", return_value=sync_client):
with patch(
"openai.DefaultAsyncHttpxClient",
side_effect=RuntimeError(
"async client construction failed"
),
):
with pytest.raises(
RuntimeError, match="async client construction failed"
):
OpenAIEmbeddingsProvider.create_embeddings(
model="text-embedding-3-small",
api_key="test-key",
base_url="http://localhost:1234/v1",
settings_snapshot=_SETTINGS,
)
assert (
"Failed to close OpenAI embeddings sync httpx client"
in loguru_caplog.text
)
def test_embeddings_construction_failure_closes_both_clients(self):
from local_deep_research.embeddings.providers.implementations.openai import (
OpenAIEmbeddingsProvider,
)
sync_client = MagicMock()
async_client = AsyncMock()
with patch("openai.DefaultHttpxClient", return_value=sync_client):
with patch(
"openai.DefaultAsyncHttpxClient", return_value=async_client
):
with patch(
"local_deep_research.embeddings.providers.implementations."
"owned_openai_embeddings.OwnedOpenAIEmbeddings",
side_effect=RuntimeError("embeddings construction failed"),
):
with pytest.raises(
RuntimeError, match="embeddings construction failed"
):
OpenAIEmbeddingsProvider.create_embeddings(
model="text-embedding-3-small",
api_key="test-key",
base_url="http://localhost:1234/v1",
settings_snapshot=_SETTINGS,
)
sync_client.close.assert_called_once_with()
async_client.aclose.assert_awaited_once_with()
def test_embeddings_failure_preserves_original_error_when_cleanup_fails(
self, loguru_caplog
):
from local_deep_research.embeddings.providers.implementations.openai import (
OpenAIEmbeddingsProvider,
)
sync_client = MagicMock()
sync_client.close.side_effect = RuntimeError("sync cleanup failed")
async_client = AsyncMock()
async_client.aclose.side_effect = RuntimeError("async cleanup failed")
with loguru_caplog.at_level("WARNING"):
with patch("openai.DefaultHttpxClient", return_value=sync_client):
with patch(
"openai.DefaultAsyncHttpxClient", return_value=async_client
):
with patch(
"local_deep_research.embeddings.providers.implementations."
"owned_openai_embeddings.OwnedOpenAIEmbeddings",
side_effect=RuntimeError(
"embeddings construction failed"
),
):
with pytest.raises(
RuntimeError, match="embeddings construction failed"
):
OpenAIEmbeddingsProvider.create_embeddings(
model="text-embedding-3-small",
api_key="test-key",
base_url="http://localhost:1234/v1",
settings_snapshot=_SETTINGS,
)
assert (
"Failed to close OpenAI embeddings sync httpx client"
in loguru_caplog.text
)
assert (
"Failed to close OpenAI embeddings async httpx client"
in loguru_caplog.text
)