Handler Decorators
AutoGen Core provides three decorators for defining message handlers:@event
One-way messages (no response)
@rpc
Request-response messages
@message_handler
Generic handler (both event and RPC)
@event Decorator
Use@event for handlers that process messages without returning a response (fire-and-forget).
Basic Usage
Event handlers must have return type
None. They’re called when ctx.is_rpc == False.Event Handler Signature
- Must be an
asyncmethod - Must have exactly 3 parameters:
self,message,ctx messagemust be type-hinted with the message type to handlectxmust be typeMessageContext- Return type must be
None
Event Parameters
bool
default:"True"
If
True, raises exception for type mismatches. If False, logs warnings.Callable[[MessageType, MessageContext], bool]
default:"None"
Secondary routing function. Applied in alphabetical order of handlers. First matching handler is called.
With Match Function
@rpc Decorator
Use@rpc for handlers that process requests and return responses.
Basic Usage
RPC handlers must return a value (not
None). They’re called when ctx.is_rpc == True.RPC Handler Signature
- Must be an
asyncmethod - Must have exactly 3 parameters:
self,message,ctx messagemust be type-hinted with the request type to handlectxmust be typeMessageContext- Return type must be type-hinted (not
None) - Must return a value
RPC Parameters
bool
default:"True"
If
True, raises exception for type mismatches. If False, logs warnings.Callable[[MessageType, MessageContext], bool]
default:"None"
Secondary routing function for selecting between multiple RPC handlers.
With Match Function
@message_handler Decorator
Use@message_handler for generic handlers that can process both events and RPCs.
Basic Usage
Message Handler Signature
Message Handler Parameters
bool
default:"True"
If
True, raises exception for type mismatches. If False, logs warnings.Callable[[MessageType, MessageContext], bool]
default:"None"
Secondary routing function.
Message Routing
Type-Based Routing
Messages are routed to handlers based on the type hint:Handler Selection Order
- Type match: Handler’s type annotation must match message type
- Alphabetical order: Handlers are sorted alphabetically by method name
- Match function: If provided, must return
True - First match wins: First handler that passes all checks is called
Unhandled Messages
on_unhandled_message logs an info message. Override it to customize behavior.
Advanced Patterns
Chained Handlers
Publish from Handler
Error Handling
State Management in Handlers
Complete Example
Best Practices
Use @event for notifications
Use @event for notifications
Use
@event when the sender doesn’t need a response. Perfect for logging, notifications, and status updates.Use @rpc for requests
Use @rpc for requests
Use
@rpc when the sender needs a response. Perfect for queries, calculations, and operations that produce results.Type hint messages
Type hint messages
Always type-hint message parameters. This enables type checking and automatic routing.
Handle errors gracefully
Handle errors gracefully
RPC handlers should return error responses rather than raising exceptions when possible. This provides better error information to callers.
Check cancellation
Check cancellation
For long-running handlers, periodically check
ctx.cancellation_token.is_cancelled().Next Steps
Message Passing
Learn about messages, contexts, and subscriptions
Distributed Runtime
Scale event-driven agents across processes
Agent Runtime
Understand runtime operations
Core Overview
Return to Core API overview