Skip to content

Feature request: Add LexResolver for simplified intent routing #5684

Open
@fakhtar

Description

@fakhtar

Use case

Feature Request: Add LexResolver for simplified intent routing

Description

Add a LexResolver class similar to APIGatewayRestResolver that enables decorator-based routing for Amazon Lex V2 Lambda functions. This simplifies handling different intents and event types in Lex bot fulfillment/dialog code hooks.

Use Case

from aws_lambda_powertools.utilities.lex import LexResolver

app = LexResolver()

@app.fulfillment("OrderPizza")
def handle_order_pizza():
    # Handle pizza order fulfillment
    return {
        "sessionAttributes": session_attributes,
        "messages": [{
            "contentType": "PlainText",
            "content": "Your pizza order is confirmed"
        }]
    }

@app.dialog("CheckOrder")
def handle_check_order():
    # Handle order status dialog
    pass

Proposed Features

  • Intent-based routing using decorators
  • Separate decorators for DialogCodeHook and FulfillmentCodeHook
  • Access to session attributes via resolver context
  • Type hints and response validation
  • Error handling with appropriate Lex response formats
  • Middleware support similar to APIGatewayRestResolver

Benefits

  • Reduces boilerplate code
  • Cleaner separation of concerns
  • Type-safe handling of Lex events
  • Consistent with existing Powertools patterns

Implementation Notes

  • Follow similar pattern to APIGatewayRestResolver
  • Support Lambda Powertools logger and tracer integration
  • Maintain compatibility with Lex V2 response formats

Solution/User Experience

Solution/User Experience

The LexResolver would provide a clean, intuitive API for routing Lex events:

from aws_lambda_powertools.utilities.lex import LexResolver

app = LexResolver()

@app.fulfillment("OrderPizza")
def handle_pizza_order(session_attributes: dict):
    return {
        "sessionAttributes": {**session_attributes, "order_status": "confirmed"},
        "messages": [{
            "contentType": "PlainText",
            "content": "Your pizza order is confirmed"
        }]
    }

@app.dialog("CheckDelivery", required_slots=["order_id"])
def handle_delivery_check(session_attributes: dict):
    return {
        "sessionAttributes": session_attributes,
        "messages": [{
            "contentType": "PlainText",
            "content": f"Your delivery is on its way"
        }]
    }

def lambda_handler(event, context):
    return app.resolve(event, context)

Alternative solutions

Acknowledgment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions