Skip to content

Bedrock Agent Runtime  >  Operations  >  retrieve_and_generate

retrieve_and_generate

Operation

retrieve_and_generate async

retrieve_and_generate(input: RetrieveAndGenerateOperationInput, plugins: list[Plugin] | None = None) -> RetrieveAndGenerateOperationOutput

Queries a knowledge base and generates responses based on the retrieved results and using the specified foundation model or inference profile. The response only cites sources that are relevant to the query.

Note

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

Parameters:

Name Type Description Default
input RetrieveAndGenerateOperationInput

An instance of RetrieveAndGenerateOperationInput.

required
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

Returns:

Type Description
RetrieveAndGenerateOperationOutput

An instance of RetrieveAndGenerateOperationOutput.

Input

RetrieveAndGenerateOperationInput dataclass

Dataclass for RetrieveAndGenerateOperationInput structure.

Attributes

input class-attribute instance-attribute
input: RetrieveAndGenerateInput | None = field(repr=False, default=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 = 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 = None

Contains details about the session with the knowledge base.

session_id class-attribute instance-attribute
session_id: str | None = 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 = field(repr=False, default=None)

Contains information about the user making the request. Use this to pass user identity information for access control filtering, so that retrieval results only include documents the user is authorized to access.

Output

RetrieveAndGenerateOperationOutput dataclass

Dataclass for RetrieveAndGenerateOperationOutput structure.

Attributes

citations class-attribute instance-attribute
citations: list[Citation] | None = None

A list of segments of the generated response that are based on sources in the knowledge base, alongside information about the sources.

guardrail_action class-attribute instance-attribute
guardrail_action: GuadrailAction | None = None

Specifies if there is a guardrail intervention in the response.

output class-attribute instance-attribute
output: RetrieveAndGenerateOutput = field(repr=False)

Contains the response generated from querying the knowledge base.

session_id instance-attribute
session_id: str

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.