Skip to content

Trouble with AWS Bedrock Metadata filtering for Agents & KB retrieve apis

0

Trouble with AWS Bedrock Metadata Filtering for Agents & KB retrieve APIs

Hi all,

I’m using AWS Bedrock Knowledge Base’s metadata filtering to allow users to query documents by their IDs (e.g., DOC-123-2025). To do this, I added a document_name tag in the metadataAttributes inside a .metadata.json file with the same name as the document, following the AWS guide.

The issue I’m running into

  • When a user queries the document ID, the KB returns the correct document, but it says it cannot find the matching ID. It will say like: "Sorry I cannot find about the ID: DOC-123-2025. The only thing I found was.. //" and then proceed to list the exact content of the document I'm looking for. For context, the ID does not exist within the text content itself, only in the metadata.
  • This occurs both when invoking the Bedrock agent via invoke_agent and when using the retrieve_and_generate API directly.
  • In the agent's Advanced Prompt Templates, the search_results placeholder variable used for KB response generation include only the content, while metadata is moved to the return response. As a result, the model doesn’t “see” the metadata attributes and cannot match the user query with the document_name.
  • The same behavior occurs when using a prompt template as part of the generationConfiguration parameter of the retrieve_and_generate API, as it also uses a search_results variable.

Question

Is there a way to persist metadata into the search results so that the model can use it when generating the final response? Essentially, I want the model to be aware of metadata attributes like document_name during prompt generation.

Any guidance or best practices for this would be appreciated!

For reference, this is the blog i'm following:
https://aws.amazon.com/blogs/machine-learning/amazon-bedrock-knowledge-bases-now-supports-metadata-filtering-to-improve-retrieval-accuracy/

I'm using the model: Claude Sonnet 3.5v2


Example: KB Response (redacted)

{
  "retrievedReferences": [
    {
      "content": {
        "text": "REDACTED_DOCUMENT_CONTENT",
        "type": "TEXT"
      },
      "metadata": {
        "document_name": "DOC_123_2025.csv"
      }
    }
  ],
  "traceId": "TRACE_ID",
  "type": "KNOWLEDGE_BASE"
}

Example: KB Response Generation Template on Agent Call (redacted)

This is how the KB response generation template is being populated on agent call:

{
  "trace": {
    "orchestrationTrace": {
      "modelInvocationInput": {
        "text": "Some instructions...Here are the search results:\n<search_results>\n  <search_result>\n    <content>\n      REDACTED_DOCUMENT_CONTENT\n    </content>\n    <source>\n      CadJ\n    </source>\n .. output instructions"
      }
    }
  }
}

*Note: I have redacted the document content and sensitive data for confidentiality. As you can see, the metadata got lost instead of populating inside the $search_results.

2 Answers
0

When using AWS Bedrock Knowledge Base's metadata filtering, there's a key challenge with how metadata is handled during response generation. The metadata attributes (like your document_name) are not automatically included in the content that gets passed to the foundation model for response generation.

Here's what's happening:

  1. Your metadata filtering is working correctly for retrieval - it's finding the right document based on the document ID in the metadata.
  2. However, when the content is passed to the model via the search_results placeholder in prompt templates, only the document content is included, not the metadata.
  3. This is why the model doesn't "see" the document ID and can't acknowledge it found the specific document you requested.

To solve this issue, you have a few options:

  1. Include document IDs in the content itself: The most straightforward solution is to include the document ID within the actual document content, so it's visible to the model during response generation.

  2. Custom prompt engineering: Modify your prompt template to explicitly include metadata. For example, you could create a custom prompt that prepends each search result with its metadata information:

Here are the search results:
Document ID: {document_name}
Content: {content}

You would need to implement this in your application code by processing the retrieved references and constructing this format before sending it to the model.

  1. Pre-processing during ingestion: During the knowledge base ingestion process, you could modify your documents to include metadata information at the beginning of each document.

  2. Post-processing in your application: After receiving the response from the model, you could modify it in your application code to include references to the document IDs that were found.

The key insight is that metadata filtering is primarily designed for retrieval accuracy (finding the right documents), not for including that metadata in the context provided to the model. You'll need to bridge this gap with one of the approaches above to ensure the model can acknowledge the document IDs in its responses.
Sources
Integrating metadata filtering within Bedrock flows | AWS re:Post
Multi-tenancy in RAG applications in a single Amazon Bedrock knowledge base with metadata filtering | AWS Machine Learning Blog

answered a year ago

0

You are seeing the expected behavior of Amazon Bedrock Knowledge Bases. Metadata filtering is fully supported during retrieval, but metadata attributes are not injected into the model prompt during response generation.

1. Why this happens

In Bedrock Knowledge Bases:

  • Metadata (from metadata.json) is used only during the vector search filtering step.
  • Retrieved documents are returned in the API response under retrievedReferences, including both content and metadata.
  • When the model is invoked, the $search_results variable in the prompt template contains only the text content, not the metadata.

This behavior is documented in:

  • Knowledge Bases Prompt Variables → $search_results contains the retrieved content text
  • RetrieveAndGenerate API → metadata is returned in retrievedReferences, not injected into the model input

Because of this separation: The retriever correctly finds the document using document_name, but the model cannot “see” that field in the prompt and therefore cannot confirm the ID.

2. Can metadata be added to $search_results?

Not at this time.

There is no configuration in Agents, Knowledge Bases, or RetrieveAndGenerate that changes how $search_results is formatted.

Metadata is intentionally kept out of the model prompt and is available only in the API response.

3. Practical Solutions

A) Add document IDs to the content itself

If the model must recognize IDs such as DOC-123-2025, include them inside the document content:

Document ID: DOC-123-2025
<original document content>

Because $search_results includes only content text, this ensures the model can reason about the ID during generation. This is the simplest and most reliable fix.

B) Use a custom RAG pipeline (manual retrieve → custom prompt → InvokeModel)

If you want the model to use metadata directly, you need to bypass the managed generation step:

  1. Call Retrieve or the retrieval portion of your Knowledge Base.
  2. Read both content and metadata from retrievedReferences.
  3. Construct your own prompt, for example:
<doc>
  <document_name>DOC_123_2025.csv</document_name>
  <content> ... </content>
</doc>
  1. Call InvokeModel directly with your prompt.

This gives full control over how metadata is presented to the model.

C) Prepend metadata during ingestion

At ingestion time, you can modify documents to include identifiers (title block, header, etc.) before chunking. This ensures the ID becomes part of the text that’s embedded and retrieved.

4. Summary

  • Bedrock does not persist metadata into $search_results today.
  • Metadata is used for retrieval filtering, not for model reasoning.
  • To make the model aware of document IDs, you must either:
    • Include IDs inside the content, or
    • Build a custom RAG pipeline that injects metadata into the prompt.

This behavior is consistent with current Knowledge Bases and RetrieveAndGenerate design.

Thank you.

Sources

Amazon Bedrock Knowledge Bases – metadata filtering https://aws.amazon.com/blogs/machine-learning/amazon-bedrock-knowledge-bases-now-supports-metadata-filtering-to-improve-retrieval-accuracy/

Knowledge Base Retrieve and Generate https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-retrieve-generate.html

Knowledge Base Retrieve API https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-retrieve.html

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.