Skip to content

fix: guard against None score/metadata/content in Bedrock KB retrieval#37681

Open
lavkeshdwivedi wants to merge 1 commit into
langgenius:mainfrom
lavkeshdwivedi:fix/bedrock-kb-retrieval-none-guards
Open

fix: guard against None score/metadata/content in Bedrock KB retrieval#37681
lavkeshdwivedi wants to merge 1 commit into
langgenius:mainfrom
lavkeshdwivedi:fix/bedrock-kb-retrieval-none-guards

Conversation

@lavkeshdwivedi

Copy link
Copy Markdown

Summary

The AWS Bedrock Knowledge Base Retrieve API documents score and metadata as optional fields in each RetrievalResult. The current ExternalDatasetTestService.knowledge_retrieval() parser crashes on any result that omits either of these fields:

Location Bug Crash type
retrieval_result.get("score") < threshold None < float when score absent TypeError
retrieval_result.get("metadata").get(...) None.get(...) when metadata absent AttributeError
retrieval_result.get("content").get("text") None.get(...) when content absent AttributeError

Fix

Extract each field into a local variable with a safe fallback before use:

score = retrieval_result.get("score") or 0.0
metadata = retrieval_result.get("metadata") or {}
content = retrieval_result.get("content") or {}

Test plan

  • Added test_knowledge_retrieval_should_handle_missing_score — missing score treated as 0.0, no TypeError
  • Added test_knowledge_retrieval_should_handle_missing_metadata — missing metadata yields title=None, no AttributeError
  • Added test_knowledge_retrieval_should_handle_missing_content — missing content yields content=None, no AttributeError
  • All existing tests continue to pass

AWS Bedrock Knowledge Base retrieve results have optional fields:
- score is optional per the API spec
- metadata is optional per the API spec
- content.text access was unsafe if content key was absent

Before this fix, a result missing any of these fields would crash with
TypeError (None < float) or AttributeError (None.get(...)).

Extract each field to a local variable with a safe fallback before use.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant