Skip to content

Lambda and Bedrock Runtime

These clients invoke existing functions and models. Install their service packages as described in Getting started.

To run rboto inside a function, see AWS Lambda.

Invoke Lambda

Import LambdaClient from rboto.

import json

from rboto import LambdaClient


async def invoke(client: LambdaClient, function_name: str) -> bytes:
    response = await client.invoke(
        function_name=function_name,
        invocation_type="RequestResponse",
        payload=json.dumps({"order_id": "order-123"}).encode(),
    )
    payload = response.payload or b""
    if response.function_error is not None:
        raise RuntimeError(f"Function failed: {response.function_error}")
    return payload

RequestResponse waits for the result. Inspect function_error even when the API call succeeds. The invocation payload is returned as bytes. For event invocation, acceptance does not mean the function has completed.

Send a Bedrock conversation

Use BedrockRuntimeClient from rboto with a model or inference-profile identifier that supports Converse and is available to your account in the configured region.

from rboto import BedrockRuntimeClient
from rboto_bedrock_runtime.types import Message


async def converse(client: BedrockRuntimeClient, model_id: str) -> None:
    message: Message = {"role": "user", "content": [{"text": "Explain this in one sentence."}]}
    response = await client.converse(model_id=model_id, messages=[message])
    print(response.output)
    print(response.usage)

Content blocks select a variant, here text. The response contains the output and usage reported by the service. invoke_model() accepts encoded bytes for model-specific payloads.

BedrockClient() manages model-related resources; BedrockRuntimeClient() handles inference. See Streaming for incremental and bidirectional operations.