Processors
This section contains auto-generated documentation for all processor classes in Routir. Processors handle request batching, caching, and content processing.
Base Classes
- class routir.processors.abstract.Processor(cache_size=1024, cache_ttl=600, cache_key=None, redis_url=None, redis_kwargs={})[source]
Bases:
FactoryEnabledBase class for request processors with optional caching.
A
Processorsits between the HTTP layer and an engine. It handles cache lookup/store and delegates to_submit()for the actual work.When to subclass Processor vs BatchProcessor
Subclass
Processorwhen requests should be handled one at a time — e.g. content lookup by document ID where batching adds no value. Override_submit()to implement the logic.Subclass
BatchProcessorwhen the underlying engine (e.g. a GPU model) benefits from processing multiple requests together. Override_process_batch()instead.
Both classes share the same caching interface; the cache is checked before
_submit()/_process_batch()is called.- cache_key
Function
(item: dict) -> hashableused to derive the cache key from a request dict. Default key includesservice,query,limit, andsubset.- Type:
callable
- __init__(cache_size=1024, cache_ttl=600, cache_key=None, redis_url=None, redis_kwargs={})[source]
Initialize the processor with optional caching.
- Parameters:
cache_size (int) – Maximum number of cached entries.
-1or0disables caching entirely. Whenredis_urlis set, this controls the Redis key-count budget (approximate).cache_ttl (int) – Cache entry time-to-live in seconds (default 600).
cache_key (callable, optional) –
(item: dict) -> hashablefunction to derive a cache key from a request dict. The default key is(service, query, limit, subset). Override when additional request fields affect the result (e.g. passcache_key_fieldsvia a closure, asload_config()does per service).redis_url (str, optional) – Redis connection URL. When provided, Redis is used instead of the in-memory LRU cache.
redis_kwargs (dict) – Additional keyword arguments forwarded to the Redis client.
- class routir.processors.abstract.BatchProcessor(batch_size=32, max_wait_time=0.1, cache_size=1024, cache_ttl=600, cache_key=None, **kwargs)[source]
Bases:
ProcessorProcessor that accumulates requests into batches before engine inference.
Requests are queued in an
asyncio.Queueby_submit(). A background worker collects items until eitherbatch_sizeis reached ormax_wait_timeseconds elapse, then calls_process_batch()with the whole batch. This amortises GPU/model overhead across concurrent requests, improving throughput at the cost of a small latency increase.Subclasses must override
_process_batch(); all other machinery is provided here.The worker is started lazily on the first request (or explicitly via
start()), and runs for the lifetime of the process.- __init__(batch_size=32, max_wait_time=0.1, cache_size=1024, cache_ttl=600, cache_key=None, **kwargs)[source]
Initialize the batch processor.
- Parameters:
batch_size (int) – Maximum number of requests accumulated into one batch before the engine is called (default 32).
max_wait_time (float) – Maximum seconds to wait for the batch to fill before processing a partial batch (default 0.1 s). Tune this to balance latency vs. GPU utilisation — lower values reduce wait time, higher values pack more requests per batch.
cache_size (int) – LRU cache size;
-1disables caching.cache_ttl (int) – Cache TTL in seconds.
cache_key (callable, optional) – Custom cache-key function; see
Processorfor details.**kwargs – Forwarded to
Processor.__init__.
Query Processors
- class routir.processors.query_processors.AsyncQueryProcessor(engine, cache_size=1024, cache_ttl=600, cache_key=None, **kwargs)[source]
Bases:
ProcessorProcessor that serve every request independently through async calls. Should be used when all the engine does is issuing async calls
- __init__(engine, cache_size=1024, cache_ttl=600, cache_key=None, **kwargs)[source]
Initialize the processor with optional caching.
- Parameters:
cache_size (int) – Maximum number of cached entries.
-1or0disables caching entirely. Whenredis_urlis set, this controls the Redis key-count budget (approximate).cache_ttl (int) – Cache entry time-to-live in seconds (default 600).
cache_key (callable, optional) –
(item: dict) -> hashablefunction to derive a cache key from a request dict. The default key is(service, query, limit, subset). Override when additional request fields affect the result (e.g. passcache_key_fieldsvia a closure, asload_config()does per service).redis_url (str, optional) – Redis connection URL. When provided, Redis is used instead of the in-memory LRU cache.
redis_kwargs (dict) – Additional keyword arguments forwarded to the Redis client.
- class routir.processors.query_processors.BatchQueryProcessor(engine, **kwargs)[source]
Bases:
BatchProcessor- __init__(engine, **kwargs)[source]
Initialize the batch processor.
- Parameters:
batch_size (int) – Maximum number of requests accumulated into one batch before the engine is called (default 32).
max_wait_time (float) – Maximum seconds to wait for the batch to fill before processing a partial batch (default 0.1 s). Tune this to balance latency vs. GPU utilisation — lower values reduce wait time, higher values pack more requests per batch.
cache_size (int) – LRU cache size;
-1disables caching.cache_ttl (int) – Cache TTL in seconds.
cache_key (callable, optional) – Custom cache-key function; see
Processorfor details.**kwargs – Forwarded to
Processor.__init__.
- class routir.processors.query_processors.BatchDecomposeQueryProcessor(engine, **kwargs)[source]
Bases:
BatchProcessor- __init__(engine, **kwargs)[source]
Initialize the batch processor.
- Parameters:
batch_size (int) – Maximum number of requests accumulated into one batch before the engine is called (default 32).
max_wait_time (float) – Maximum seconds to wait for the batch to fill before processing a partial batch (default 0.1 s). Tune this to balance latency vs. GPU utilisation — lower values reduce wait time, higher values pack more requests per batch.
cache_size (int) – LRU cache size;
-1disables caching.cache_ttl (int) – Cache TTL in seconds.
cache_key (callable, optional) – Custom cache-key function; see
Processorfor details.**kwargs – Forwarded to
Processor.__init__.
Content Processors
- class routir.processors.content_processors.ContentProcessor(collection_config, cache_size=0, cache_ttl=600)[source]
Bases:
ProcessorProcessor for retrieving document content by ID.
Provides fast random access to documents in JSONL files using offset maps.
- config
Collection configuration
- line_reader
Random access reader for document file
- content_field
Field(s) containing document text
- lang_mapping
Optional mapping of document IDs to language codes
- __init__(collection_config, cache_size=0, cache_ttl=600)[source]
Initialize content processor.
- Parameters:
collection_config (CollectionConfig) – Collection configuration with doc_path, id_field, etc.
cache_size – Maximum cache entries
cache_ttl – Cache TTL in seconds
- class routir.processors.content_processors.IRDSProcessor(collection_config, cache_size=0, cache_ttl=600)[source]
Bases:
ProcessorProcessor for retrieving document content by ID from IRDS format.
Inherits from ContentProcessor and uses IRDS-specific line reader.
- __init__(collection_config, cache_size=0, cache_ttl=600)[source]
Initialize content processor.
- Parameters:
collection_config (CollectionConfig) – Collection configuration with doc_path, id_field, etc.
cache_size – Maximum cache entries
cache_ttl – Cache TTL in seconds
Score Processors
- class routir.processors.score_processors.AsyncPairwiseScoreProcessor(engine, cache_size=1024, cache_ttl=600, cache_key=None, **kwargs)[source]
Bases:
ProcessorProcessor that serve every request independently through async calls. Should be used when all the engine does is issuing async calls
- __init__(engine, cache_size=1024, cache_ttl=600, cache_key=None, **kwargs)[source]
Initialize the processor with optional caching.
- Parameters:
cache_size (int) – Maximum number of cached entries.
-1or0disables caching entirely. Whenredis_urlis set, this controls the Redis key-count budget (approximate).cache_ttl (int) – Cache entry time-to-live in seconds (default 600).
cache_key (callable, optional) –
(item: dict) -> hashablefunction to derive a cache key from a request dict. The default key is(service, query, limit, subset). Override when additional request fields affect the result (e.g. passcache_key_fieldsvia a closure, asload_config()does per service).redis_url (str, optional) – Redis connection URL. When provided, Redis is used instead of the in-memory LRU cache.
redis_kwargs (dict) – Additional keyword arguments forwarded to the Redis client.
- class routir.processors.score_processors.BatchPairwiseScoreProcessor(engine, **kwargs)[source]
Bases:
BatchProcessor- __init__(engine, **kwargs)[source]
Initialize the batch processor.
- Parameters:
batch_size (int) – Maximum number of requests accumulated into one batch before the engine is called (default 32).
max_wait_time (float) – Maximum seconds to wait for the batch to fill before processing a partial batch (default 0.1 s). Tune this to balance latency vs. GPU utilisation — lower values reduce wait time, higher values pack more requests per batch.
cache_size (int) – LRU cache size;
-1disables caching.cache_ttl (int) – Cache TTL in seconds.
cache_key (callable, optional) – Custom cache-key function; see
Processorfor details.**kwargs – Forwarded to
Processor.__init__.
Cache
- class routir.processors.cache.Cache(capacity=-1, ttl=-1)[source]
Bases:
ABCAbstract base class for cache implementations.
- class routir.processors.cache.LRUCache(capacity=-1, ttl=-1)[source]
Bases:
CacheAn async LRU cache implementation.
- class routir.processors.cache.RedisCache(capacity=-1, ttl=-1, redis_url='redis://localhost:6379', key_prefix='routircache:', **redis_kwargs)[source]
Bases:
CacheA Redis-based cache implementation with async support.
- __init__(capacity=-1, ttl=-1, redis_url='redis://localhost:6379', key_prefix='routircache:', **redis_kwargs)[source]
Initialize Redis cache.
- Parameters:
capacity (int) – Maximum number of items to store (-1 for unlimited, not enforced by Redis)
ttl (float) – Time-to-live in seconds for cache entries (-1 for no expiry)
redis_url (str) – Redis connection URL
key_prefix (str) – Prefix for all cache keys to avoid collisions
**redis_kwargs – Additional arguments to pass to Redis client
File Random Access Reader
Registry
- class routir.processors.registry.DummyProcessor(engine, method)[source]
Bases:
ProcessorA minimal, no-cache processor that wraps a single engine method.
Used internally by
auto_register()to expose an engine method (search,fuse, etc.) as aProcessorwithout any batching or caching overhead. Requests are dispatched directly to the engine method on every call.Not intended for production workloads where batching matters; prefer the config-based loading path via
load_config(), which createsBatchProcessorinstances.- __init__(engine, method)[source]
Initialize the processor with optional caching.
- Parameters:
cache_size (int) – Maximum number of cached entries.
-1or0disables caching entirely. Whenredis_urlis set, this controls the Redis key-count budget (approximate).cache_ttl (int) – Cache entry time-to-live in seconds (default 600).
cache_key (callable, optional) –
(item: dict) -> hashablefunction to derive a cache key from a request dict. The default key is(service, query, limit, subset). Override when additional request fields affect the result (e.g. passcache_key_fieldsvia a closure, asload_config()does per service).redis_url (str, optional) – Redis connection URL. When provided, Redis is used instead of the in-memory LRU cache.
redis_kwargs (dict) – Additional keyword arguments forwarded to the Redis client.
- routir.processors.registry.auto_register(methods, **default_init_kwargs)[source]
Class decorator that instantiates an engine and registers it as a processor.
This is a lightweight alternative to the JSON config-based loading path, useful for built-in or singleton engines (e.g. stateless fusion rules like RRF) that do not need per-service batching or caching.
Warning
auto_registercreates a single engine instance withdefault_init_kwargsat decoration time usingDummyProcessor(no batching, no caching). For production engines with GPU models, use the config-based path instead so thatBatchProcessorand LRU/Redis caching are applied.The engine class must implement the
{method}_batchmethod for every method listed. The decorator checks the correspondingcan_{method}property (e.g.can_fusefor"fuse") and raisesTypeErrorif it returnsFalse.The service is registered under the engine’s class name in
ProcessorRegistry.- Parameters:
- Returns:
The unmodified engine class (decorator is transparent).
Example
@auto_register("fuse") class RRFFusion(Engine): async def fuse_batch(self, queries, batch_scores, **kwargs): ... # RRFFusion is now accessible as ProcessorRegistry["RRFFusion"]["fuse"]