Skip to main content

Message Mapping

Use message mappers to block requests or provide custom responses based on routing conditions. This is useful for error handling, rate limiting, and content moderation.

Use Case

  • Block inappropriate requests
  • Provide custom error messages
  • Handle rate limit exceeded scenarios
  • Return maintenance messages

Basic Configuration

{
"model": "router/dynamic",
"router": {
"type": "conditional",
"pre_request": [
{
"name": "rate_limiter",
"type": "interceptor",
"limit": 10,
"period": "hour",
"target": "user_id"
}
],
"routes": [
{
"name": "rate_limit_exceeded",
"conditions": {
"all": [
{
"pre_request.rate_limiter.passed": {
"$eq": false
}
}
]
},
"message_mapper": {
"modifier": "block",
"content": "You have exceeded your hourly limit of 10 requests. Please try again later."
}
},
{
"name": "premium_user_only",
"conditions": {
"all": [
{
"extra.user.tier": {
"$neq": "premium"
}
}
]
},
"message_mapper": {
"modifier": "block",
"content": "This feature is only available to premium users. Please upgrade your account to access advanced AI models."
}
},
{
"name": "allowed_requests",
"conditions": {
"all": [
{
"pre_request.rate_limiter.passed": {
"$eq": true
}
},
{
"extra.user.tier": {
"$eq": "premium"
}
}
]
},
"targets": "openai/gpt-4o"
},
{
"name": "fallback_message",
"conditions": {
"all": []
},
"message_mapper": {
"modifier": "block",
"content": "Service temporarily unavailable. Please check your account status and try again."
}
}
]
}
}

How It Works

  1. Rate Limit Block: If user exceeds 10 requests per hour, block with helpful message
  2. Premium Only Block: Block non-premium users with upgrade message
  3. Allowed Requests: Route premium users within limits to GPT-4o
  4. Fallback Block: Block all other requests with generic message

Message Mapper Properties

  • modifier: Must be "block" to block the request
  • content: The message to return to the user instead of an AI response

Variables Used

  • pre_request.rate_limiter.passed: Rate limiter result (from interceptor)
  • extra.user.tier: User subscription tier (from request)

Customization

  • Add different messages for different user tiers
  • Include helpful links or contact information in blocked messages
  • Combine with guardrails for content-based blocking
  • Add maintenance window messages