powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Feature request: Add LexResolver for simplified intent routing

Open fakhtar opened this issue 11 months ago • 2 comments

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

fakhtar avatar Dec 04 '24 17:12 fakhtar

Hey @fakhtar ! Thank you for your feature request for a LexResolver. We appreciate your detailed suggestion and will evaluate it carefully in our next iterations. We'll get back to you soon or with any follow-up questions!

anafalcao avatar Dec 11 '24 00:12 anafalcao

Assigning to me to review this issue!

leandrodamascena avatar Jan 20 '25 12:01 leandrodamascena

Given the current roadmap and the lack of substantial demand for this feature, we currently don't have plans for adding this resolver at this time.

We're going to leave the issue open for a while to give other customers more time to add their influence and revisit this in early 2026 to make a final decision.

dreamorosi avatar Sep 05 '25 14:09 dreamorosi

Hello everyone! I think this issue has been open for a long time, and we haven't received much customer attention or any other requests. I think we can close it in favor of this new project that AWS recently launched: https://github.com/aws/lex-helper

We'll keep an eye on this issue, and if we see increasing customer demand, we may consider reopening it.

leandrodamascena avatar Sep 08 '25 17:09 leandrodamascena

[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.