{"id":10319,"library":"types-boto3-bedrock-runtime","title":"Type Annotations for boto3 Bedrock Runtime","description":"This package provides comprehensive type annotations (stubs) for the `boto3` Bedrock Runtime client, enabling static type checking with tools like `mypy`. It ensures that your interactions with the AWS Bedrock Runtime service are type-safe, catching potential errors at development time. The current version is 1.42.82, and new releases are frequent, typically aligning with `boto3` and `botocore` updates.","status":"active","version":"1.42.82","language":"en","source_language":"en","source_url":"https://github.com/youtype/mypy_boto3_builder","tags":["aws","boto3","bedrock","mypy","type-hints","stubs","ai","generative-ai"],"install":[{"cmd":"pip install types-boto3-bedrock-runtime boto3 mypy","lang":"bash","label":"Install package and dependencies"}],"dependencies":[{"reason":"This package provides type stubs for `boto3`. `boto3` must be installed for runtime execution.","package":"boto3","optional":false},{"reason":"Static type checker that consumes these type annotations.","package":"mypy","optional":true},{"reason":"Required for Python versions < 3.11 for certain typing features.","package":"typing-extensions","optional":true}],"imports":[{"note":"Type stub packages should only be imported for type checking purposes to avoid runtime overhead or errors. Use `if TYPE_CHECKING:`.","wrong":"from types_boto3_bedrock_runtime import BedrockRuntimeClient","symbol":"BedrockRuntimeClient","correct":"from typing import TYPE_CHECKING\nif TYPE_CHECKING:\n    from types_boto3_bedrock_runtime.client import BedrockRuntimeClient"},{"note":"Specific type definitions for requests/responses are found in `type_defs` and should also be conditionally imported.","symbol":"InvokeModelRequestRequestTypeDef","correct":"from typing import TYPE_CHECKING\nif TYPE_CHECKING:\n    from types_boto3_bedrock_runtime.type_defs import InvokeModelRequestRequestTypeDef"}],"quickstart":{"code":"import boto3\nimport os\nfrom typing import TYPE_CHECKING\n\n# Conditional import for type checking\nif TYPE_CHECKING:\n    from types_boto3_bedrock_runtime.client import BedrockRuntimeClient\n    from types_boto3_bedrock_runtime.type_defs import InvokeModelRequestRequestTypeDef\n\n\ndef invoke_bedrock_model(model_id: str, prompt: str):\n    # Type annotate the client\n    client: BedrockRuntimeClient = boto3.client(\n        'bedrock-runtime',\n        aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'DUMMY_KEY'),\n        aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'DUMMY_SECRET'),\n        region_name=os.environ.get('AWS_REGION', 'us-east-1')\n    )\n\n    body_content = {\"prompt\": prompt, \"max_tokens_to_sample\": 200, \"temperature\": 0.7}\n    \n    # Example of type-hinted request body (mypy will check its structure)\n    request_body: InvokeModelRequestRequestTypeDef = {\n        'body': str(body_content).encode('utf-8'), # In real use, convert dict to JSON string\n        'contentType': 'application/json',\n        'accept': 'application/json',\n        'modelId': model_id\n    }\n\n    try:\n        response = client.invoke_model(**request_body)\n        print(\"Successfully invoked model:\")\n        # Process response body (example)\n        response_body = response['body'].read().decode('utf-8')\n        print(response_body)\n    except client.exceptions.AccessDeniedException as e:\n        print(f\"Access Denied: {e}\")\n        print(\"Please ensure your AWS credentials and permissions are correctly configured.\")\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n\n\n# Example usage:\nif __name__ == \"__main__\":\n    # Replace with an actual model ID available in your region\n    # e.g., 'anthropic.claude-v2' or 'amazon.titan-text-express-v1'\n    model = \"DUMMY_MODEL_ID\" \n    user_prompt = \"Hello, how are you?\"\n    invoke_bedrock_model(model, user_prompt)\n","lang":"python","description":"This quickstart demonstrates how to initialize a `boto3` Bedrock Runtime client with type annotations. It shows the conditional import of `BedrockRuntimeClient` and `InvokeModelRequestRequestTypeDef` for static analysis and then uses the client to invoke a model. Remember to set your AWS credentials and region via environment variables or other boto3 configuration methods for actual execution."},"warnings":[{"fix":"Ensure your `mypy` configuration is up-to-date. If using Python 3.8, you must use an older version of `types-boto3-bedrock-runtime` or upgrade to Python 3.9+.","message":"Starting with version 8.12.0, `types-boto3-bedrock-runtime` and other `mypy-boto3-builder` packages migrated to PEP 561 standard packages. This means older `mypy` configurations might need adjustment, though most modern setups will handle it seamlessly. Python 3.8 support was also removed.","severity":"breaking","affected_versions":">=8.12.0"},{"fix":"If encountering `NameError` or `ImportError` for TypeDefs, check the changelog for the specific version for renaming conventions and adjust your imports/references accordingly.","message":"In version 8.9.0, there were breaking changes to how TypeDef names are generated, leading to shorter names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`). While this example is not for Bedrock Runtime, it indicates a pattern that might affect specific Bedrock Runtime TypeDefs in future updates if their names become redundant.","severity":"breaking","affected_versions":">=8.9.0"},{"fix":"Always install `boto3` alongside `types-boto3-bedrock-runtime`: `pip install boto3 types-boto3-bedrock-runtime`.","message":"This package provides only type stubs for static analysis and does not contain any runtime code. You must have the actual `boto3` library installed for your application to run.","severity":"gotcha","affected_versions":"all"},{"fix":"Always wrap imports of types from `types-boto3-*` packages within an `if TYPE_CHECKING:` block. This ensures they are only considered by type checkers and are skipped at runtime.","message":"Directly importing from `types_boto3_bedrock_runtime` at runtime (e.g., `from types_boto3_bedrock_runtime.client import BedrockRuntimeClient`) can lead to `ModuleNotFoundError` or other issues if the stub package is not designed for runtime import, or simply adds unnecessary overhead. The stubs are meant for static checkers like `mypy`.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure the package is installed (`pip install types-boto3-bedrock-runtime`) and wrap any imports of types with `from typing import TYPE_CHECKING` and `if TYPE_CHECKING:`.","cause":"Attempting to import the stub package directly at runtime, or the package is not installed.","error":"ModuleNotFoundError: No module named 'types_boto3_bedrock_runtime'"},{"fix":"Inspect the `InvokeModelRequestRequestTypeDef` (or similar `TypeDef`) definition to ensure your dictionary keys and types match precisely. `mypy` will point out the specific mismatch.","cause":"Your dictionary for a request payload does not exactly match the structure defined by the `TypeDef` in the type stubs, or you forgot to pass the required keyword arguments.","error":"TypeError: Argument of type 'dict[str, Any]' cannot be assigned to parameter of type 'InvokeModelRequestRequestTypeDef' in call"},{"fix":"Install the base `types-boto3` package (`pip install types-boto3`) in addition to `types-boto3-bedrock-runtime`, and ensure `mypy` is configured to include stub paths if necessary (though usually automatic).","cause":"You are using `boto3` without its core type stubs (`types-boto3`) or without the correct `mypy` configuration to find these stubs.","error":"error: Library \"boto3\" has no py.typed file (hint: see https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs)"}]}