Type Stubs for boto3 LexRuntimeV2
mypy-boto3-lexv2-runtime provides static type annotations for the `boto3` LexRuntimeV2 service, generated by `mypy-boto3-builder`. It enhances type checking for your AWS `LexRuntimeV2` client interactions using `mypy`, improving code quality and catching potential errors at development time. The library releases frequently, often in sync with `boto3` and `mypy-boto3-builder` updates, with the current version being 1.42.3.
Warnings
- breaking Python 3.8 support has been removed. `mypy-boto3-builder` versions 8.12.0 and later (which generated `mypy-boto3-lexv2-runtime` 1.42.3) require Python 3.9 or newer.
- breaking Breaking changes to `TypeDef` naming conventions occurred in `mypy-boto3-builder` 8.9.0. Specifically, argument TypeDefs may have shorter names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`), and `Extra` postfixes moved to the end.
- gotcha These packages provide *only* type stubs. For your Python code to run and interact with AWS services, you must also install the `boto3` library.
- gotcha For full IDE auto-completion and static analysis with `mypy`, explicit type annotations for `boto3.client()` calls are often necessary, as Python's type inference might not correctly resolve the specific client type from overloaded `boto3.client` signatures. Using `if TYPE_CHECKING:` with `cast` is a recommended pattern.
- gotcha The version of `mypy-boto3-lexv2-runtime` (e.g., 1.42.3) is typically synchronized with the version of `boto3` it provides types for, not with the `mypy-boto3-builder` version that generated it. This can sometimes cause confusion when correlating releases.
Install
-
pip install mypy-boto3-lexv2-runtime boto3 -
pip install 'boto3-stubs[lexv2-runtime]' boto3
Imports
- LexRuntimeV2Client
from mypy_boto3_lexv2_runtime.client import LexRuntimeV2Client
- RecognizeTextRequestTypeDef
from mypy_boto3_lexv2_runtime.type_defs import RecognizeTextRequestTypeDef
- RecognizeTextResponseTypeDef
from mypy_boto3_lexv2_runtime.type_defs import RecognizeTextResponseTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING, cast
import os
if TYPE_CHECKING:
from mypy_boto3_lexv2_runtime.client import LexRuntimeV2Client
from mypy_boto3_lexv2_runtime.type_defs import RecognizeTextRequestTypeDef, RecognizeTextResponseTypeDef
# Instantiate boto3 client (type-checked by mypy-boto3-lexv2-runtime)
# Credentials handled automatically by boto3, e.g., via AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY env vars
# or IAM roles.
client = boto3.client("lexv2-runtime")
if TYPE_CHECKING:
client = cast(LexRuntimeV2Client, client)
# Example Lex V2 Runtime operation: recognize_text
# Replace with your actual bot_id, bot_alias_id, locale_id
bot_id = os.environ.get('LEX_BOT_ID', 'YOUR_BOT_ID')
bot_alias_id = os.environ.get('LEX_BOT_ALIAS_ID', 'YOUR_BOT_ALIAS_ID')
locale_id = os.environ.get('LEX_LOCALE_ID', 'en_US')
session_id = 'test_session_id_123'
request_params: RecognizeTextRequestTypeDef = {
'botId': bot_id,
'botAliasId': bot_alias_id,
'localeId': locale_id,
'sessionId': session_id,
'text': 'Hello, I need to book a flight.'
}
try:
response: RecognizeTextResponseTypeDef = client.recognize_text(**request_params)
print("Lex V2 Response:", response)
if response.get('messages'):
print("Bot message:", response['messages'][0]['content'])
except Exception as e:
print(f"Error calling Lex V2: {e}")
# To run mypy: mypy your_script_name.py