Amazon BedrockΒΆ
Amazon Bedrock models can be called through the Bedrock Runtime Converse and Converse Stream APIs using the actions below.
Install the optional extra (pulls boto3):
pip install "burr[bedrock]"
IAM permissions must allow bedrock:InvokeModel / streaming equivalents for your
chosen models; see AWS documentation for your account and model IDs.
- class burr.integrations.bedrock.BedrockAction(
- model_id: str,
- input_mapper: StateToPromptMapper,
- reads: list[str],
- writes: list[str],
- name: str = 'bedrock_invoke',
- region: str | None = None,
- guardrail_id: str | None = None,
- guardrail_version: str | None = None,
- inference_config: dict[str, Any] | None = None,
- max_retries: int = 3,
- client: Any | None = None,
Bases:
_BedrockBase,SingleStepActionAction that invokes Amazon Bedrock models using the Converse API.
- Parameters:
model_id β Bedrock model identifier (e.g. Anthropic Claude on Bedrock).
input_mapper β Callable mapping
Stateto Bedrockmessages/ optionalsystemkeys.reads β State keys this action reads.
writes β State keys to update (typically include
response).name β Action name for the graph.
region β AWS region for the Bedrock runtime client (optional).
guardrail_id β If set,
guardrail_versionmust also be set explicitly.guardrail_version β Guardrail version string (use
DRAFTonly when intended).inference_config β Passed as
inferenceConfig; if omitted, defaults to amaxTokenslimit. Pass an empty dict explicitly to send an empty config.max_retries β Botocore retry configuration for the runtime client.
client β Optional pre-built
bedrock-runtimeclient (for tests or injection).
Use
run_and_update()to run the model and merge outputs into state.
- class burr.integrations.bedrock.BedrockStreamingAction(
- model_id: str,
- input_mapper: StateToPromptMapper,
- reads: list[str],
- writes: list[str],
- name: str = 'bedrock_stream',
- region: str | None = None,
- guardrail_id: str | None = None,
- guardrail_version: str | None = None,
- inference_config: dict[str, Any] | None = None,
- max_retries: int = 3,
- client: Any | None = None,
Bases:
_BedrockBase,StreamingActionStreaming Bedrock action using the Converse Stream API.
Parameters match
BedrockActionexcept the defaultnameisbedrock_stream. Yields chunk dicts fromstream_run()and merges the final response inupdate().- stream_run(
- state: State,
- **run_kwargs,
Streaming action
stream_runis different than standard action run. It: 1. streams in an intermediate result (the dict output) 2. yields the final result at the endNote that the user, in this case, is responsible for joining the result.
For instance, you could have:
def stream_run(state: State) -> Generator[dict, None, dict]: buffer = [] # you might want to be more efficient than simple strcat for token in query(state['prompt']): yield {'response' : token} buffer.append(token) yield {'response' : "".join(buffer)}
This would utilize a simple string buffer (implemented by a list) to store the results and then join them at the end. We return the final result.
- Parameters:
state β State to run the action on
run_kwargs β parameters passed to the run function β these are specified by inputs
- Returns:
A generator that streams in a result and returns the final result
- update(result: dict, state: State) StateΒΆ
Performs a state update given a result and the current state. Returns a new, modified
State(recall state is immutable β simply changing the state in place will not work).In the context of Burr, this is only applied in the two-step actions, where the
runand update() functions are separate. The function-based APIs for Burr use the SingleStepAction class, which performs them both at once. This is not (yet) exposed as an interface for users to extend.- Parameters:
result β Result of a function executing on the state
state β State to update
- Returns:
A new, modified state.