Skip to main content
Knowledge search returns a typed result you can loop over and score.

Quick Start

1

Loop over results

Call knowledge.search() and iterate the results list.
2

Convert to a plain dict

Use to_legacy_format() or to_dict() when you need raw data.

How It Works

Knowledge.search() normalises every backend into one typed SearchResult.

The SearchResult shape

The container holds the ranked items plus search-level metadata.

The SearchResultItem shape

Each item carries the content, its score, and optional source hints.

Common Patterns

Show top-N with scores

Convert to legacy dict format

Handle any backend shape

Custom backends can return a typed SearchResult, a legacy dict, or a plain list. Normalise them with one call.

Best Practices

An empty SearchResult is still a dataclass instance, so it is always truthy.
metadata is never None on SearchResult or SearchResultItem — it defaults to an empty dict, so you never need to guard against None.
Older code that expects mem0-shaped {"results": [...]} dicts stays compatible.
Typed attributes are clearer and safer than dict lookups.

Knowledge Overview

Give agents access to your documents

Knowledge CLI

Search from the command line