fix: guard against None score/metadata/content in Bedrock KB retrieval#37681
Open
lavkeshdwivedi wants to merge 1 commit into
Open
fix: guard against None score/metadata/content in Bedrock KB retrieval#37681lavkeshdwivedi wants to merge 1 commit into
lavkeshdwivedi wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The AWS Bedrock Knowledge Base Retrieve API documents
scoreandmetadataas optional fields in eachRetrievalResult. The currentExternalDatasetTestService.knowledge_retrieval()parser crashes on any result that omits either of these fields:retrieval_result.get("score") < thresholdNone < floatwhen score absentTypeErrorretrieval_result.get("metadata").get(...)None.get(...)when metadata absentAttributeErrorretrieval_result.get("content").get("text")None.get(...)when content absentAttributeErrorFix
Extract each field into a local variable with a safe fallback before use:
Test plan
test_knowledge_retrieval_should_handle_missing_score— missing score treated as 0.0, no TypeErrortest_knowledge_retrieval_should_handle_missing_metadata— missing metadata yieldstitle=None, no AttributeErrortest_knowledge_retrieval_should_handle_missing_content— missing content yieldscontent=None, no AttributeError