The documentation shows how to configure for guardrails using the generateText({…}) invocation. Please let me know if the streamText({…}) can be used with the same configuration for guardrails, or if there is difference with streamText({…}) .
Yes, I’d expect the same Bedrock providerOptions shape to work with streamText. The docs show generateText, but the guardrail config is a provider option on the Bedrock model call, not something specific to generateText.
Example:
import { bedrock, type AmazonBedrockLanguageModelOptions } from "@ai-sdk/amazon-bedrock"
import { streamText } from "ai"
const result = streamText({
model: bedrock("anthropic.claude-3-sonnet-20240229-v1:0"),
prompt: "Write a story about space exploration.",
providerOptions: {
bedrock: {
guardrailConfig: {
guardrailIdentifier: "1abcd2ef34gh",
guardrailVersion: "1",
trace: "enabled",
streamProcessingMode: "async",
},
} satisfies AmazonBedrockLanguageModelOptions,
},
})
return result.toTextStreamResponse()
The main thing I’d watch is streamProcessingMode. For streaming responses, async is usually the setting I’d try first because it lets streaming continue while Bedrock evaluates guardrails. If you need stricter blocking behavior before output reaches the client, test the other mode and confirm the latency/behavior matches what you want.
The Bedrock provider docs are here:
If it does not behave the same with streamText, I’d log the Bedrock error body and the AI SDK / @ai-sdk/amazon-bedrock versions, because that would likely be a provider-specific streaming issue rather than a different top-level API shape.