ooai_llm.cache

LangChain cache bootstrap helpers.

Purpose:

Provide small utilities for resolving a project-local LLM cache path and configuring the global LangChain cache.

Design:
  • Keep imports lazy so the module can be imported without LangChain.

  • Default to SQLite because it is file-backed, simple, and local.

  • Support networked caches through optional Redis and Upstash Redis clients.

  • Reuse ooai_llm.settings.AppSettings for path resolution.

Examples

>>> from pathlib import Path
>>> settings = AppSettings(app_root=Path.cwd())
>>> resolve_llm_cache_path(settings).name
'langchain_llm_cache.sqlite3'

Attributes

Classes

NamespacedCache

Wrap a LangChain cache and add namespace/profile material to cache keys.

Functions

resolve_llm_cache_path(→ pathlib.Path)

Resolve the effective LLM cache path.

build_sqlite_cache(→ Any)

Build a SQLite-backed LangChain cache.

build_memory_cache(→ Any)

Build an in-memory LangChain cache.

build_sqlalchemy_cache(→ Any)

Build a SQLAlchemy-backed LangChain cache.

build_redis_cache(→ Any)

Build a Redis-backed LangChain cache.

build_upstash_redis_cache(→ Any)

Build an Upstash Redis-backed LangChain cache.

build_llm_cache(→ Any)

Build the configured LangChain cache backend.

build_namespaced_cache(→ NamespacedCache)

Build the configured cache backend wrapped with namespace key material.

configure_global_llm_cache(→ Any)

Configure the global LangChain LLM cache.

normalize_cache_argument(→ BaseCache | bool | None)

Normalize the per-model cache argument.

Module Contents

ooai_llm.cache.BaseCache[source]
ooai_llm.cache.BaseCacheType[source]
ooai_llm.cache.logger[source]
class ooai_llm.cache.NamespacedCache(cache: langchain_core.caches.BaseCache, *, namespace: str, profile_key: str)[source]

Bases: langchain_core.caches.BaseCache

Wrap a LangChain cache and add namespace/profile material to cache keys.

The wrapper rewrites only LangChain’s llm_string component. It does not mutate prompt serialization, so chat prompt cache semantics remain delegated to the underlying cache implementation.

cache[source]
namespace[source]
profile_key[source]
namespaced_llm_string(llm_string: str) str[source]

Return the LangChain cache key component with namespace material.

lookup(prompt: str, llm_string: str) Any[source]

Look up a cached value using the namespaced key component.

update(prompt: str, llm_string: str, return_val: Any) None[source]

Update a cached value using the namespaced key component.

clear(**kwargs: Any) None[source]

Clear the wrapped cache.

async alookup(prompt: str, llm_string: str) Any[source]

Async cache lookup when the wrapped cache supports it.

async aupdate(prompt: str, llm_string: str, return_val: Any) None[source]

Async cache update when the wrapped cache supports it.

async aclear(**kwargs: Any) None[source]

Async cache clear when the wrapped cache supports it.

ooai_llm.cache.resolve_llm_cache_path(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) pathlib.Path[source]

Resolve the effective LLM cache path.

Parameters:
  • settings – Application settings.

  • path – Optional explicit cache path override.

Returns:

Resolved cache path.

ooai_llm.cache.build_sqlite_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]

Build a SQLite-backed LangChain cache.

Parameters:
  • settings – Application settings.

  • path – Optional explicit cache path override.

Returns:

SQLite cache instance.

Raises:

ImportError – If langchain_community is not installed.

ooai_llm.cache.build_memory_cache() Any[source]

Build an in-memory LangChain cache.

Returns:

In-memory cache instance.

Raises:

ImportError – If langchain_community is not installed.

ooai_llm.cache.build_sqlalchemy_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]

Build a SQLAlchemy-backed LangChain cache.

Parameters:
  • settings – Application settings.

  • path – Optional SQLite path used when sqlalchemy_url is unset.

Returns:

SQLAlchemy cache instance.

Raises:

ImportError – If SQLAlchemy or LangChain community caches are missing.

ooai_llm.cache.build_redis_cache(settings: ooai_llm.settings.AppSettings) Any[source]

Build a Redis-backed LangChain cache.

Parameters:

settings – Application settings.

Returns:

Redis cache instance.

Raises:

ImportError – If redis or LangChain community caches are missing.

ooai_llm.cache.build_upstash_redis_cache(settings: ooai_llm.settings.AppSettings) Any[source]

Build an Upstash Redis-backed LangChain cache.

Parameters:

settings – Application settings.

Returns:

Upstash Redis cache instance.

Raises:
  • ValueError – If the Upstash URL or token is missing.

  • ImportError – If upstash_redis or LangChain community caches are missing.

ooai_llm.cache.build_llm_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]

Build the configured LangChain cache backend.

Parameters:
  • settings – Application settings.

  • path – Optional SQLite path override for file-backed backends.

Returns:

Cache object.

Raises:
  • ValueError – If the configured backend is unsupported.

  • ImportError – If backend-specific dependencies are missing.

ooai_llm.cache.build_namespaced_cache(settings: ooai_llm.settings.AppSettings, *, namespace: str, profile_key: str, path: str | pathlib.Path | None = None) NamespacedCache[source]

Build the configured cache backend wrapped with namespace key material.

ooai_llm.cache.configure_global_llm_cache(settings: ooai_llm.settings.AppSettings, *, path: str | pathlib.Path | None = None) Any[source]

Configure the global LangChain LLM cache.

Parameters:
  • settings – Application settings.

  • path – Optional explicit cache path override.

Returns:

Cache object when enabled, otherwise None.

Raises:
  • ValueError – If the configured backend is unsupported.

  • ImportError – If required LangChain cache packages are not installed.

ooai_llm.cache.normalize_cache_argument(cache: BaseCache | bool | None) BaseCache | bool | None[source]

Normalize the per-model cache argument.

Parameters:

cacheTrue to force the global cache, False to disable, None to inherit the global setting, or a concrete cache object.

Returns:

Unchanged normalized cache value.