Skip to content

fix(memory): allow RetrievalConfig top_k=1 (ge=1, not gt=1) - #728

Open
PratikDhanave wants to merge 1 commit into
aws:mainfrom
PratikDhanaveFork:fix/retrievalconfig-top-k-allow-one
Open

PratikDhanave wants to merge 1 commit into
aws:mainfrom
PratikDhanaveFork:fix/retrievalconfig-top-k-allow-one

Conversation

@PratikDhanave

Copy link
Copy Markdown

Summary

RetrievalConfig.top_k used Field(default=10, gt=1, le=100), which rejects top_k=1 — a legitimate request for the single best-scoring record.

Why it matters

  • The SDK itself calls self.retrieve_memories(..., top_k=1) (memory/client.py), and RetrievalConfig is a public parameter of MemorySessionManager.process_turn_with_llm, so a caller passing top_k=1 hits pydantic.ValidationError: Input should be greater than 1.
  • The docstring only says "Number of top-scoring records to return (default: 10)" — no documented minimum > 1.
  • The upper bound is already inclusive (le=100); the lower bound should be too.

Fix

top_k: int = Field(default=10, gt=1, le=100)   # ->
top_k: int = Field(default=10, ge=1, le=100)

Testing

Added test_retrieval_config_top_k_allows_one: RetrievalConfig(top_k=1) must be accepted, while top_k=0 and top_k=101 still raise.

  • Before: top_k=1 raises ValidationError (test fails).
  • After: accepted; bounds still enforced.
  • pytest tests/bedrock_agentcore/memory/test_session.py → 157 passed.

Developed with AI assistance (Claude Code): the off-by-one bound was located, the fix applied, and the regression test written and run locally. I reviewed and verified the change and take responsibility for it.

RetrievalConfig.top_k used Field(gt=1), which rejects top_k=1 — a legitimate
request for the single best-scoring record. The SDK itself calls
retrieve_memories(..., top_k=1) (client.py), and RetrievalConfig is a public
parameter of MemorySessionManager.process_turn_with_llm, so a caller passing
top_k=1 hits pydantic ValidationError. The upper bound is already inclusive
(le=100); make the lower bound inclusive too. Add a regression test.

This branch is waiting to be deployed

1 waiting deployment
manual-approval — 5c37e6fb Waiting Nov 2, 2026 by PratikDhanave via Test (services) #1683
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/xs PR size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant