Smritea

Python SDK

Reference for the Smritea Python SDK. Install it, create a client, and call add, search, get and delete.

Ask AI about this page

The Python SDK is the smritea-sdk package. It gives you a synchronous SmriteaClient with four calls: add, search, get and delete. It needs Python 3.9 or later.

Install

pip install smritea-sdk

Import the client and the scope type from smritea:

from smritea import MemoryScope, SmriteaClient

Create a client

SmriteaClient takes your API key and your app ID. You must pass both. Create them in the dashboard; see Create an app and API key.

ArgumentTypeDefaultDescription
api_keystrnoneThe API key, in the form smrt_<prefix>_<secret>.
app_idstrnoneThe app ID, in the form app_....
base_urlstrhttps://api-us.smritea.aiThe host for your region. See Regions.
max_retriesint2Retries after a rate limit. 0 turns retries off.
trace_idstr or NoneNoneA trace ID to send with each call. A bad value raises ValueError.
http_clienturllib3.PoolManager or NoneNoneYour own HTTP client. Set timeouts and proxies here.

The client has no timeout argument. Set a timeout on the http_client you pass in.

Methods

The table lists the client methods and what each one returns.

MethodReturnsDescription
add(content, *, scope, metadata, event_occurred_at, relative_standing)MemoryCreationResultExtracts and stores memories from the text.
search(query, *, ...)list[SearchResult]Finds memories that match the query.
get(memory_id)MemoryReturns one memory by ID.
delete(memory_id)NoneDeletes one memory by ID.

The get_all method exists but raises NotImplementedError. Use search to find memories.

Add options

The table lists the options that add accepts.

OptionTypeDescription
scopeMemoryScopeWho and what the memory belongs to.
metadatadictValues must be strings, integers or floats.
event_occurred_atstrAn RFC 3339 string, such as 2026-11-05T10:00:00Z. Smritea uses it to resolve words like "yesterday" and to date the memory.
relative_standingRelativeStandingThe importance and decay settings for the memory.

Build a RelativeStanding with importance, decay_factor and decay_function. Import it from smritea.types, because the top-level package does not export it. importance ranges from 0 to 1. decay_factor is 0 or higher. decay_function takes exponential, gaussian or linear. Decay affects quick_search, context_aware_search and deep_search with temporal or rrf_temporal; importance affects deep_search with rrf or rrf_temporal.

Search options

The table lists the options that search accepts.

OptionTypeDescription
scopeMemoryScopeLimits results to this scope.
speaker_actor_idstrWho is asking. It never filters results.
limitintMost results to return. Omit it to use the app default.
methodstrquick_search, deep_search or context_aware_search.
reranker_typestrApplies to deep_search only. See Search methods and rerankers.
graph_depthintA value from 1 to 5.
valid_atstrAn RFC 3339 time. Returns memories valid at that moment.
from_time, to_timestrAn RFC 3339 range. Use either one alone for an open range.
metadata_filterdictSee Filter by metadata.

You cannot combine valid_at with from_time or to_time.

Memory scope

MemoryScope decides who a memory belongs to. Every field is optional. See Memory scope for the rules.

FieldDescription
actor_idThe actor's ID, up to 64 characters. Set it with actor_type.
actor_typeuser, agent or system.
actor_nameA display name, up to 255 characters. Used by add only.
conversation_idA conversation ID, up to 64 characters.
source_typeconversation, document or api. Defaults to api.
participant_idsA list of one or more IDs. Search only.

Results

search returns a list of SearchResult. Each one has a score and a memory.

FieldDescription
scoreThe relevance score.
memory.contentThe memory text.
memory.idThe memory ID.
memory.metadataThe metadata you stored.
memory.scopeThe scope fields of the memory.
memory.active_from, memory.active_toWhen the memory is valid.

add returns a MemoryCreationResult.

FieldDescription
memoriesThe memories the call stored. Empty when explicit_skip is true.
facts_extractedHow many facts the text held.
skipped_countHow many facts the call skipped.
updated_countHow many existing memories the call updated.
explicit_skipTrue when nothing was worth storing.

get returns a Memory. It has id, app_id, content, created_at, updated_at, active_from, active_to, metadata and scope.

Exceptions

Every error derives from SmriteaError. The SDK picks the class from the HTTP status.

ExceptionHTTP statusWhen
SmriteaBadRequestError400A parameter is invalid.
SmriteaUnauthorizedError401The API key is missing or wrong.
SmriteaPaymentRequiredError402Your plan quota has run out.
SmriteaForbiddenError403The key cannot do this.
SmriteaNotFoundError404The memory does not exist.
SmriteaConflictError409The request conflicts with current state.
SmriteaUnprocessableError422The request is well formed but not allowed.
SmriteaTooManyRequestsError429You hit a rate limit.
SmriteaErrorotherAny other failure, including 5xx.

Each exception has http_status, code, retryable, body and request_id. SmriteaTooManyRequestsError also has retry_after, in seconds.

The SDK retries a 429 up to max_retries times. It never retries a 402. For every code, see Errors and codes.

Example

This script stores a memory, searches for it and handles the failures.

import os

from smritea import (
    MemoryScope,
    SmriteaClient,
    SmriteaError,
    SmriteaPaymentRequiredError,
    SmriteaTooManyRequestsError,
)

client = SmriteaClient(
    api_key=os.environ["SMRITEA_API_KEY"],
    app_id=os.environ["SMRITEA_APP_ID"],
)
alice = MemoryScope(actor_id="alice", actor_type="user")

try:
    client.add("Alice is vegetarian and loves hiking", scope=alice)
    results = client.search("What does Alice eat?", scope=alice)
    for r in results:
        if r.memory:
            print(f"{r.score:.2f}  {r.memory.content}")
except SmriteaPaymentRequiredError:
    print("Plan quota used up (402)")
except SmriteaTooManyRequestsError as e:
    print(f"Rate limited. Retry in {e.retry_after}s")
except SmriteaError as e:
    print(e.http_status, e.code, e.request_id)

Don't see your language? Request it on GitHub. Search existing requests first and add a 👍 if yours is there. That helps the Smritea team rank requests.

Last updated on