mypy-boto3-lex-runtime Type Stubs
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
- breaking Python 3.8 support was officially removed with mypy-boto3-builder version 8.12.0 (released alongside mypy-boto3-lex-runtime versions around 1.30.0+). Projects still utilizing Python 3.8 will encounter compatibility issues.
- breaking Type definition naming conventions were refactored in mypy-boto3-builder 8.9.0. This included shortening redundant TypeDef suffixes (e.g., `CreateDistributionRequestRequestTypeDef` to `CreateDistributionRequestTypeDef`) and moving conflicting `Extra` postfixes. This can break explicit type imports in existing codebases.
- gotcha These packages provide only type stubs for `boto3`. For your application to run, you must install the actual `boto3` library. For static analysis, you also need to install `mypy`.
- gotcha To leverage these type stubs, you must actively run `mypy` (or another compatible static type checker) as part of your development or CI/CD workflow. The stubs themselves do not add runtime type enforcement.
- gotcha The `mypy-boto3` packages migrated to the PEP 561 standard for Python type stubs with `mypy-boto3-builder` version 8.12.0. While this is a positive change for compatibility, it might affect certain older build systems or tools that had specific assumptions about non-PEP 561 package structures.
- deprecated AWS services can be deprecated or renamed (e.g., `sms-voice` service was replaced by `pinpoint-sms-voice` in builder 8.11.0). While Lex Runtime is generally stable, future `mypy-boto3` versions will reflect any such changes in AWS service availability or APIs, which could break old service imports.
- gotcha It is recommended to wrap `mypy-boto3` type imports in `if TYPE_CHECKING:` blocks. This prevents loading potentially large stub modules at runtime, which can be beneficial for performance and robustness if the stubs are only a development dependency.
Install
-
pip install mypy-boto3-lex-runtime -
pip install boto3 mypy
Imports
- LexRuntimeServiceClient
from mypy_boto3_lex_runtime.client import LexRuntimeServiceClient
- PostTextRequestRequestTypeDef
from mypy_boto3_lex_runtime.type_defs import PostTextRequestRequestTypeDef
- PostTextResponseTypeDef
from mypy_boto3_lex_runtime.type_defs import PostTextResponseTypeDef
- boto3.client without typing
import boto3 # For type-checking, assign to a type-hinted variable: client: LexRuntimeServiceClient = boto3.client("lex-runtime")
Quickstart
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}")