Create middleware with wrap_model_call hook from a function.
Converts a function with handler callback into middleware that can intercept model calls, implement retry logic, handle errors, and rewrite responses.
wrap_model_call(
func: _CallableReturningModelResponse[StateT, ContextT, ResponseT] | None = None,
*,
state_schema: type[StateT] | None = None,
tools: list[BaseTool] | None = None,
name: str | None = None
) -> Callable[[_CallableReturningModelResponse[StateT, ContextT, ResponseT]], AgentMiddleware[StateT, ContextT]] | AgentMiddleware[StateT, ContextT]| Name | Type | Description |
|---|---|---|
func | _CallableReturningModelResponse[StateT, ContextT, ResponseT] | None | Default: NoneFunction accepting (request, handler) that calls handler(request)
to execute the model and returns Request contains state and runtime. |
state_schema | type[StateT] | None | Default: NoneCustom state schema. Defaults to |
tools | list[BaseTool] | None | Default: NoneAdditional tools to register with this middleware. |
name | str | None | Default: NoneMiddleware class name. Defaults to function name. |