Skip to content

Strategy

Strategy ABC and StrategyMeta metaclass.

This module defines the Strategy abstract base class and its metaclass that auto-generates builder methods for slots marked with uses().

Strategy()

Bases: ABC

Abstract base class for all RAG strategies.

Strategy defines the interface for RAG strategies with auto-generated builder methods for optional slots marked with uses().

Initialize strategy with empty slot values and no cached pipeline.

__getattr__(name)

Provide typed fallback for metaclass-generated with_() methods.

This fallback is primarily for static analysis tools that cannot infer methods generated dynamically by StrategyMeta. At runtime, valid with_ methods already exist on the class and will be resolved before getattr.

Parameters:

Name Type Description Default
name str

The missing attribute name.

required

Returns:

Type Description
Callable[[Any], Self]

Callable[[Any], Self]: Bound builder method for the requested slot.

Raises:

Type Description
AttributeError

If the attribute is not a known slot builder.

arun(question, top_k=5, **kwargs) async

Async entry point.

Parameters:

Name Type Description Default
question str

The user query.

required
top_k int

Number of top results to retrieve. Defaults to 5.

5
**kwargs

Additional retrieval parameters. Supported: query_filter, threshold.

{}

Returns:

Type Description
dict[str, Any]

dict[str, Any]: Final state dict with response.

as_tool(name='', description='')

Return a tool representation of this strategy.

Parameters:

Name Type Description Default
name str

Tool name. Defaults to class name.

''
description str

Tool description. Defaults to class docstring.

''

Returns:

Name Type Description
Any Any

Tool representation via pipeline.as_tool().

build()

Materialize the pipeline.

Idempotent - returns cached instance if already built.

Returns:

Name Type Description
Any Any

Pipeline instance.

Raises:

Type Description
NotImplementedError

If _build_pipeline() not implemented by subclass.

describe() classmethod

Return strategy description.

Returns:

Name Type Description
str str

Description with strategy name, slot names/types, and slot descriptions.

run(question, top_k=5, **kwargs)

Synchronous entry point.

Parameters:

Name Type Description Default
question str

The user query.

required
top_k int

Number of top results to retrieve. Defaults to 5.

5
**kwargs Any

Additional retrieval parameters. Supported: query_filter, threshold.

{}

Returns:

Type Description
dict[str, Any]

dict[str, Any]: Final state dict with response.

Raises:

Type Description
RuntimeError

If called from within a running event loop.

StrategyMeta

Bases: ABCMeta

Metaclass for Strategy that auto-generates builder methods.

Combines with ABCMeta to preserve abstract class functionality while auto-generating .with_() builder methods for slots marked with uses().

__new__(mcs, name, bases, namespace)

Create new Strategy subclass with auto-generated builder methods.

Parameters:

Name Type Description Default
mcs StrategyMeta

The metaclass.

required
name str

Name of the class being created.

required
bases tuple[type, ...]

Base classes.

required
namespace dict[str, Any]

Class namespace dictionary.

required

Returns:

Name Type Description
StrategyMeta StrategyMeta

The newly created class with generated builder methods.