mypy-boto3-lex-runtime Type Stubs

1.42.3 · active · verified Sat Apr 11

mypy-boto3-lex-runtime provides high-quality type annotations for the boto3 LexRuntimeService. Currently at version 1.42.3, this library is part of the larger mypy-boto3 project, which automatically generates stubs for all AWS services. It's updated frequently, typically aligning with new boto3 releases and enhancements in the mypy-boto3-builder.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `boto3` Lex Runtime client and make a `post_text` call with `mypy-boto3-lex-runtime` type annotations. The `TYPE_CHECKING` guard ensures that stub imports are only processed by the type checker, which can improve runtime performance and robustness if `mypy-boto3` is primarily a development dependency.

import os
import boto3
from typing import TYPE_CHECKING

# Import specific types only when type checking using TYPE_CHECKING guard
if TYPE_CHECKING:
    from mypy_boto3_lex_runtime.client import LexRuntimeServiceClient
    from mypy_boto3_lex_runtime.type_defs import PostTextRequestRequestTypeDef, PostTextResponseTypeDef

# Configure AWS credentials (for demonstration, use environment variables or default config)
# In a real application, consider using AWS IAM roles or standard AWS configuration.
aws_access_key_id = os.environ.get("AWS_ACCESS_KEY_ID", "MOCK_ACCESS_KEY")
aws_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY", "MOCK_SECRET_KEY")
aws_region = os.environ.get("AWS_REGION", "us-east-1")

# Initialize the Lex Runtime client with type annotation.
# The '"LexRuntimeServiceClient"' uses a string forward reference, common with TYPE_CHECKING.
client: "LexRuntimeServiceClient" = boto3.client(
    "lex-runtime",
    region_name=aws_region,
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
)

# Define request parameters using TypedDicts for type safety. 
# Replace 'YourLexBotName' with your actual Amazon Lex bot's name.
request_params: "PostTextRequestRequestTypeDef" = {
    "botName": "YourLexBotName", 
    "botAlias": "$LATEST",       
    "userId": "testuser123",
    "inputText": "Hello, what is your name?",
}

# Call a client method; mypy will check arguments and return type
response: "PostTextResponseTypeDef" = client.post_text(**request_params)

print(f"Lex Runtime PostText Response: {response}")

# Access typed fields from the response
message_content = response.get("message")
print(f"Bot's message: {message_content}")

view raw JSON →