Type annotations for aiobotocore BedrockRuntime

3.4.0 · active · verified Tue Apr 14

Type annotations for `aiobotocore` BedrockRuntime service. This library provides static type checking for `aiobotocore` clients, resources, paginators, and service-specific `TypeDefs` and `Literals` for Amazon Bedrock Runtime. It is generated by `mypy-boto3-builder` and aims to be compatible with various IDEs and type checkers like mypy and pyright. The current version is 3.4.0 and it releases in sync with `aiobotocore` updates.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `aiobotocore` with the `types-aiobotocore-bedrock-runtime` stubs for type-checked asynchronous interaction with the Amazon Bedrock Runtime service. It explicitly types the client and a response, then invokes a model and streams its output. Note that `aiobotocore` itself must be installed alongside the type stubs.

import asyncio
from typing import TYPE_CHECKING

import aiobotocore.session

if TYPE_CHECKING:
    from types_aiobotocore_bedrock_runtime.client import BedrockRuntimeClient
    from types_aiobotocore_bedrock_runtime.type_defs import InvokeModelResponseTypeDef

async def main():
    session = aiobotocore.session.get_session()
    async with session.create_client("bedrock-runtime") as client:
        client: BedrockRuntimeClient  # Explicit type hint (optional, but good practice)

        response: InvokeModelResponseTypeDef = await client.invoke_model(
            modelId="anthropic.claude-3-sonnet-20240229-v1:0",
            contentType="application/json",
            accept="application/json",
            body=b'{"prompt": "Human: Hello, world!\nAssistant:"}'
        )

        async with response["body"] as stream:
            payload = stream.iter_bytes()
            async for chunk in payload:
                print(chunk.decode())

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →