AWSAWS Certified Generative AI Developer - Professional2026-102.4 Implement FM API integrations
Preserve stream events through client delivery
Completed by the knowledge check
Learning objective
Stream Bedrock responses through supported streaming APIs and WebSocket, SSE, or chunked-transfer delivery paths.
Core idea
Streaming is an end-to-end delivery contract. Bedrock may produce incremental events, but a buffering backend or gateway can still deliver the entire answer only after generation ends. Choose the model operation and client transport together, then verify that each intermediate hop forwards data as it arrives.
A stream also changes completion semantics. Receiving the first bytes proves progress, not success. The application needs distinct delta, completion, and error events, and the client must recognize an interrupted stream as incomplete. A successful HTTP handshake cannot establish that the model finished successfully.
Keep the upstream Bedrock event protocol behind an adapter. Deliver a stable application protocol to clients so a model change does not force each client to interpret model-native events.
How it works
Select a supported Bedrock stream
Use ConverseStream for the common message interface or InvokeModelWithResponseStream for the selected model’s native inference format. Both require a target that supports streaming. AWS documents checking GetFoundationModel and its responseStreamingSupported field; also verify that the target supports the chosen API. A streaming-capable model does not make every inference interface interchangeable.
The streaming IAM action is bedrock:InvokeModelWithResponseStream. Use an AWS SDK that can consume the stream. The AWS CLI does not support these Bedrock streaming operations, so a successful nonstreaming CLI invocation is not an end-to-end streaming test.
ConverseStream returns typed events including messageStart, contentBlockStart, contentBlockDelta, contentBlockStop, messageStop, and metadata. Associate deltas with their contentBlockIndex; forward supported text deltas while preserving the distinction between text and other content. messageStop.stopReason explains why generation stopped, and metadata carries usage and metrics.
Do not discard nontext events by treating every event as a token string. Tool arguments or structured content can arrive incrementally and require assembly before validation or execution. See 2.4.3 for the response acceptance boundary.
InvokeModelWithResponseStream returns payload events with chunk.bytes plus possible error events. Decode the bytes according to the selected model’s native streaming response format. This is not the Converse event schema, and transport chunks do not necessarily correspond to one application message or one complete JSON object.
Both streaming APIs expose errors such as modelStreamErrorException within the stream. Consume the entire event sequence and make errors visible through the application protocol. Receiving HTTP 200 before such an event does not convert the later error into success.
Choose a delivery path that forwards increments
For a one-way live answer, server-sent events (SSE) provide named application events over an HTTP response using Content-Type: text/event-stream. Encode complete SSE records and flush them as they become available. Include a job or response identifier, sequence information if needed, and an explicit terminal event.
The adapter forwards each delta as an SSE record when it arrives, and the stream ends in an explicit terminal or error event.
For API Gateway, response streaming is supported on REST APIs with HTTP_PROXY or AWS_PROXY integrations. Set the integration response transfer mode to STREAM; the default is BUFFERED. It does not apply to HTTP APIs, and request streaming is not supported by this feature.
A Lambda proxy stream requires API Gateway to invoke Lambda with InvokeWithResponseStream and use the streaming integration URI ending in response-streaming-invocations. Lambda must emit the required metadata JSON followed by an eight-null-byte delimiter within the first 16 KB, then the streamed payload. A normal buffered Lambda proxy response is a different output contract. Setting STREAM without matching the invoke operation and output format is insufficient.
For chunked-transfer delivery, use the same supported streaming integration and define how the client frames application records inside the body. AWS documents that API Gateway appends Transfer-Encoding: chunked when the streaming Lambda response does not include that header or Content-length. Chunking moves bytes incrementally; it does not supply SSE event names or JSON record boundaries. Select explicit framing that the receiving client understands.
Lambda function URLs offer another HTTP delivery path for SSE or chunked responses. Set the function URL invoke mode to RESPONSE_STREAM, which uses InvokeWithResponseStream; the default BUFFERED mode uses Invoke. Function URLs do not support response streaming within a VPC environment. For VPC access, AWS documents using the SDK’s InvokeWithResponseStream operation through an interface VPC endpoint for Lambda.
For bidirectional interaction or independently pushed updates, use an API Gateway WebSocket API. The backend sends messages with the API Gateway Management API’s PostToConnection, using ConnectionId and Data. Build the management endpoint for the connection’s API and stage, and use signed requests with the required IAM authorization. Handle GoneException when the client connection no longer exists.
Bound the connection and completion behavior
API Gateway REST response streams can run for up to 15 minutes. Their idle timeouts are five minutes for Regional and private endpoints and 30 seconds for edge-optimized endpoints. Choose the endpoint and heartbeat strategy from the expected silent intervals; also account for intermediaries with shorter timeouts. These gateway limits do not extend the backend’s execution budget.
API Gateway WebSocket route integrations retain a maximum 29-second integration timeout, while connections can last up to two hours with a 10-minute idle timeout. Accept a request and detach longer work rather than holding the route integration open for the connection’s lifetime. See 2.4.2 for durable jobs.
WebSocket frames are limited to 32 KB and message payloads to 128 KB. The frame limit also applies to @connections commands. Keep application messages below the frame limit unless you implement supported fragmentation; do not send the accumulated answer as one oversized callback.
Apply bounded buffering and a policy for disconnected or slow clients. AWS notes that Lambda might continue executing when the client-to-API-Gateway or API-Gateway-to-Lambda streaming connection closes due to timeout. A dropped browser connection therefore does not prove inference was canceled. Stop forwarding, attempt supported cancellation where available, and record the actual execution outcome.
Key distinctions and trade-offs
Upstream events versus downstream transport: Bedrock events describe generation. SSE, WebSocket, and chunked transfer describe delivery to your client. An adapter must preserve completion and error meaning across that boundary.
Progress versus acceptance: Partial text can improve perceived responsiveness but has not passed whole-result validation. Do not perform business actions from incomplete structured output.
Connection lifetime versus invocation lifetime: A long-lived WebSocket connection does not lengthen a route’s integration timeout. A REST response stream does not extend Lambda runtime.
SSE fits one-way event delivery; WebSocket supports messages in both directions and requires connection management. Chunked delivery fits clients with their own framing. Choose from the interaction contract and verified infrastructure support, then measure first delivered content and final accepted completion separately.
Architecture / decision scenario
A support assistant serves a browser, a bidirectional agent console, and a command-line client. One backend consumes ConverseStream, translates text deltas into application records, and retains stop reason, usage, and terminal errors. A native-model adapter separately decodes InvokeModelWithResponseStream without exposing its payload format to clients.
The browser receives SSE through a REST proxy integration configured as STREAM. Its Lambda path uses InvokeWithResponseStream and the required output delimiter. The command-line client receives framed records through a chunked response on that supported path. The console uses WebSocket callbacks with small PostToConnection payloads, while longer work runs outside the route integration.
If generation fails after partial output, each client receives an error outcome or detects the missing completion record. The service does not append a fallback model’s answer to the same partial response; see 2.4.5 for retry boundaries. A disconnect clears delivery state while execution tracking establishes whether the backend actually stopped.
Common exam traps
- Assuming a Bedrock stream passes through a
BUFFEREDintegration incrementally. - Applying REST response streaming settings to an HTTP API.
- Treating native
chunk.bytesas Converse events. - Declaring completion from HTTP 200 or the first text delta.
- Equating WebSocket connection duration with route integration timeout.
- Sending oversized callback messages or assuming disconnect cancels Lambda.
What to remember
Verify model support, consume typed stream events, and configure every delivery hop for streaming. Preserve explicit completion and errors, respect transport and execution limits, and treat partial results as incomplete until the application accepts them.
Knowledge check
A REST API Lambda proxy integration has its response transfer mode set to STREAM, and the Lambda function consumes ConverseStream. Yet the browser still receives the answer only when generation ends, because the integration uses the standard Lambda invocation URI and the handler returns an ordinary proxy response. What else must change?
InvokeModelWithResponseStreamresponse-streaming-invocations URI and emit the streaming output formatSTREAM alone is not enough: API Gateway must invoke Lambda with InvokeWithResponseStream through the streaming integration URI, and the function must write the metadata JSON and eight-null-byte delimiter before the payload. HTTP APIs do not support this response streaming, and Bedrock is already producing increments.A support assistant has two clients. A browser only needs to receive named progress, answer and completion events for each question. An agent console must send instructions and receive updates over one long-lived connection. Which delivery fits each client?
PostToConnection callbacks and connection management. Chunked transfer has no event names, and an SSE response carries nothing back from the client.A ConverseStream call returns HTTP 200, the client receives several text deltas, and then the stream delivers a modelStreamErrorException before any messageStop. What should the application tell the client?
An agent console uses an API Gateway WebSocket API. One route’s integration starts an analysis that runs for several minutes and returns a 200 KB result. How should the backend deliver it?
PostToConnection call when done@connections commands, so results go out as bounded callbacks rather than one oversized message.A browser closes its tab while a REST API response stream from Lambda is still sending tokens. What can the backend conclude about the inference?
References
- AWS, ConverseStream
- AWS, InvokeModelWithResponseStream
- AWS, Stream the integration response for your proxy integrations in API Gateway
- AWS, Set up a Lambda proxy integration with payload response streaming in API Gateway
- AWS, Use @connections commands in your backend service
- AWS, Quotas for configuring and running a WebSocket in API Gateway
- AWS, Invoking a response streaming enabled function using Lambda function URLs
- AWS, Response streaming for Lambda functions