Skip to main content

Middleware API Reference

Middleware intercepts agent invocations, chat requests, and function calls to add custom processing.

AgentMiddleware

Abstract base class for agent middleware that intercepts agent invocations.

Abstract Method

process()

Process an agent invocation.
AgentContext
required
Agent invocation context containing agent, messages, and metadata.Use context.stream to determine if this is a streaming call. Set context.result to override execution, or observe the actual execution result after calling call_next().
Callable[[], Awaitable[None]]
required
Function to call the next middleware or final agent execution. Does not return anything - all data flows through the context.

Example: Retry Middleware


AgentContext

Context object for agent middleware invocations.

Attributes

SupportsAgentRun
The agent being invoked.
list[Message]
The messages being sent to the agent.
AgentSession | None
The agent session for this invocation, if any.
Mapping[str, Any] | None
The options for the agent invocation as a dict.
bool
Whether this is a streaming invocation.
dict[str, Any]
Metadata dictionary for sharing data between agent middleware.
AgentResponse | ResponseStream[AgentResponseUpdate, AgentResponse] | None
Agent execution result. Can be observed after calling call_next() or set to override the execution result.
  • For non-streaming: should be AgentResponse
  • For streaming: should be ResponseStream[AgentResponseUpdate, AgentResponse]
dict[str, Any]
Additional keyword arguments passed to the agent run method.

Example


ChatMiddleware

Abstract base class for chat middleware that intercepts chat client requests.

Abstract Method

process()

Process a chat client request.
ChatContext
required
Chat invocation context containing chat client, messages, options, and metadata.Use context.stream to determine if this is a streaming call. Set context.result to override execution.
Callable[[], Awaitable[None]]
required
Function to call the next middleware or final chat execution.

ChatContext

Context object for chat middleware invocations.

Attributes

SupportsChatGetResponse
The chat client being invoked.
Sequence[Message]
The messages being sent to the chat client.
Mapping[str, Any] | None
The options for the chat request as a dict.
bool
Whether this is a streaming invocation.
dict[str, Any]
Metadata dictionary for sharing data between chat middleware.
ChatResponse | ResponseStream[ChatResponseUpdate, ChatResponse] | None
Chat execution result. Can be observed after calling call_next() or set to override the execution result.
  • For non-streaming: should be ChatResponse
  • For streaming: should be ResponseStream[ChatResponseUpdate, ChatResponse]
dict[str, Any]
Additional keyword arguments passed to the chat client.

Example


FunctionMiddleware

Abstract base class for function middleware that intercepts function invocations.

Abstract Method

process()

Process a function invocation.
FunctionInvocationContext
required
Function invocation context containing function, arguments, and metadata.Set context.result to override execution, or observe the actual execution result after calling call_next().
Callable[[], Awaitable[None]]
required
Function to call the next middleware or final function execution.

FunctionInvocationContext

Context object for function middleware invocations.

Attributes

FunctionTool
The function being invoked.
BaseModel | Mapping[str, Any]
The validated arguments for the function.
dict[str, Any]
Metadata dictionary for sharing data between function middleware.
Any
Function execution result. Can be observed after calling call_next() or set to override the execution result.
dict[str, Any]
Additional keyword arguments passed to the chat method that invoked this function.

Example


Middleware Decorators

Function-based middleware using decorators.

@agent_middleware

Mark a function as agent middleware.

@function_middleware

Mark a function as function middleware.

@chat_middleware

Mark a function as chat middleware.

MiddlewareTermination

Control-flow exception to terminate middleware execution early.

Constructor

str
default:"Middleware terminated execution."
Error message.
Any
default:"None"
Optional result to return when terminating.

Example


categorize_middleware()

Categorize middleware from multiple sources into agent, function, and chat types.
MiddlewareTypes | Sequence[MiddlewareTypes] | None
required
Variable number of middleware sources to categorize.
MiddlewareDict
Dictionary with keys “agent”, “function”, “chat” containing lists of categorized middleware.