Plugin API Reference
This section documents the built-in plugins provided by SmartRoute.
For additional reference, see:
LLM API Details - Complete plugin API reference
Scope/channel policies are available via SmartPublisher (
smartpublisher.smartroute_plugins.publish.PublishPlugin).
Auto-Generated Plugin API
Logging plugin (source of truth).
Rebuild behaviour exactly as described; no hidden defaults beyond this text.
Responsibilities
Wrap each handler call and emit configurable messages:
before(default True) logs"{entry.name} start";after(default True) logs"{entry.name} end (<ms> ms)"with elapsed time.Sinks: when
printis true, always useprint(message); whenlogis true, uselogger.info(message)if the logger reports handlers viahasHandlers(), otherwise fall back toprint(message); else no output.enabledgates the plugin entirely (default True).Use a provided
logging.Logger(defaultlogging.getLogger("smartroute")).
Configuration
Accepted keys (router-level or per-handler):
enabled,before,after,log,print. They can be provided as individual kwargs (e.g.logging_after=False) or inlogging_flags(e.g."enabled:off,before:on,after:on,log:on,print:off").Runtime:
router.logging.configuremirrors the same options, plus per-handler viaconfigure["handler"].before = False.flagsstring values are parsed like other plugins viaBasePlugin.
Behaviour and API
LoggingPlugin(name=None, logger=None, **cfg)delegates toBasePlugin; ifnameis falsy it sets"logger"as the plugin name.loggeris stored inself._logger; additional**cfgseeds initial config._emit(message, cfg)chooses sink based oncfgas described above.wrap_handler(route, entry, call_next)applies the configuration on each call. Exceptions propagate; the end message is skipped when an exception is raised.
Registration
At module import, the plugin registers itself globally as "logging" via
Router.register_plugin(LoggingPlugin).
- class smartroute.plugins.logging.LoggingPlugin(router, *, logger=None, **cfg)[source]
Bases:
BasePluginSimplified logging plugin for SmartRoute.
- Parameters:
logger (
Optional[Logger])
-
plugin_code:
str= 'logging'
-
plugin_description:
str= 'Logs handler calls with timing'
Pydantic validation plugin (source of truth).
Rebuild exactly from this contract; no hidden behaviour.
Responsibilities
At registration time (
on_decore), inspect handler type hints and build a Pydantic model capturing annotated parameters.At call time (
wrap_handler), validate annotated args/kwargs before calling the real handler; non-annotated parameters bypass validation.Surface validation failures as Pydantic
ValidationErrorwith contextual title"Validation error in <entry.name>".
Dependencies and guards
Importing this module requires
pydantic; otherwise an ImportError is raised with install guidance. Under TYPE_CHECKING it hintsRouterimport.If type hint resolution fails or no usable hints remain after dropping the return annotation, no model is created and wrapping becomes a passthrough.
Behaviour and data
PydanticPlugin(name=None, **config): delegates toBasePlugin; default name is"pydantic".on_decore(route, func, entry):resolves type hints via
get_type_hints(func); exceptions skip model.removes
returnhint if present.- builds a fields dict from function signature:
annotated parameters missing from signature get required ellipsis.
parameters with default use that default; otherwise required.
creates a model
<func.__name__>_Modelviacreate_model.stores metadata in
entry.metadata["pydantic"]:{"model": model, "hints": hints, "signature": sig}.
wrap_handler(route, entry, call_next):calls
get_model()to check if validation is disabled or no model exists.if
get_model()returns None, returnscall_next(passthrough).binds incoming args/kwargs with the captured
signature(sig.bind) and applies defaults.splits bound arguments into two dicts: annotated (validated) and non-annotated (passthrough).
runs the model with annotated args; on
ValidationErrorraises a newValidationError.from_exception_datawith the contextual title.merges validated values back with passthrough args into
final_argsand callscall_next(**final_args).return value is propagated unchanged.
get_model(entry): returns the Pydantic model unless configdisabledis truthy or no model exists.
Registration
Registers itself globally as "pydantic" during module import via
Router.register_plugin(PydanticPlugin).
- class smartroute.plugins.pydantic.PydanticPlugin(router, **config)[source]
Bases:
BasePluginValidate handler inputs with Pydantic using type hints.
- Parameters:
config (
Any)
-
plugin_code:
str= 'pydantic'
-
plugin_description:
str= 'Validates handler inputs using Pydantic type hints'
- configure(disabled=False)[source]
Configure pydantic plugin options.
The wrapper added by __init_subclass__ handles writing to store.
- Parameters:
disabled (
bool)
- on_decore(route, func, entry)[source]
Override to run logic when a handler is registered.
Called once per handler at decoration time. Use this to: - Inspect type hints and store metadata - Pre-compute validation models - Annotate
entry.metadatafor later use in wrap_handler- Parameters:
router – The Router instance registering the handler.
func (
Callable) – The original handler function.entry (
MethodEntry) – MethodEntry with name, func, router, plugins, metadata.route (
Router)
- Return type:
None
- wrap_handler(route, entry, call_next)[source]
Validate annotated parameters with the cached Pydantic model before calling.
- Parameters:
route (
Router)entry (
MethodEntry)call_next (
Callable)
- get_model(entry)[source]
Return the Pydantic model for this handler if not disabled.
- Parameters:
entry (
MethodEntry)- Return type:
Optional[Tuple[str,Any]]
- name