~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
from typing import Any
from langchain_core.callbacks import BaseCallbackManager, CallbackManager
from langchain_core.language_models import BaseChatModel, BaseLanguageModel
from langchain_core.messages import AIMessage
from langchain_core.runnables import RunnableBinding, ensure_config
from langchain_core.runnables.config import merge_configs
from loguru import logger
from ..llm import get_llm_from_registry, is_llm_registered
from ..security.log_sanitizer import sanitize_for_log, scrub_error
from ..security.secure_logging import log_exception_type
from ..settings.exceptions import SettingsValueError
from ..utilities.search_utilities import remove_think_tags
# Import providers module to trigger auto-discovery. get_llm() has no
# fallback construction path: if this import fails (e.g. a broken
# langchain install), the module must fail loudly here rather than start
# with an empty registry and confusing per-call errors.
from ..llm.providers import discover_providers # noqa: F401
from ..llm.providers.auto_discovery import is_builtin_provider_factory
from ..llm.providers.base import normalize_provider
from .openai_endpoint_binding import (
OPENAI_ENDPOINT_KEY_OWNER_MARKER,
bind_openai_endpoint_override,
)
from .thread_settings import get_setting_from_snapshot
class LLMNotConfiguredError(ValueError):
"""``get_llm`` could not build an LLM because none is CONFIGURED.
Raised for the "the user has not set this up yet" failures — no
provider, an unknown provider, no ``llm.model``, a provider that
auto-discovery did not register. It is deliberately a ``ValueError``
subclass so every existing ``except ValueError`` around ``get_llm``
keeps behaving exactly as before.
It exists so an HTTP boundary can tell a configuration failure apart
from a request-validation failure *by type* — the alternative is
matching on the exception text, which is what the fixed-message
boundary rule forbids. Without it, a route that maps ``ValueError`` to
"your request was invalid" tells a fresh install with no ``llm.model``
that its perfectly valid request was malformed.
"""
# The fixed text a route may show for the above. A constant, never
# ``str(exc)``: the exception messages are setup guidance written for a log
# reader, not a boundary contract, and a future raise site could put an
# endpoint URL or a settings path in one.
LLM_NOT_CONFIGURED_CLIENT_MESSAGE = (
"LLM is not configured. Open Settings and choose a provider and model."
)
def get_selected_llm_provider(settings_snapshot=None):
return normalize_provider(
get_setting_from_snapshot(
"llm.provider", "ollama", settings_snapshot=settings_snapshot
)
)
def _get_context_window_for_provider(provider_type, settings_snapshot=None):
"""Resolve the context window size for a provider.
Thin wrapper around the canonical
``llm/providers/_helpers.get_context_window_for_provider`` so the
provider ``create_llm`` path and this path share a single source of
truth (the two were previously identical copies). Kept as a named
function because ``get_llm``/``wrap_llm_without_think_tags`` and tests
reference it by name and signature.
NOTE: the helper reads settings through
``thread_settings.get_setting_from_snapshot`` (a function-local import),
so tests exercising context-window resolution must patch
``...config.thread_settings.get_setting_from_snapshot`` rather than
``...config.llm_config.get_setting_from_snapshot``.
Returns:
int or None: The context window size, or None for unrestricted cloud providers.
"""
from ..llm.providers._helpers import get_context_window_for_provider
return get_context_window_for_provider(
provider_type, settings_snapshot=settings_snapshot
)
def get_llm(
model_name=None,
temperature=None,
provider=None,
openai_endpoint_url=None,
research_id=None,
research_context=None,
settings_snapshot=None,
username=None,
openai_endpoint_api_key=None,
):
"""
Get LLM instance based on model name and provider.
Args:
model_name: Name of the model to use (if None, uses database setting)
temperature: Model temperature (if None, uses database setting)
provider: Provider to use (if None, uses database setting)
openai_endpoint_url: Per-call URL override for the
``openai_endpoint`` provider. When omitted, the provider uses
the ``llm.openai_endpoint.url`` setting. The stored
``llm.openai_endpoint.api_key`` is sent to the override only
when both URLs share an origin (scheme, host, port), or when the
override is on the origin of a set
``LDR_LLM_OPENAI_ENDPOINT_URL``. A key from
``LDR_LLM_OPENAI_ENDPOINT_API_KEY`` is sent only to the env
URL's origin, or with no env URL to the saved URL's. Otherwise
the call carries no stored key unless
``openai_endpoint_api_key`` supplies one (see
config/openai_endpoint_binding.py).
research_id: Optional research ID for token tracking
research_context: Optional research context for enhanced token tracking
username: Requesting user, used to resolve per-user registered LLMs.
When omitted, falls back to the ``_username`` in the settings
snapshot; ``None`` resolves shared/built-in providers only.
openai_endpoint_api_key: Per-call API key for the
``openai_endpoint`` provider, bound to this call's endpoint.
Takes precedence over the stored key.
Returns:
A LangChain LLM instance with automatic think-tag removal
"""
# Resolve the effective username for per-user registry lookups. An
# explicit argument wins; otherwise fall back to the ``_username`` the
# settings snapshot carries. None resolves the shared namespace (which
# holds the auto-discovered built-in providers) only.
if username is None:
from ..search_system import username_from_snapshot
username = username_from_snapshot(settings_snapshot)
# Use database values for parameters if not provided
if model_name is None:
model_name = get_setting_from_snapshot(
"llm.model", "", settings_snapshot=settings_snapshot
)
if temperature is None:
temperature = get_setting_from_snapshot(
"llm.temperature", 0.7, settings_snapshot=settings_snapshot
)
if provider is None:
provider = get_setting_from_snapshot(
"llm.provider", "ollama", settings_snapshot=settings_snapshot
)
# Clean model name: remove quotes and extra whitespace
if model_name:
model_name = model_name.strip().strip("\"'").strip()
# Normalize provider: strip surrounding quotes/whitespace and lowercase
# (the same canonical form the route checks use).
provider = normalize_provider(provider)
had_settings_snapshot = settings_snapshot is not None
if openai_endpoint_api_key is not None and not isinstance(
openai_endpoint_api_key, str
):
raise ValueError("openai_endpoint_api_key must be a string")
explicit_endpoint_key = (openai_endpoint_api_key or "").strip()
# The endpoint override must reach both the egress-policy gate and the
# provider factory. Overlay it onto a per-call copy so those paths share
# one effective value without mutating the caller's settings snapshot.
#
# The API key is bound in the same copy. The provider reads its key
# from ``llm.openai_endpoint.api_key``, which belongs to the configured
# ``llm.openai_endpoint.url``; an override pointing at another origin
# must not inherit it (see config/openai_endpoint_binding.py). A
# snapshot whose URL was overridden earlier (runtime overlay) carries an
# owner marker and is re-checked here, so a key restored by a later
# environment-override pass cannot follow the overridden URL.
if provider == "openai_endpoint" and (
openai_endpoint_url is not None
or explicit_endpoint_key
or (
isinstance(settings_snapshot, dict)
and OPENAI_ENDPOINT_KEY_OWNER_MARKER in settings_snapshot
)
):
override_url = None
if openai_endpoint_url is not None:
if (
not isinstance(openai_endpoint_url, str)
or not openai_endpoint_url.strip()
):
raise ValueError(
"openai_endpoint_url must be a non-empty string"
)
override_url = openai_endpoint_url.strip()
settings_snapshot = dict(settings_snapshot or {})
bind_openai_endpoint_override(
settings_snapshot, override_url, explicit_endpoint_key
)
# Egress policy PEP for LLM endpoints. Fires here (before the registered-
# LLM dispatch) because all built-in providers are auto-registered via
# discover_providers(), so the registered branch handles every real LLM
# call.
#
# Two paths: snapshot-present runs the full PEP; snapshot-absent runs
# an allow-list check so background helpers / scaffolding paths cannot
# silently instantiate a cloud LLM. The allow-list (known-local) is
# deliberately tight — any provider not in it (including ambiguous
# ones like ``openai_endpoint`` and future cloud providers) fails
# closed instead of bypassing the PEP.
if provider:
from ..security.egress.policy import (
Decision,
EgressContext,
EgressScope,
PolicyDeniedError,
_LOCAL_DEFAULT_LLM_PROVIDERS,
_is_user_registered_llm,
context_from_snapshot,
evaluate_llm_endpoint,
resolve_run_primary_engine,
)
if not had_settings_snapshot:
if (
provider == "openai_endpoint"
and openai_endpoint_url is not None
):
# A per-call endpoint is enough to classify this otherwise
# snapshot-less provider. Preserve the fail-closed contract:
# only a local endpoint may proceed without a policy snapshot.
ctx = EgressContext(
scope=EgressScope.PRIVATE_ONLY,
primary_engine="snapshotless-explicit-endpoint",
require_local_llm=True,
require_local_embeddings=True,
)
decision = evaluate_llm_endpoint(
provider,
ctx,
settings_snapshot=settings_snapshot,
username=username,
)
if not decision.allowed:
logger.bind(policy_audit=True).warning(
"Snapshot-less explicit LLM endpoint denied",
provider=provider,
reason=decision.reason,
)
raise PolicyDeniedError(decision, target=provider)
# User-registered in-process LLMs are exempt here for the same
# reason evaluate_llm_endpoint allows them: no endpoint to
# classify, operator-injected, audit-hook backstopped.
elif provider not in _LOCAL_DEFAULT_LLM_PROVIDERS and not (
_is_user_registered_llm(provider, username=username)
):
logger.bind(policy_audit=True).warning(
"LLM constructed without policy snapshot; refusing "
"non-local provider",
provider=provider,
)
raise PolicyDeniedError(
Decision(False, "no_snapshot_for_provider"),
target=provider,
)
else:
try:
# Derive the run's primary the SAME way the search-engine
# factory does (single source of truth), instead of the old
# ``search.tool`` + searxng fallback. That fallback was a
# fail-OPEN: a missing/blank primary defaulted to searxng ->
# ADAPTIVE -> PUBLIC_ONLY -> require_local_llm stayed False ->
# the endpoint check below was skipped, admitting a CLOUD LLM
# for a run whose actual posture was private. resolve_run_
# primary_engine raises on a missing/invalid primary, which we
# treat as a hard stop here.
primary_engine = resolve_run_primary_engine(settings_snapshot)
# Pass username so a per-user private retriever set as the
# run's primary is classified against the caller's own
# namespace. Without it ADAPTIVE can't see the retriever
# (shared namespace only), resolves to the permissive BOTH,
# leaves require_local_llm False, and admits a cloud LLM for a
# run over the user's private corpus (fail-open).
ctx = context_from_snapshot(
settings_snapshot, primary_engine, username=username
)
except ValueError as exc:
# No configured primary, or an invalid policy config. Fail
# closed: previously a missing primary silently fell back to
# searxng/PUBLIC_ONLY and skipped the LLM endpoint check
# entirely, opening a cloud-LLM bypass under the very
# configuration the user asked to be strict about.
logger.bind(policy_audit=True).warning(
"no/invalid egress policy primary; refusing LLM",
provider=provider,
reason=str(exc),
)
raise PolicyDeniedError(
Decision(False, "invalid_policy_config"),
target=provider,
) from exc
if ctx is not None and ctx.require_local_llm:
decision = evaluate_llm_endpoint(
provider,
ctx,
settings_snapshot=settings_snapshot,
username=username,
)
if not decision.allowed:
logger.bind(policy_audit=True).warning(
"LLM endpoint denied by egress policy",
provider=provider,
reason=decision.reason,
)
raise PolicyDeniedError(decision, target=provider)
# Check if this is a registered custom LLM first. Resolve against the
# caller's own namespace before the shared/built-in providers so one
# user's registration can't shadow a built-in for anyone else.
if provider and is_llm_registered(provider, username=username):
logger.info(f"Using registered custom LLM: {provider}")
custom_llm = get_llm_from_registry(provider, username=username)
# Check if it's a callable (factory function) or a BaseChatModel instance
if callable(custom_llm) and not isinstance(custom_llm, BaseChatModel):
# It's a callable (factory function), call it with parameters
try:
llm_instance = custom_llm(
model_name=model_name,
temperature=temperature,
settings_snapshot=settings_snapshot,
)
except TypeError as e:
# Re-raise TypeError with better message
raise TypeError(
f"Registered LLM factory '{provider}' has invalid signature. "
f"Factory functions must accept 'model_name', 'temperature', and 'settings_snapshot' parameters. "
f"Error: {e}"
)
# Validate the result is a BaseChatModel
if not isinstance(llm_instance, BaseChatModel):
raise ValueError(
f"Factory function for {provider} must return a BaseChatModel instance, "
f"got {type(llm_instance).__name__}"
)
elif isinstance(custom_llm, BaseChatModel):
# It's already a proper LLM instance, use it directly
llm_instance = custom_llm
else:
raise ValueError(
f"Registered LLM {provider} must be either a BaseChatModel instance "
f"or a callable factory function. Got: {type(custom_llm).__name__}"
)
return wrap_llm_without_think_tags(
llm_instance,
research_id=research_id,
provider=provider,
research_context=research_context,
settings_snapshot=settings_snapshot,
# LDR closes only a model it knows it built: a built-in
# provider's create_llm builds a new one on every call. A
# registered instance, or whatever a registered factory
# returns, can be serving other researches (a factory can
# return one cached or singleton model), so it stays with the
# code that registered it.
owns_llm=is_builtin_provider_factory(custom_llm),
)
# Validate the provider against the auto-discovered set — NOT a hardcoded
# list. Auto-discovery (discover_providers, run at module import) registers
# every llm/providers/implementations/*.py class, and the
# is_llm_registered() check above already serves them, so the registry /
# discovery IS the single source of truth for "valid provider".
#
# We deliberately do not keep a separate VALID_PROVIDERS constant: it was a
# third copy of the provider list (besides the implementations directory
# and the registry) and it silently drifted from auto-discovery (xai,
# ionos and deepseek were valid+registered yet missing from it). Deriving
# the set from discovery here means it can never drift. ('none' is the
# explicit "unset" sentinel, handled by the guard further down.)
from ..llm.providers import get_discovered_provider_options
valid_providers = {
normalize_provider(option["value"])
for option in get_discovered_provider_options()
} | {"none"}
if provider not in valid_providers:
# ``provider`` is a user-settable value (``llm.provider``) reaching a
# log sink. The process-wide loguru patcher (``_sanitize_record`` in
# utilities/log_utils.py, via ``redact_log_record``) already strips
# control characters (\x00-\x1f, \x7f) from every record's message
# and redacts credentials, so ``sanitize_for_log`` isn't newline/ANSI
# defence here -- it adds the length cap plus the ``None`` guard
# below. The exception text is a different audience (it never
# reaches a client -- boundaries answer with
# LLM_NOT_CONFIGURED_CLIENT_MESSAGE) and is left intact so
# `match="Invalid provider: <name>"` assertions and the operator's
# "which value did I actually set?" question survive.
# ``str()`` first: ``normalize_provider`` returns None for an
# empty/whitespace-only ``llm.provider``, and that None reaches here
# (the ``if provider:`` gates above all skipped). ``sanitize_for_log``
# takes a str, so the bare call would turn this configuration error
# into a TypeError. ``str(None)`` renders "None", exactly what the
# previous f-string logged.
logger.error(
f"Invalid provider in settings: {sanitize_for_log(str(provider))}"
)
raise LLMNotConfiguredError(
f"Invalid provider: {provider}. "
f"Must be one of: {sorted(valid_providers)}"
)
# Require an explicit model for built-in providers. Mirrors the
# API-key-not-configured pattern in openai_base.py and the URL-not-
# configured pattern in providers/implementations/ollama.py: no silent
# substitution to a hardcoded default model.
if not model_name or not model_name.strip():
logger.error("llm.model is not configured (empty/None after lookup)")
raise LLMNotConfiguredError(
"LLM model not configured. Please open Settings, choose an LLM "
"provider, and select a model name (e.g. 'gpt-4o-mini' for "
"OpenAI, 'claude-3-5-sonnet-20241022' for Anthropic, "
"'llama3.1:8b' for Ollama). The 'llm.model' setting is required."
)
logger.info(
f"Getting LLM with model: {model_name}, temperature: {temperature}, provider: {provider}"
)
# Set context_limit on research_context for overflow detection. The
# actual max_tokens calculation lives in each provider's create_llm
# (via providers/_helpers.compute_max_tokens) so the cap is consistent
# across the live registered-LLM path.
context_window_size = _get_context_window_for_provider(
provider, settings_snapshot
)
if research_context and context_window_size:
research_context["context_limit"] = context_window_size
logger.info(
f"Set context_limit={context_window_size} in research_context"
)
else:
logger.debug(
f"Context limit not set: research_context={bool(research_context)}, "
f"context_window_size={context_window_size}"
)
# Reaching here means the registered-LLM branch above did NOT fire,
# which is unusual — auto-discovery normally registers all 6+ built-in
# providers (anthropic, openai, openai_endpoint, ollama, lmstudio,
# llamacpp + xai/ionos/openrouter via OpenAICompatibleProvider) at
# import time. Two specific guards preserve the user-facing error
# messages for known-bad cases.
if provider == "none":
raise LLMNotConfiguredError(
"No LLM provider configured. Please set llm.provider in settings "
"to a valid provider (e.g., 'ollama', 'openai', 'anthropic')."
)
raise LLMNotConfiguredError(
f"Provider '{provider}' was not registered by auto-discovery. "
f"This usually indicates an import error during startup — check the "
f"logs for 'Error loading provider from <module>' messages. "
f"Note that clear_llm_registry()/unregister_llm() also remove "
f"built-in providers; discover_providers(force_refresh=True) "
f"restores them."
)
def _log_llm_error(error: Exception) -> None:
"""Log an LLM call failure with credential redaction."""
safe_msg = scrub_error(error)
logger.warning(f"LLM Request - Failed with error: {safe_msg}")
def _merge_callbacks(existing, added, *, inherit: bool = True):
"""Return a callback manager holding *existing* plus *added*.
*existing* is never changed. It holds a call's callbacks: the ones its
caller passed, or the ones it inherits from an enclosing run such as a
LangGraph node. A caller can pass one handler list or
``BaseCallbackManager`` to many calls, and an enclosing run's manager
serves all of its child runs, so adding to either in place could make
those other calls fire this research's token callback too, writing
their usage under this research's user and research id.
*existing* stays inheritable. With ``inherit=False`` the added handlers
see only the run the result is passed to, the way a model's own
``callbacks`` field behaves, and child runs do not inherit them.
The result is a manager even when *existing* is a list. A
``RunnableBinding`` merges a call's callbacks with the ones it binds,
which the call's callbacks can already hold (see
``_with_research_callbacks``). LangChain's ``merge_configs`` adds each
handler once when either side is a manager, but joins two lists with
every handler from both.
"""
added = list(added)
if isinstance(existing, BaseCallbackManager):
merged = existing.copy()
if merged is existing:
raise TypeError("A callback manager must support isolated copies")
else:
inherited = list(existing or [])
merged = CallbackManager(
handlers=inherited, inheritable_handlers=list(inherited)
)
for handler in added:
merged.add_handler(handler, inherit=inherit)
return merged
def _config_receiver(runnable):
"""Return the runnable that receives the config passed to *runnable*.
LDR's rate-limiting wrapper hands each call's arguments, the config
included, unchanged to the runnable it wraps.
"""
seen = set()
while (
getattr(type(runnable), "_ldr_forwards_call_arguments", False)
and id(runnable) not in seen
):
seen.add(id(runnable))
runnable = runnable.base_llm
return runnable
def _starts_the_model_run(runnable) -> bool:
"""Whether the config passed to *runnable* starts the model's own run.
A model starts its run with the config it is called with, and a
``RunnableBinding``, which ``bind``, ``bind_tools`` and ``with_config``
return for a standard model, passes its merged config on to the
runnable it binds. Anything else is taken to start a run of its own
and run the model as a child of that run, as the chain that
``with_structured_output`` returns does, or a chain like
``prompt | self`` that a custom ``bind_tools`` returns.
"""
while isinstance(runnable, RunnableBinding):
runnable = runnable.bound
return isinstance(runnable, BaseLanguageModel)
class ProcessingLLMWrapper:
# Token callbacks of the research this wrapper serves, passed with each
# call instead of stored on the model; see _with_research_callbacks.
# The class default keeps lookups away from __getattr__, which would
# otherwise ask the model.
_ldr_research_callbacks: tuple = ()
def __init__(
self,
base_llm,
*,
owns_llm: bool = True,
research_id=None,
provider=None,
settings_snapshot=None,
structured_output: bool = False,
research_callbacks=(),
):
self.base_llm = base_llm
self._owns_llm = owns_llm
self._ldr_research_callbacks = tuple(research_callbacks)
# Cancellation scope: explicit id wins, otherwise the thread's
# search context is consulted per call (see _effective_research_id).
# Stored under both names — ``research_id`` for readability and
# ``_ldr_research_id`` for the shared bind helper in
# llm/cancellation.py (which propagates through nested wrappers).
self.research_id = research_id
self._ldr_research_id = research_id
# Token-accounting gate: the stream-under-invoke path silently
# converts every scoped invoke into a streaming request. For the
# ``openai_endpoint`` provider that loses token/cost metrics unless
# ``llm.openai_endpoint.stream_usage`` is enabled (custom gateways
# omit usage on streams, or reject stream_options outright).
# The provider + snapshot let _stream_cancel_allowed() make that
# tradeoff a decision instead of an accident.
self._provider = provider
self._settings_snapshot = settings_snapshot
# Structured-output contract: a wrapper created via
# with_structured_output() wraps a Runnable whose invoke/stream
# yield dicts/Pydantic models, not AIMessageChunks. Reassembling
# those through _combine_stream_chunks would corrupt them into an
# AIMessage holding str(dict). Such wrappers must use the direct
# invoke path (pre/post termination checks still apply).
# A bound wrapper must not outlive its run: the id is resolved per
# call so re-binding for the next run takes effect without
# reconstructing the LLM, and a stale binding is only ever used
# for a fail-fast/post-check, never to route into another run.
self._ldr_structured_output = structured_output
@staticmethod
def _normalize_response(response: Any) -> Any:
"""Strip <think> tags and normalize the response shape.
This is the SINGLE authorized place that strips <think> tags from a
fresh LLM response. Every LLM from get_llm() is wrapped here, so
downstream code can use ``response.content`` directly — do NOT add
per-site ``remove_think_tags`` / ``get_llm_response_text`` calls on
fresh ``invoke``/``ainvoke`` results; they are redundant and hide
bugs. (Exceptions: ``.stream()``/``.astream()`` pass chunks on
unstripped, Runnable transforms other than ``bind_tools``,
``with_structured_output``, ``bind`` and ``with_config`` still
bypass this wrapper via ``__getattr__``, and injected/unwrapped LLMs
are not wrapped at all — those may still need explicit handling.
Those four are NOT exceptions: they re-wrap so the stripper survives
— see the ``bind_tools`` override below and #4804.)
A message keeps its object identity (only ``.content`` is rewritten,
so ``additional_kwargs``/``reasoning_content``/``tool_calls`` survive).
A bare-string return (some providers/wrappers) is wrapped into an
``AIMessage`` so callers can always rely on ``.content``. Anything
else is passed through unchanged.
Only *string* ``.content`` is stripped: ``remove_think_tags`` is
text-only, and the ``<think>...</think>`` artifact is only ever
emitted as plain text by some local models. Non-string content
(e.g. provider content-block lists like Anthropic's, or ``None``)
is passed through untouched — running the regex on it would raise
``TypeError`` or corrupt the structured content.
"""
if hasattr(response, "content"):
if isinstance(response.content, str):
response.content = remove_think_tags(response.content)
elif isinstance(response, str):
response = AIMessage(content=remove_think_tags(response))
return response
@staticmethod
def _log_llm_error(error: Exception) -> None:
_log_llm_error(error)
def _with_research_callbacks(self, args, kwargs):
"""Return the call arguments with this research's callbacks added.
The callbacks go into the call's ``RunnableConfig`` (positional
argument two or ``config=``), so the model is invoked as it is and
never stores them. Any model can serve other researches and users:
a registered instance does, and so does a model that a registered
factory returns more than once, such as a cached or singleton
model.
Only the callbacks change: every other config key resolves as it
does for the same call without this wrapper. The config passed on
holds the caller's own keys rather than everything the call
inherits, plus the callbacks the call would use anyway: the
caller's, or those of an enclosing run such as a LangGraph node.
A ``RunnableBinding``, such as the result of ``with_config``, lets
its bound config win over an inherited one only when it is called
without a config. For a binding, the config passed on is therefore
its bound config already merged with the call's, as the binding
merges them, so the binding merging its bound config in again gives
the same values.
The handlers are not inheritable when that config starts the
model's own run (see ``_starts_the_model_run``), which matches a
model's own ``callbacks`` field: a run the model starts inside its
own run is not recorded as well. When the config starts a chain
instead, such as under ``with_structured_output`` or a custom
``bind_tools`` that returns ``prompt | self``, the model call is a
child run, so the handlers are inheritable. If the model itself
invokes another runnable during that call, the handlers see that
inner run too and record its tokens as well.
Only the wrapper's own call methods add the callbacks: ``invoke``,
``ainvoke``, ``stream``, ``astream``, ``batch`` and ``abatch``. The
wrappers that ``bind_tools``, ``with_structured_output``, ``bind``
and ``with_config`` return have the same methods and callbacks. A
call that leaves the wrapper through ``__getattr__`` reaches the
model without them, so its tokens are not recorded for the
research. That covers the other Runnable transforms, such as
``with_retry`` and ``with_fallbacks``, and methods such as
``generate`` and ``astream_events``.
"""
if not self._ldr_research_callbacks:
return args, kwargs
positional = len(args) > 1
explicit = args[1] if positional else kwargs.get("config")
receiver = _config_receiver(self.base_llm)
if isinstance(receiver, RunnableBinding):
config = merge_configs(receiver.config, explicit)
else:
config = dict(explicit or {})
config["callbacks"] = _merge_callbacks(
ensure_config(config).get("callbacks"),
self._ldr_research_callbacks,
inherit=not _starts_the_model_run(receiver),
)
if positional:
return (args[0], config, *args[2:]), kwargs
return args, {**kwargs, "config": config}
def _effective_research_id(self):
"""Resolve the cancellation scope for this call.
Precedence: explicit ctor id > ``_ldr_research_id`` binding >
current thread's search context. Consulted per call so a late
``bind_llm_to_research`` (or context propagation into pool
threads) takes effect without reconstructing the LLM.
"""
from ..llm.cancellation import effective_research_id
return effective_research_id(self, self.research_id)
def _check_terminated(self, research_id=None):
"""Raise ``ResearchTerminatedException`` if Stop was requested."""
from ..llm.cancellation import raise_if_terminated
rid = (
research_id
if research_id is not None
else self._effective_research_id()
)
if rid:
raise_if_terminated(rid)
def _walk_base_chain(self):
"""Yield self.base_llm and each nested ``base_llm`` once.
Covers ``Processing -> RateLimited -> ChatModel`` without
recursing (each level is a plain attribute read). Cycle-safe:
a mis-wired test double that points at itself terminates.
"""
seen = set()
cur = getattr(self, "base_llm", None)
while cur is not None and id(cur) not in seen:
seen.add(id(cur))
yield cur
try:
cur = getattr(cur, "base_llm", None)
except Exception:
break
def _underlying_stream_usage(self) -> bool:
"""True if any wrapped model opts into usage on streams.
Covers ``ChatOpenAI(stream_usage=True)`` and equivalents that
stash ``stream_options.include_usage`` in ``model_kwargs``.
"""
for base in self._walk_base_chain():
try:
if getattr(base, "stream_usage", None):
return True
mk = getattr(base, "model_kwargs", None)
if isinstance(mk, dict):
so = mk.get("stream_options", {})
if isinstance(so, dict) and so.get("include_usage"):
return True
except Exception:
continue
return False
def _underlying_disables_streaming(self) -> bool:
"""True if any wrapped model forbids streaming."""
for base in self._walk_base_chain():
try:
if getattr(base, "disable_streaming", False):
return True
except Exception:
continue
return False
def _stream_cancel_allowed(self) -> bool:
"""Whether invoke may stream under the hood for cancellation.
Two gates, both conservative (False keeps the direct invoke path
with pre/post termination checks, so Stop still aborts between
calls — only mid-generation abort is lost):
1. Structured-output wrappers never stream: their chunks are
dicts/Pydantic models, and reassembly would corrupt them into
``AIMessage(str(dict))``.
2. ``openai_endpoint`` without usage-on-stream never streams:
the reassembled message would carry no ``usage_metadata`` and
token/cost metrics would silently go to zero. Opt in via the
``llm.openai_endpoint.stream_usage`` setting (or an underlying
model built with ``stream_usage=True``). Other providers stream
because their streamed chunks preserve usage via
``usage_metadata``/``response_metadata``.
"""
if getattr(self, "_ldr_structured_output", False):
return False
if self._underlying_disables_streaming():
return False
provider = getattr(self, "_provider", None)
try:
from ..llm.providers.base import normalize_provider
provider = normalize_provider(provider) if provider else None
except Exception:
logger.debug(
"Provider normalization failed; treating as unknown provider",
exc_info=True,
)
if provider == "openai_endpoint":
try:
from .thread_settings import get_setting_from_snapshot
snapshot = getattr(self, "_settings_snapshot", None)
if get_setting_from_snapshot(
"llm.openai_endpoint.stream_usage",
False,
settings_snapshot=snapshot,
):
return True
except SettingsValueError:
raise
except Exception:
logger.debug(
"stream_usage setting lookup failed; "
"falling back to model capability",
exc_info=True,
)
# Model-level opt-in covers callers that built ChatOpenAI with
# stream_usage=True but have no settings context on this thread.
if self._underlying_stream_usage():
return True
return False
# Unknown provider but recognisably a ChatOpenAI-style model with
# a custom base_url and no usage-on-stream: same zero-metrics trap
# as openai_endpoint (e.g. thread-context fallback where the wrapper
# was built without a provider label). Be conservative.
for base in self._walk_base_chain():
try:
if hasattr(base, "stream_usage") and hasattr(
base, "openai_api_base"
):
if getattr(base, "stream_usage", None):
return True
if getattr(base, "openai_api_base", None):
return False
# Default OpenAI endpoint without an explicit opt-in:
# the validator enables stream_usage for the default
# URL, so a falsy value here is unexpected — fall
# through to the safe default below.
return False
except Exception:
continue
return True
@staticmethod
def _is_structured_chunk(chunk: Any) -> bool:
"""True for structured-output chunks (dicts/Pydantic, no .content)."""
if isinstance(chunk, dict):
return True
if isinstance(chunk, str):
return False
if hasattr(chunk, "content"):
return False
try:
from pydantic import BaseModel as _PydanticBaseModel
if isinstance(chunk, _PydanticBaseModel):
return True
except Exception:
logger.debug(
"Pydantic structured-chunk check failed; treating as unstructured",
exc_info=True,
)
return False
@staticmethod
def _combine_stream_chunks(chunks):
"""Combine streamed chunks into a single response object.
``AIMessageChunk`` parts merge with ``+`` (preserves ``tool_calls``,
``additional_kwargs`` and content-block lists); the merged chunk is
converted to a plain ``AIMessage`` so downstream code sees the same
shape as ``invoke()``. Plain-string chunks (some test doubles)
are joined into an ``AIMessage``. Returns ``None`` when *chunks*
is empty (test doubles only — real models raise rather than yield
zero chunks) so the caller can fall back to a direct ``invoke()``.
Structured-output chunks (dicts/Pydantic models without ``.content``)
are never reassembled here: returning ``None`` lets the caller fall
back to a direct ``invoke()`` that preserves the contract instead of
corrupting the payload into ``AIMessage(str(dict))``. That fallback
is a second upstream request for one logical call (token callbacks
fire twice); callers log a warning when it happens after chunks
were already consumed.
"""
if not chunks:
return None
for chunk in chunks:
if isinstance(chunk, dict):
return None
if isinstance(chunk, str):
continue
if hasattr(chunk, "content"):
continue
# Pydantic models (and other parsed outputs) have no .content:
# treat as structured, not as text to stringify.
try:
from pydantic import BaseModel as _PydanticBaseModel
if isinstance(chunk, _PydanticBaseModel):
return None
except Exception:
logger.debug(
"Pydantic structured-chunk check failed; continuing",
exc_info=True,
)
# Unknown non-message shape — only safe to stringify when it
# is clearly a message-like object; otherwise fall back.
if not hasattr(chunk, "tool_calls") and not hasattr(
chunk, "additional_kwargs"
):
return None
try:
# Fast path: message chunks support addition.
combined = chunks[0]
for chunk in chunks[1:]:
try:
combined = combined + chunk
except Exception:
# Mixed shapes — fall back to text join below.
combined = None
break
if combined is not None and hasattr(combined, "content"):
try:
from langchain_core.messages.utils import (
message_chunk_to_message,
)
return message_chunk_to_message(combined)
except Exception:
return combined
except Exception:
logger.debug(
"Stream-chunk combine via '+' failed; using text fallback"
)
# Text fallback: join content/text of each chunk.
from ..utilities.json_utils import _coerce_content_blocks
parts = []
for chunk in chunks:
text = (
chunk
if isinstance(chunk, str)
else getattr(chunk, "content", None)
)
if text is None:
text = str(chunk)
if isinstance(text, list):
try:
text = _coerce_content_blocks(text)
except Exception:
text = "".join(str(p) for p in text)
if text:
parts.append(str(text))
if not parts:
return None
return AIMessage(content="".join(parts))
def _invoke_via_stream(self, *args: Any, **kwargs: Any):
"""Implement ``invoke`` as stream+reassemble with per-chunk cancel.
Returns the combined response, or ``None`` when streaming is
unavailable (no ``stream`` attr), disallowed (structured-output or
usage-unsafe endpoint — see :meth:`_stream_cancel_allowed`), or
produced zero chunks (only empty test-double generators — real
LangChain models raise inside the generator on failure rather
than yielding zero chunks). The caller then falls back to a
direct ``invoke()``. Raises ``ResearchTerminatedException``
promptly when the flag is set mid-generation; the generator is
closed so the underlying HTTP response is released instead of
leaking to GC.
Mid-stream structured fallback (a dict/Pydantic chunk after one
or more message chunks were already pulled) also returns ``None``,
so the caller issues a *second* upstream request for the same
logical call — token callbacks fire for both attempts. No in-app
path produces such mixed shapes today (structured-output wrappers
are excluded up front via :meth:`_stream_cancel_allowed`), so this
is defense-in-depth only; a warning is logged when it triggers so
the double request is visible instead of silent.
"""
if getattr(self, "_ldr_structured_output", False):
return None
stream_fn = getattr(self.base_llm, "stream", None)
if stream_fn is None:
return None
research_id = self._effective_research_id()
try:
gen = stream_fn(*args, **kwargs)
except Exception:
logger.debug(
"Sync stream creation failed; falling back to direct invoke",
exc_info=True,
)
return None
chunks = []
try:
for chunk in gen:
if research_id:
self._check_terminated(research_id)
# Structured-output runnables yield dicts/models: abort the
# streaming attempt and fall back to direct invoke rather
# than corrupting the contract.
if self._is_structured_chunk(chunk):
# NOTE: package logging is disabled by default
# (local_deep_research/__init__.py); this surfaces
# once the app enables it at startup.
logger.warning(
"Sync stream yielded structured chunk after "
f"{len(chunks)} message chunk(s); falling back "
"to direct invoke issues a second upstream "
"request for one logical call (token callbacks "
"fire twice)"
)
try:
close = getattr(gen, "close", None)
if close is not None:
close()
except Exception:
logger.debug(
"Sync stream generator close failed "
"(structured fallback, non-critical)"
)
return None
chunks.append(chunk)
except BaseException:
# On abort (or any mid-stream failure) release the HTTP
# response promptly. ``GeneratorExit`` from close() surfaces
# here for some providers — never mask the original raise.
try:
close = getattr(gen, "close", None)
if close is not None:
close()
except Exception:
logger.debug(
"Sync stream generator close failed on abort (non-critical)"
)
raise
else:
try:
close = getattr(gen, "close", None)
if close is not None:
close()
except Exception:
logger.debug(
"Sync stream generator close failed (non-critical)"
)
if not chunks:
# Only reachable with empty test-double generators; real
# LangChain models raise inside the generator on failure.
return None
combined = self._combine_stream_chunks(chunks)
if combined is None:
# Chunks were consumed but not reassemblable (structured or
# unknown shapes): the caller's direct-invoke fallback is a
# second upstream request — see the docstring above.
logger.warning(
"Sync stream produced "
f"{len(chunks)} non-reassemblable chunk(s); falling back "
"to direct invoke issues a second upstream request for "
"one logical call (token callbacks fire twice)"
)
return combined
async def _ainvoke_via_stream(self, *args: Any, **kwargs: Any):
"""Async twin of :meth:`_invoke_via_stream` (via ``astream``).
Same fallback contract: zero chunks (test doubles only — real
models raise) and mid-stream structured shapes return ``None``;
a structured shape after pulled chunks means the direct-ainvoke
fallback is a second upstream request (warning logged).
"""
if getattr(self, "_ldr_structured_output", False):
return None
astream_fn = getattr(self.base_llm, "astream", None)
if astream_fn is None:
return None
research_id = self._effective_research_id()
chunks = []
# ``astream`` is an async-generator function: calling it must not
# raise for a missing implementation (that is what the None check
# above covers); iteration errors propagate to the caller.
try:
agen = astream_fn(*args, **kwargs)
except Exception:
logger.debug(
"Async stream creation failed; falling back to direct ainvoke",
exc_info=True,
)
return None
try:
async for chunk in agen:
if research_id:
self._check_terminated(research_id)
if self._is_structured_chunk(chunk):
logger.warning(
"Async stream yielded structured chunk after "
f"{len(chunks)} message chunk(s); falling back "
"to direct ainvoke issues a second upstream "
"request for one logical call (token callbacks "
"fire twice)"
)
try:
aclose = getattr(agen, "aclose", None)
if aclose is not None:
await aclose()
except Exception:
logger.debug(
"Async stream generator close failed "
"(structured fallback, non-critical)"
)
return None
chunks.append(chunk)
except BaseException:
try:
aclose = getattr(agen, "aclose", None)
if aclose is not None:
await aclose()
else:
close = getattr(agen, "close", None)
if close is not None:
close()
except Exception:
logger.debug(
"Async stream generator close failed on abort (non-critical)"
)
raise
else:
try:
aclose = getattr(agen, "aclose", None)
if aclose is not None:
await aclose()
except Exception:
logger.debug(
"Async stream generator close failed (non-critical)"
)
if not chunks:
# Only reachable with empty test-double generators; real
# LangChain models raise inside the generator on failure.
return None
combined = self._combine_stream_chunks(chunks)
if combined is None:
logger.warning(
"Async stream produced "
f"{len(chunks)} non-reassemblable chunk(s); falling back "
"to direct ainvoke issues a second upstream request for "
"one logical call (token callbacks fire twice)"
)
return combined
def invoke(self, *args: Any, **kwargs: Any) -> Any:
# Fail fast if Stop arrived while queued (e.g. between
# check_termination checkpoints) — no new HTTP request.
self._check_terminated()
try:
args, kwargs = self._with_research_callbacks(args, kwargs)
# When a cancellation scope is known, stream under the hood
# so a Stop mid-generation aborts within one chunk instead
# of waiting for the full response (minutes on slow local
# models). Without a scope there is nothing to check
# between chunks, so keep the direct path. The streaming
# path is additionally gated by _stream_cancel_allowed():
# structured-output wrappers and usage-unsafe endpoints
# (openai_endpoint without stream_usage) stay on direct
# invoke so token/cost metrics and the dict/Pydantic contract
# survive — pre/post checks still abort promptly between calls.
if (
self._effective_research_id() is not None
and self._stream_cancel_allowed()
):
streamed = self._invoke_via_stream(*args, **kwargs)
if streamed is not None:
self._check_terminated()
return self._normalize_response(streamed)
response = self.base_llm.invoke(*args, **kwargs)
except Exception as e:
self._log_llm_error(e)
raise
# Post-check: the flag may have been set while blocked. Raise
# so the worker aborts before the next pipeline step instead of
# continuing with a stale result.
self._check_terminated()
return self._normalize_response(response)
async def ainvoke(self, *args: Any, **kwargs: Any) -> Any:
# Async counterpart of invoke(); without this, ainvoke() would fall
# through __getattr__ to the base LLM and bypass think-tag stripping.
self._check_terminated()
try:
args, kwargs = self._with_research_callbacks(args, kwargs)
if (
self._effective_research_id() is not None
and self._stream_cancel_allowed()
):
streamed = await self._ainvoke_via_stream(*args, **kwargs)
if streamed is not None:
self._check_terminated()
return self._normalize_response(streamed)
response = await self.base_llm.ainvoke(*args, **kwargs)
except Exception as e:
self._log_llm_error(e)
raise
self._check_terminated()
return self._normalize_response(response)
def stream(self, *args: Any, **kwargs: Any):
"""Yield chunks with per-chunk termination checks.
Previously ``stream`` fell through ``__getattr__`` to the raw
model, bypassing both think-tag normalization and cancellation.
Now the generator checks the flag before yielding each chunk and
raises ``ResearchTerminatedException`` promptly on Stop. Chunks
are forwarded untouched (no per-chunk scrubbing — credentials
can straddle boundaries).
"""
research_id = self._effective_research_id()
if research_id:
self._check_terminated(research_id)
args, kwargs = self._with_research_callbacks(args, kwargs)
gen = None
try:
gen = self.base_llm.stream(*args, **kwargs)
for chunk in gen:
if research_id:
self._check_terminated(research_id)
yield chunk
except Exception as error:
log_exception_type(
"LLM Request - Failed with error in stream()", error
)
raise
finally:
try:
close = getattr(gen, "close", None)
if close is not None:
close()
except Exception:
logger.debug(
"Sync stream generator close failed in stream() (non-critical)"
)
async def astream(self, *args: Any, **kwargs: Any):
"""Async generator twin of :meth:`stream` with the same checks."""
research_id = self._effective_research_id()
if research_id:
self._check_terminated(research_id)
args, kwargs = self._with_research_callbacks(args, kwargs)
agen = None
try:
agen = self.base_llm.astream(*args, **kwargs)
async for chunk in agen:
if research_id:
self._check_terminated(research_id)
yield chunk
except Exception as error:
log_exception_type(
"LLM Request - Failed with error in astream()", error
)
raise
finally:
try:
aclose = getattr(agen, "aclose", None)
if aclose is not None:
await aclose()
except Exception:
logger.debug(
"Async stream generator close failed in astream() (non-critical)"
)
# Pass through any other attributes to the base LLM
def __getattr__(self, name):
return getattr(self.base_llm, name)
def batch(
self, inputs, config=None, *, return_exceptions=False, **kwargs: Any
):
"""Batch via :meth:`invoke` so per-item cancellation applies."""
results = []
# Resolve shared vs per-input configs like LangChain does.
if isinstance(config, list):
if len(config) != len(inputs):
raise ValueError(
f"configs must match the number of inputs ({len(config)} != {len(inputs)})"
)
configs = list(config)
else:
configs = [config] * len(inputs)
for single_input, single_config in zip(inputs, configs):
try:
if single_config is not None:
results.append(
self.invoke(single_input, single_config, **kwargs)
)
else:
results.append(self.invoke(single_input, **kwargs))
except Exception as e:
if return_exceptions:
results.append(e)
else:
raise
# ResearchTerminatedException (BaseException) propagates even with
# return_exceptions=True — a Stop must never be swallowed into a
# result list.
return results
async def abatch(
self, inputs, config=None, *, return_exceptions=False, **kwargs: Any
):
"""Async twin of :meth:`batch`."""
results = []
if isinstance(config, list):
if len(config) != len(inputs):
raise ValueError(
f"configs must match the number of inputs ({len(config)} != {len(inputs)})"
)
configs = list(config)
else:
configs = [config] * len(inputs)
for single_input, single_config in zip(inputs, configs):
try:
if single_config is not None:
results.append(
await self.ainvoke(
single_input, single_config, **kwargs
)
)
else:
results.append(await self.ainvoke(single_input, **kwargs))
except Exception as e:
if return_exceptions:
results.append(e)
else:
raise
return results
def close(self):
"""Close the model's clients if this wrapper owns the model.
``get_llm()`` gives a wrapper ownership only of a model that a
built-in provider built for it. A registered instance, or a model
that a registered factory returns, stays with the code that
registered it, since other researches can be using it.
"""
if not self._owns_llm:
return
try:
from ..utilities.llm_utils import _close_base_llm
_close_base_llm(self.base_llm)
except Exception:
logger.debug(
"best-effort cleanup of HTTP clients on shutdown",
exc_info=True,
)
def bind_tools(self, tools, **kwargs: Any) -> Any:
"""Re-wrap the tool-bound model so the <think>-stripper survives.
``langchain.agents.create_agent()`` and similar helpers call
``model.bind_tools(...)`` and then use the *return value* as
the model that actually runs inside the agent loop. Without
this override, Python falls through ``__getattr__`` to
``self.base_llm.bind_tools(...)``, which returns an unwrapped
``BaseChatModel``; the wrapper's ``_normalize_response`` (the
single authorized site that strips ``<think>…`` from fresh
responses) is then bypassed for every LLM call the agent
makes.
That bypass is the root cause of the regression tracked in
issue #4804: reasoning-mode models (Qwen 3.x, deepseek-r1,
etc.) emit ``<think>…</think>`` as plain text, the wrapper
fails to strip it, the raw artifact is appended to the
agent's ``AIMessage.content``, and on the next turn the
provider's strict parser rejects the request with
``Failed to parse input at pos 0: <think>…``.
Re-wrapping the bound model keeps the stripper in the loop
for every LLM call the agent makes, not just the ones we
issue directly via ``self.model.invoke(...)``.
Args:
tools: Tool definitions (schemas, callables, or
``BaseTool`` instances) to bind to the model.
**kwargs: Provider-specific binding options forwarded
verbatim to ``base_llm.bind_tools``.
Returns:
A ``ProcessingLLMWrapper`` wrapping the bound model, so
every subsequent ``invoke``/``ainvoke`` continues to
strip ``<think>`` tags.
"""
bound = self.base_llm.bind_tools(tools, **kwargs)
return self._rewrap(bound, structured_output=False)
def bind(self, **kwargs: Any) -> Any:
"""Re-wrap like :meth:`bind_tools`.
``langchain.agents.create_agent()`` calls ``model.bind(...)`` for an
agent without tools and runs the result. Without this override the
call would fall through ``__getattr__`` to the model, and the bound
model would skip the ``<think>`` stripping, the termination checks
and this research's token callbacks. Like this wrapper, the result
cannot be composed with ``|``.
"""
return self._rewrap(self.base_llm.bind(**kwargs))
def with_config(self, config=None, **kwargs: Any) -> Any:
"""Re-wrap like :meth:`bind`.
The bound config resolves with each call's config as it does in
LangChain: when the call passes no config, its keys win over those
inherited from an enclosing run. This research's token callbacks
run beside any callbacks it binds.
"""
return self._rewrap(self.base_llm.with_config(config, **kwargs))
def _rewrap(self, runnable, *, structured_output=None):
"""Wrap *runnable*, derived from this wrapper's model, the same way.
The new wrapper keeps this wrapper's ownership, cancellation scope,
provider, settings and research callbacks. It is a structured-output
wrapper if this one is, unless *structured_output* says otherwise.
"""
if structured_output is None:
structured_output = self._ldr_structured_output
return ProcessingLLMWrapper(
runnable,
owns_llm=self._owns_llm,
research_id=self._effective_research_id(),
provider=getattr(self, "_provider", None),
settings_snapshot=getattr(self, "_settings_snapshot", None),
structured_output=structured_output,
research_callbacks=self._ldr_research_callbacks,
)
def with_structured_output(self, schema, **kwargs: Any) -> Any:
"""Re-wrap like :meth:`bind_tools` so cancellation survives.
Structured-output runnables are used by several strategies for
JSON extraction; without re-wrapping, the bound runnable would
bypass the termination checks in ``invoke``. The re-wrapped
instance is marked ``structured_output`` so ``invoke``/``ainvoke``
use the direct path (pre/post checks) instead of stream+reassemble,
preserving the dict/Pydantic contract.
"""
bound = self.base_llm.with_structured_output(schema, **kwargs)
try:
from ..llm.cancellation import bind_llm_to_research
bind_llm_to_research(bound, self._effective_research_id())
except Exception:
logger.debug(
"Structured-output scope propagation failed (non-critical)"
)
return self._rewrap(bound, structured_output=True)
def wrap_llm_without_think_tags(
llm,
research_id=None,
provider=None,
research_context=None,
settings_snapshot=None,
*,
owns_llm: bool = True,
):
"""Wrap response processing and metrics with explicit resource ownership.
Set ``owns_llm=False`` for a model the wrapper must not close.
``get_llm()`` keeps ``True`` only for a model that a built-in provider
built for this call, and passes ``False`` for a registered instance and
for any model a registered factory returns: a factory can return the
same cached or singleton model on every call, and closing it when one
research ends would break the others. Whoever registered such a model
remains responsible for closing it after all consumers are done.
Ownership only decides who closes the model. Whether it owns the model
or not, this function writes nothing about the research onto the
model: the research's token callback is passed with each call, and the
cancellation scope is bound to LDR's wrappers only.
"""
# The model before any LDR wrapper. No research state lands on it or
# on anything it holds.
model = llm
# First apply rate limiting if enabled
from ..web_search_engines.rate_limiting.llm import (
create_rate_limited_llm_wrapper,
)
# Check if LLM rate limiting is enabled (independent of search rate limiting)
# Use the thread-safe get_db_setting defined in this module
if get_setting_from_snapshot(
"rate_limiting.llm_enabled", False, settings_snapshot=settings_snapshot
):
llm = create_rate_limited_llm_wrapper(model, provider)
# Set context_limit in research_context for overflow detection.
# This is needed for providers that go through the registered provider path
# (which returns before the code in get_llm that sets context_limit).
if research_context is not None and provider is not None:
if "context_limit" not in research_context:
context_limit = _get_context_window_for_provider(
provider, settings_snapshot
)
if context_limit is not None:
research_context["context_limit"] = context_limit
logger.info(
f"Set context_limit={context_limit} in wrap_llm for provider={provider}"
)
# Import token counting functionality if research_id is provided
callbacks = []
if research_id is not None:
from ..metrics import TokenCounter
token_counter = TokenCounter()
token_callback = token_counter.create_callback(
research_id, research_context
)
# Set provider and model info on the callback
if provider:
token_callback.preset_provider = provider
# Try to extract model name from the LLM instance
if hasattr(model, "model_name"):
token_callback.preset_model = model.model_name
elif hasattr(model, "model"):
token_callback.preset_model = model.model
callbacks.append(token_callback)
wrapped = ProcessingLLMWrapper(
llm,
owns_llm=owns_llm,
research_id=research_id,
provider=provider,
settings_snapshot=settings_snapshot,
# The model can serve other researches and users, owned or not, so
# this research's callbacks travel in each call's RunnableConfig.
# The rate-limiting wrapper passes that config on to the model.
research_callbacks=callbacks,
)
if research_id is not None:
# Propagate the scope to inner wrappers (rate-limiting layer)
# so direct uses of the inner instance also abort promptly. The
# walk stops at the model: it can be shared, and any model can
# hold shared objects under ``base_llm``.
try:
from ..llm.cancellation import bind_llm_to_research
bind_llm_to_research(wrapped, research_id, stop_at=model)
except Exception:
logger.debug(
"Inner-wrapper scope propagation failed (non-critical)"
)
return wrapped