bedrock

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package bedrock is an AWS Bedrock Runtime client for Bedrock InvokeModel and native Converse/ConverseStream. It routes the selected native dialect to the corresponding model path and signs every request with AWS Signature Version 4.

Credentials are AWS SigV4, not a bearer key, so a Bedrock client is constructed directly via New (auto.New cannot supply SigV4 credentials and errors to here).

Index

Constants

View Source
const (
	CachePointTTL5m = "5m"
	CachePointTTL1h = "1h"
)

Variables

This section is empty.

Functions

func New

func New(creds auth.SigV4Credentials, region string, options ...Option) (inference.Client, error)

New constructs a Bedrock client bound to region, signing with creds. It fails closed with *ConfigError when the region or either mandatory credential field (AccessKeyID, SecretAccessKey) is empty — no Client and no network object are created. The session token is optional (used for temporary credentials).

func NewCounter

func NewCounter(creds auth.SigV4Credentials, region string, options ...Option) (contextcount.ContextCounter, error)

NewCounter constructs an exact provider counter bound to one AWS region.

Types

type BodyTransformError

type BodyTransformError struct {
	Err error
}

BodyTransformError wraps a failure to turn the Anthropic Messages body into the Bedrock InvokeModel body (unmarshal, field rewrite, or re-marshal). It is kept distinct from transport/API errors so a caller can tell a local encode fault from a wire fault. Err is the underlying cause.

func (*BodyTransformError) Error

func (e *BodyTransformError) Error() string

func (*BodyTransformError) Unwrap

func (e *BodyTransformError) Unwrap() error

type CachePointOptions

type CachePointOptions struct {
	Type string
	TTL  string
}

