Type Stubs for boto3 LexRuntimeV2

1.42.3 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `boto3` LexRuntimeV2 client and perform a `recognize_text` operation with explicit type annotations provided by `mypy-boto3-lexv2-runtime`. It includes `TYPE_CHECKING` guards for runtime performance and `mypy` compatibility, as well as placeholders for necessary AWS LexV2 bot parameters.

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

view raw JSON →