Dynamic component
Dynamic runtime component wrapper and lazy constructor bindings.
DynamicComponent(component_class, init_kwargs=None, cache_instances=False, cache_size=256)
Bases: Component, Generic[ComponentT]
Instantiate an inner component at runtime from invocation kwargs.
Examples:
from gllm_core.schema import DynamicComponent, Lazy
from gllm_inference.lm_invoker import OpenAILMInvoker
class LMInvokerComponent(Component):
def __init__(self, model: str) -> None:
self._lm_invoker = OpenAILMInvoker(model_name=model)
async def _run(self, prompt: str, **kwargs: object) -> object:
return await self._lm_invoker.invoke(prompt, **kwargs)
dynamic_lm = DynamicComponent(
component_class=LMInvokerComponent,
init_kwargs={
"model": Lazy.from_runtime("model"),
},
cache_instances=True,
)
result = await dynamic_lm.run(
model="gpt-5-nano",
prompt="Explain RAG in one sentence.",
)
Initialize dynamic wrapper configuration and optional instance cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component_class
|
type[ComponentT]
|
Concrete component class to instantiate at runtime. |
required |
init_kwargs
|
dict[str, object | Lazy[object]] | None
|
Constructor kwargs, literal or lazy. Defaults to None. |
None
|
cache_instances
|
bool
|
Reuse inner component instances for equal resolved init kwargs. Defaults to False. |
False
|
cache_size
|
int | None
|
Maximum cache size for cached instances.
Use |
256
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Lazy(*, mode, runtime_name=None, fn=None, arg_name=None)
Bases: Generic[T]
Runtime binding definition for DynamicComponent constructor fields.
Lazy marks constructor kwargs that must be resolved at invocation time
instead of component-construction time.
Supported binding modes:
1. from_runtime(name):
Read the constructor value directly from runtime kwargs using name.
2. resolver(fn, arg_name=...):
Resolve the constructor value through a sync callable.
3. async_resolver(fn, arg_name=...):
Resolve through a callable that may return an awaitable.
Examples:
Direct runtime lookup:
Lazy.from_runtime("model")
Resolver using one runtime argument:
Lazy.resolver(resolve_base_url, arg_name="tenant_id")
Resolver using multiple runtime arguments:
Lazy.resolver(build_key, arg_name=["tenant_id", "region"])
Async-capable resolver:
Lazy.async_resolver(load_api_key, arg_name="tenant_id")
Initialize a Lazy binding definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
LazyMode
|
Binding mode ( |
required |
runtime_name
|
str | None
|
Runtime kwarg name for runtime bindings. Defaults to None. |
None
|
fn
|
Callable[..., object] | None
|
Resolver callable for resolver modes. Defaults to None. |
None
|
arg_name
|
str | list[str] | None
|
Runtime argument mapping for resolver call. Defaults to None. |
None
|
async_resolver(fn, *, arg_name=None)
staticmethod
Create an async-capable resolver binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Awaitable[T]] | Callable[..., T]
|
Resolver that may return awaitable or plain value. |
required |
arg_name
|
str | list[str] | None
|
Runtime kwarg mapping used to call |
None
|
Returns:
| Type | Description |
|---|---|
Lazy[T]
|
Lazy[T]: Async-capable resolver binding. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
from_runtime(name)
staticmethod
Create a binding that reads directly from invocation kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Runtime kwarg key to read. |
required |
Returns:
| Type | Description |
|---|---|
Lazy[object]
|
Lazy[object]: Runtime lookup binding. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
resolve(runtime_kwargs)
async
Resolve this lazy binding using runtime kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
runtime_kwargs
|
dict[str, object]
|
Invocation kwargs provided to |
required |
Returns:
| Type | Description |
|---|---|
tuple[T, set[str]]
|
tuple[T, set[str]]: Resolved constructor value and consumed runtime keys. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a required runtime key is missing. |
TypeError
|
If resolver binding is invalid at runtime. |
resolver(fn, *, arg_name=None)
staticmethod
Create a sync resolver binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., T]
|
Resolver function that returns a constructor value. |
required |
arg_name
|
str | list[str] | None
|
Runtime kwarg mapping passed into |
None
|
Returns:
| Type | Description |
|---|---|
Lazy[T]
|
Lazy[T]: Resolver binding. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
LazyMode
Bases: StrEnum
Supported resolution modes for Lazy bindings.