CachePointOptions selects a native Converse cachePoint content block.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a region-bound Bedrock inference client. It owns one SigV4 signer (built from the caller's credentials) and one http.Client, and is safe for concurrent use (both are immutable after construction). Connection binding is by provider+region: a request whose Model.Provider is not ProviderBedrock is rejected pre-I/O with *failure.ModelMismatchError.

func (*Client) Invoke

func (c *Client) Invoke(ctx context.Context, req inference.Request) (*inference.Response, error)

Invoke sends a non-streaming request in the selected Bedrock dialect. Ordered, all pre-I/O guards first: provider binding, model validation, API-format selection, local encoding, request construction, SigV4 signing, HTTP error mapping, and response decoding.

func (*Client) Stream

Stream sends a native ConverseStream request. Anthropic InvokeModel streaming remains intentionally unsupported because it uses a different Bedrock wire contract and must not be silently switched to Converse.

type ConfigError

type ConfigError struct {
	Field  string
	Reason string
}

ConfigError is a fail-closed rejection of an invalid bedrock.New configuration: an empty AWS region or empty SigV4 credentials. No Client is returned and no network object is created. Field names the offending input; Reason explains the constraint. Carries no secret (never the credential values themselves).

func (*ConfigError) Error

func (e *ConfigError) Error() string

type Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter is a separately constructed exact Bedrock CountTokens client. Keeping it separate from Client prevents ordinary inference composition from accidentally acquiring the optional ContextCounter capability.

func (*Counter) CountContext

func (c *Counter) CountContext(ctx context.Context, req inference.Request) (contextcount.ContextCount, error)

CountContext sends the byte-identical InvokeModel body in AWS's base64 binary CountTokens envelope. Unsupported models remain provider API errors; this exact counter never falls back to an estimate.

func (*Counter) CounterCapability

func (c *Counter) CounterCapability() contextcount.CounterCapability

CounterCapability declares the CountTokens endpoint as the same region-routed Bedrock Runtime security boundary as InvokeModel.

type CounterEndpointError

type CounterEndpointError struct {
	Reason CounterEndpointReason
}

CounterEndpointError rejects unsafe routing without retaining credentials or provider input from the raw endpoint.

func (*CounterEndpointError) Error

func (e *CounterEndpointError) Error() string

type CounterEndpointReason

type CounterEndpointReason string

CounterEndpointReason classifies an endpoint that cannot safely receive a complete request. It deliberately carries no rejected endpoint text.

const (
	CounterEndpointMalformed           CounterEndpointReason = "malformed endpoint"
	CounterEndpointMissingHost         CounterEndpointReason = "missing endpoint host"
	CounterEndpointCredentials         CounterEndpointReason = "endpoint contains credentials"
	CounterEndpointUnsupportedScheme   CounterEndpointReason = "unsupported endpoint scheme"
	CounterEndpointInsecureTransport   CounterEndpointReason = "plaintext endpoint is not loopback"
	CounterEndpointNonASCIIHost        CounterEndpointReason = "endpoint host is not ASCII"
	CounterEndpointInvalidHost         CounterEndpointReason = "invalid endpoint host"
	CounterEndpointUnexpectedComponent CounterEndpointReason = "endpoint contains an unexpected path, query, or fragment"
)

type CounterRequestError

type CounterRequestError struct {
	Reason CounterRequestReason
	Err    error
}

CounterRequestError reports a local CountTokens envelope failure. It never retains request bytes, model ids, or the rejected body length.

func (*CounterRequestError) Error

func (e *CounterRequestError) Error() string

func (*CounterRequestError) Unwrap

func (e *CounterRequestError) Unwrap() error

type CounterRequestReason

type CounterRequestReason string

CounterRequestReason classifies a local CountTokens request-envelope failure.

const (
	CounterRequestBodyTooLarge     CounterRequestReason = "InvokeModel body exceeds 25000000 bytes"
	CounterRequestEnvelopeEncoding CounterRequestReason = "envelope encoding failed"
)

type CounterResponseError

type CounterResponseError struct {
	Reason CounterResponseReason
	Err    error
}

CounterResponseError omits provider-controlled bytes from Error while its typed cause remains available through errors.As.

func (*CounterResponseError) Error

func (e *CounterResponseError) Error() string

func (*CounterResponseError) Unwrap

func (e *CounterResponseError) Unwrap() error

type CounterResponseReason

type CounterResponseReason string

CounterResponseReason classifies a successful response that cannot produce a trustworthy normalized input-token count.

const (
	CounterResponseMalformed      CounterResponseReason = "malformed response"
	CounterResponseMissingCount   CounterResponseReason = "missing inputTokens"
	CounterResponseInvalidCount   CounterResponseReason = "invalid inputTokens"
	CounterResponseDuplicateField CounterResponseReason = "duplicate inputTokens"
	CounterResponseBodyTooLarge   CounterResponseReason = "response body too large"
)

type CounterStateError

type CounterStateError struct {
	Reason CounterStateReason
}

CounterStateError rejects invalid local state before encoding or I/O.

func (*CounterStateError) Error

func (e *CounterStateError) Error() string

type CounterStateReason

type CounterStateReason string

CounterStateReason classifies an unusable CountTokens counter boundary.

const (
	CounterStateNilReceiver          CounterStateReason = "nil counter"
	CounterStateNilContext           CounterStateReason = "nil context"
	CounterStateMissingEndpoint      CounterStateReason = "missing endpoint"
	CounterStateMissingRegion        CounterStateReason = "missing region"
	CounterStateMissingAuthenticator CounterStateReason = "missing authenticator"
	CounterStateMissingHTTPDoer      CounterStateReason = "missing HTTP doer"
	CounterStateInvalidTimeout       CounterStateReason = "invalid timeout"
)

type GuardrailOptions

type GuardrailOptions struct {
	Identifier           string
	Version              string
	Trace                string
	StreamProcessingMode string
}

GuardrailOptions configures the native Converse guardrailConfig object.

type Option

type Option func(*config)

Option customizes a Bedrock client. Options affect native Converse requests; the existing Anthropic-on-Bedrock InvokeModel path remains byte-compatible.

func WithAdditionalModelRequestFields

func WithAdditionalModelRequestFields(fields json.RawMessage) Option

WithAdditionalModelRequestFields adds documented provider-specific Converse request fields. The value must be a JSON object; it is merged with the reasoning object when WithReasoning is also present.

func WithAdditionalModelResponseFieldPaths

func WithAdditionalModelResponseFieldPaths(paths ...string) Option

WithAdditionalModelResponseFieldPaths asks Bedrock for documented additional response fields.

func WithGuardrail

func WithGuardrail(options GuardrailOptions) Option

WithGuardrail configures the native Converse guardrail request object.

func WithPerformanceLatency

func WithPerformanceLatency(latency PerformanceLatency) Option

WithPerformanceLatency sets the native performanceConfig latency value.

func WithPromptCachePoint

func WithPromptCachePoint(options CachePointOptions) Option

WithPromptCachePoint adds a native cachePoint block at the end of the system content when present, or at the end of the final message otherwise.

func WithReasoning

func WithReasoning(options ReasoningOptions) Option

WithReasoning configures native model reasoning controls.

func WithRequestMetadata

func WithRequestMetadata(metadata map[string]string) Option

WithRequestMetadata attaches the native requestMetadata map. The map is copied when the option is applied.

func WithServiceTier

func WithServiceTier(tier ServiceTier) Option

WithServiceTier sets the native serviceTier type.

type OptionError

type OptionError struct {
	Reason string
	Err    error
}

OptionError reports a local provider-option encoding failure without retaining request bytes or credential material.

func (*OptionError) Error

func (e *OptionError) Error() string

func (*OptionError) Unwrap

func (e *OptionError) Unwrap() error

type PerformanceLatency

type PerformanceLatency string

PerformanceLatency selects Bedrock's performanceConfig latency mode.

const (
	PerformanceLatencyStandard  PerformanceLatency = "standard"
	PerformanceLatencyOptimized PerformanceLatency = "optimized"
)

type ReasoningOptions

type ReasoningOptions struct {
	Type         string
	BudgetTokens *int
}

ReasoningOptions controls the model-specific reasoning object carried inside Bedrock's additionalModelRequestFields. The native Converse envelope keeps this field model-specific; these fields match the documented Anthropic thinking shape used by Claude models on Bedrock.

type RequestBuildError

type RequestBuildError struct {
	Err error
}

RequestBuildError is a failure to CONSTRUCT the outbound HTTP request (a malformed endpoint/URL), kept distinct from *failure.NetworkError (reserved for transport failures out of hc.Do) so errors.As never misclassifies a config bug as a transport fault. Unwrap exposes the net/http cause.

func (*RequestBuildError) Error

func (e *RequestBuildError) Error() string

func (*RequestBuildError) Unwrap

func (e *RequestBuildError) Unwrap() error

type ServiceTier

type ServiceTier string

ServiceTier selects Bedrock's documented request service tier.

const (
	ServiceTierDefault  ServiceTier = "default"
	ServiceTierPriority ServiceTier = "priority"
	ServiceTierFlex     ServiceTier = "flex"
	ServiceTierReserved ServiceTier = "reserved"
)

type StreamingNotSupportedError

type StreamingNotSupportedError struct{}

StreamingNotSupportedError is returned only for Anthropic-on-Bedrock InvokeModel requests. Native ConverseStream uses the event-stream codec.

func (*StreamingNotSupportedError) Error

type ThinkingBudgetError

type ThinkingBudgetError struct {
	MaxTokens    int
	BudgetTokens int
}

ThinkingBudgetError is a fail-closed rejection, before any I/O, of a Converse request whose reasoning budget is not smaller than its output cap. Anthropic states the rule as `max_tokens` must be greater than `thinking.budget_tokens` and enforces it with an HTTP 400; Anthropic-on-Bedrock inherits it.

It is caught here rather than by the request schema because the two values live in different top-level objects and one of them is opaque: inferenceConfig.maxTokens is modelled, additionalModelRequestFields is a Smithy Document whose contents no schema constrains. Both numbers are carried on the error because a message that named only one of them would not say what to change.

func (*ThinkingBudgetError) Error

func (e *ThinkingBudgetError) Error() string

type UnsupportedAPIFormatError

type UnsupportedAPIFormatError struct {
	APIFormat model.APIFormat
}

UnsupportedAPIFormatError is a fail-closed rejection, before any I/O, of a request whose Model.APIFormat this client cannot honor. Bedrock currently supports the Anthropic-on-Bedrock InvokeModel dialect and native Converse; this error protects future/unknown formats from accidental fallback.

func (*UnsupportedAPIFormatError) Error

func (e *UnsupportedAPIFormatError) Error() string

type UnsupportedImageSourceError

type UnsupportedImageSourceError struct {
	SourceType string
}

UnsupportedImageSourceError is a fail-closed rejection, before any I/O, of an Anthropic image block whose `source.type` Bedrock's InvokeModel contract cannot honor. Anthropic's first-party API accepts a remote {"type":"url"} source, so the shared anthropicapi encoder emits one, but Bedrock takes inline bytes only (its Converse ImageSource union is bytes | s3Location, and the Anthropic-on- Bedrock body accepts "base64"); the URL would otherwise reach Bedrock and draw an opaque HTTP 400. SourceType names the rejected discriminator and never the URL itself, so the error carries no caller content.

func (*UnsupportedImageSourceError) Error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL