Python client library for the Xmemory API
Project description
xmemory
Python client library for the Xmemory API.
Quick start
from xmemory import xmemory_instance
mem = xmemory_instance(
url="https://api.xmemory.ai", # or set XMEM_API_URL env var
instance_id="<your-instance-id>",
token="<your-token>", # or set XMEM_AUTH_TOKEN env var
)
mem.write("Alice is a software engineer who loves Python.")
result = mem.read("What does Alice do?")
print(result.reader_result)
Async quick start
import asyncio
from xmemory import async_xmemory_instance
async def main():
async with async_xmemory_instance(instance_id="<your-instance-id>") as mem:
await mem.write("Alice is a software engineer who loves Python.")
result = await mem.read("What does Alice do?")
print(result.reader_result)
asyncio.run(main())
Configuration
| Parameter | Env var | Default | Description |
|---|---|---|---|
url |
XMEM_API_URL |
https://api.xmemory.ai |
Base URL of the Xmemory API |
instance_id |
— | None |
Instance to read/write against |
token |
XMEM_AUTH_TOKEN |
None |
Bearer token for authentication |
timeout |
— | 60 |
Default request timeout in seconds |
Context managers
Both clients support the context manager protocol and close the underlying HTTP connection on exit.
# sync
with xmemory_instance(instance_id="abc") as mem:
mem.write("...")
# async
async with async_xmemory_instance(instance_id="abc") as mem:
await mem.write("...")
External HTTP client
You can pass your own httpx.Client (or httpx.AsyncClient). The client will not be closed when
the Xmemory instance is closed, giving you full control over its lifecycle.
import httpx
from xmemory import XmemoryAPI
http = httpx.Client(base_url="https://api.xmemory.ai", timeout=30)
mem = XmemoryAPI(http_client=http, instance_id="abc")
Methods
check_health() → None
Verify that the Xmemory API is reachable. Raises XmemoryHealthCheckError on failure.
try:
mem.check_health()
except XmemoryHealthCheckError as e:
print(f"API is unreachable: {e}")
create_instance(schema_text, schema_type, *, timeout=None) → CreateInstanceResponse
Create a new instance from a schema. On success the new instance_id is saved automatically
and used for subsequent calls.
from xmemory import SchemaType
resp = mem.create_instance(schema_yml, SchemaType.YML)
resp = mem.create_instance(schema_json, SchemaType.JSON)
print(resp.instance_id)
get_schema(*, timeout=None) → GetSchemaResponse
Fetch the current schema of the active instance.
resp = mem.get_schema()
print(resp.schema_yaml)
update_schema(schema_text, schema_type, *, timeout=None) → bool
Update the schema of the active instance. Returns True on success.
ok = mem.update_schema(new_schema_yml, SchemaType.YML)
generate_schema(schema_description, *, old_schema_yml=None, timeout=None) → GenerateSchemaResponse
Ask the API to generate a YML schema from a plain-text description.
Optionally pass old_schema_yml to refine an existing schema.
resp = mem.generate_schema("People with name, role, and location.")
print(resp.generated_schema)
read(query, *, read_mode=ReadMode.SINGLE_ANSWER, timeout=None) → ReadResponse
Query the instance and get a structured answer.
resp = mem.read("Who is on the team?")
print(resp.reader_result)
Use read_mode to control the response format:
from xmemory import ReadMode
resp = mem.read("Show people and companies", read_mode=ReadMode.XRESPONSE)
write(text, *, extraction_logic=ExtractionLogic.DEEP, timeout=None) → WriteResponse
Extract structured objects from text and persist them to the instance.
from xmemory import ExtractionLogic
resp = mem.write("Bob joined the team on Monday as a designer.")
resp = mem.write("...", extraction_logic=ExtractionLogic.FAST)
print(resp.cleaned_objects)
write_async(text, *, extraction_logic=ExtractionLogic.DEEP, timeout=None) → AsyncWriteResponse
Submit a write job and return immediately with a write_id for polling.
Useful when you don't want to block on a potentially slow extraction.
resp = mem.write_async("Bob joined the team on Monday as a designer.")
write_id = resp.write_id
write_status(write_id, *, timeout=None) → WriteStatusResponse
Poll the status of a job submitted via write_async.
from xmemory import WriteQueueStatus
status = mem.write_status(write_id)
if status.write_status == WriteQueueStatus.COMPLETED:
print("Done!")
extract(text, *, extraction_logic=ExtractionLogic.DEEP, timeout=None) → ExtractionResponse
Extract structured objects from text without writing them to the instance.
resp = mem.extract("Carol is a manager based in Berlin.")
print(resp.objects_extracted)
Async methods
AsyncXmemoryAPI exposes the same methods as XmemoryAPI, all as coroutines.
Use await for each call and async with or await mem.aclose() to clean up.
from xmemory import async_xmemory_instance, ExtractionLogic, ReadMode
async with async_xmemory_instance(instance_id="abc") as mem:
await mem.check_health()
await mem.write("Alice is an engineer.", extraction_logic=ExtractionLogic.REGULAR)
result = await mem.read("What does Alice do?", read_mode=ReadMode.SINGLE_ANSWER)
# async write with polling
job = await mem.write_async("Bob joined the team.")
status = await mem.write_status(job.write_id)
Error handling
All errors raise XmemoryAPIError (or its subclass XmemoryHealthCheckError for connectivity failures).
XmemoryAPIError carries an optional .status attribute with the HTTP status code.
from xmemory import XmemoryAPIError, XmemoryHealthCheckError, xmemory_instance
mem = xmemory_instance(url="http://localhost:8000", instance_id="abc")
try:
mem.check_health()
except XmemoryHealthCheckError as e:
print(f"Could not reach the API: {e}")
try:
resp = mem.read("something")
except XmemoryAPIError as e:
print(f"API error (HTTP {e.status}): {e}")
Package publishing to pip
python -m pip install --upgrade build twine
python -m build
# test with test.pypi.org (separate account and API key required)
python -m twine upload --repository testpypi dist/*
# publish the real version when ready
python -m twine upload dist/*
# test the package
pip install xmemory-ai
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xmemory_ai-0.3.0.tar.gz.
File metadata
- Download URL: xmemory_ai-0.3.0.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83633625690a4423175ab2f99c0630189537bf83bd1707dd2bc7283590fd7891
|
|
| MD5 |
6f72d350cb6dd0d58deb1bfe24416fa6
|
|
| BLAKE2b-256 |
197c4c5e1366bca964f3c57308ae2eae4c0fd627ce26083aba9e27d59b870b67
|
File details
Details for the file xmemory_ai-0.3.0-py3-none-any.whl.
File metadata
- Download URL: xmemory_ai-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d28aefc8e3c6318a4ea7ea0fdfd7bd15e3ebc8efa62f6821bd480689c917768
|
|
| MD5 |
07ce6ae698f07f915ad59d7bf3ed802a
|
|
| BLAKE2b-256 |
88eb93553b53de1f7a4922616498024c2bea948212c00bd7eb41891fafab71c3
|