Skip to content

retrieve_and_generate_stream

Operation

Async only

This operation uses an event stream, so only the asynchronous client supports it.

retrieve_and_generate_stream async

retrieve_and_generate_stream(request: RetrieveAndGenerateStreamInput | RetrieveAndGenerateStreamInputDict | None = None, /, *, plugins: list[Plugin] | None = None, **kwargs: Unpack[RetrieveAndGenerateStreamInputDict]) -> AsyncOutputEventStream[RetrieveAndGenerateStreamResponseOutput, RetrieveAndGenerateStreamOutput]

Queries a knowledge base and generates responses based on the retrieved results, with output in streaming format.

Note

This API cannot be used with managed knowledge bases. Use AgenticRetrieveStream or Retrieve with managed knowledge bases.

Note: The CLI doesn't support streaming operations in Amazon Bedrock, including InvokeModelWithResponseStream.

This operation requires permission for the bedrock:RetrieveAndGenerate action.

Parameters:

Name Type Description Default
request RetrieveAndGenerateStreamInput | RetrieveAndGenerateStreamInputDict | None

The input as a model or dictionary. Pass this or keyword arguments, not both.

None
plugins list[Plugin] | None

A list of callables that modify the configuration dynamically. Changes made by these plugins only apply for the duration of the operation execution and will not affect any other operation invocations.

None
**kwargs Unpack[RetrieveAndGenerateStreamInputDict]

The input fields as keyword arguments.

{}

Returns:

Type Description
AsyncOutputEventStream[RetrieveAndGenerateStreamResponseOutput, RetrieveAndGenerateStreamOutput]

An AsyncOutputEventStream for server-to-client streaming.

Input

Pass the input as keyword arguments, a positional dictionary, or a positional model object. Pass plugins separately as a keyword argument.

RetrieveAndGenerateStreamInputDict

Bases: TypedDict

Dictionary form of RetrieveAndGenerateStreamInput.

Attributes

input instance-attribute

Contains the query to be made to the knowledge base.

retrieve_and_generate_configuration instance-attribute
retrieve_and_generate_configuration: RetrieveAndGenerateConfiguration | RetrieveAndGenerateConfigurationDict | None

Contains configurations for the knowledge base query and retrieval process. For more information, see Query configurations.

session_configuration instance-attribute

Contains details about the session with the knowledge base.

session_id instance-attribute
session_id: str | None

The unique identifier of the session. When you first make a RetrieveAndGenerate request, Amazon Bedrock automatically generates this value. You must reuse this value for all subsequent requests in the same conversational session. This value allows Amazon Bedrock to maintain context and knowledge from previous interactions. You can't explicitly set the sessionId yourself.

user_context instance-attribute
user_context: UserContext | UserContextDict | None

Contains information about the user making the request. This is used for access control filtering to ensure that retrieval results only include documents the user is authorized to access.

RetrieveAndGenerateStreamInput dataclass

Bases: _SerdeShape

Dataclass for RetrieveAndGenerateStreamInput structure.

Attributes

input class-attribute instance-attribute
input: RetrieveAndGenerateInput | None = ...

Contains the query to be made to the knowledge base.

retrieve_and_generate_configuration class-attribute instance-attribute
retrieve_and_generate_configuration: RetrieveAndGenerateConfiguration | None = ...

Contains configurations for the knowledge base query and retrieval process. For more information, see Query configurations.

session_configuration class-attribute instance-attribute
session_configuration: RetrieveAndGenerateSessionConfiguration | None = ...

Contains details about the session with the knowledge base.

session_id class-attribute instance-attribute
session_id: str | None = ...

The unique identifier of the session. When you first make a RetrieveAndGenerate request, Amazon Bedrock automatically generates this value. You must reuse this value for all subsequent requests in the same conversational session. This value allows Amazon Bedrock to maintain context and knowledge from previous interactions. You can't explicitly set the sessionId yourself.

user_context class-attribute instance-attribute
user_context: UserContext | None = ...

Contains information about the user making the request. This is used for access control filtering to ensure that retrieval results only include documents the user is authorized to access.

Output

This operation returns an AsyncOutputEventStream for server-to-client streaming.

Event Stream Structure

Output Event Type

RetrieveAndGenerateStreamResponseOutput

Initial Response Structure

RetrieveAndGenerateStreamOutput dataclass

Bases: _SerdeShape

Dataclass for RetrieveAndGenerateStreamOutput structure.

Attributes
response_metadata class-attribute instance-attribute
response_metadata: ResponseMetadata = ...

Metadata about the response that produced this output. Use this to recover the request identifiers a service's support team needs in order to investigate a call. Members of the metadata are individually optional.

session_id instance-attribute
session_id: str

The session ID.