Lightweight Python SDK for Cognee - AI Memory Platform
Project description
Cognee Python SDK
Lightweight, type-safe, and fully asynchronous Python SDK for Cognee - an AI memory platform that transforms documents into persistent and dynamic knowledge graphs.
Features
- 🚀 Lightweight: Only ~5-10MB (vs 500MB-2GB for full cognee library)
- 🔒 Type Safe: Full type hints with Pydantic validation
- ⚡ Async First: Fully asynchronous API with
httpx - 🛡️ Error Handling: Comprehensive error handling with retry mechanism
- 📁 File Upload: Support for multiple file formats and input types
- 🔌 WebSocket: Optional WebSocket support for real-time progress updates
- 🔄 Retry Logic: Automatic retry with exponential backoff
- 📊 Batch Operations: Support for batch data operations
Installation
pip install cognee-sdk
Optional Dependencies
For WebSocket support:
pip install cognee-sdk[websocket]
Quick Start
import asyncio
from cognee_sdk import CogneeClient, SearchType
async def main():
# Initialize client
client = CogneeClient(
api_url="http://localhost:8000",
api_token="your-token-here" # Optional
)
try:
# Add data
result = await client.add(
data="Cognee turns documents into AI memory.",
dataset_name="my-dataset"
)
print(f"Added data: {result.data_id}")
# Process data
cognify_result = await client.cognify(datasets=["my-dataset"])
print(f"Cognify status: {cognify_result.status}")
# Search
results = await client.search(
query="What does Cognee do?",
search_type=SearchType.GRAPH_COMPLETION
)
for result in results:
print(result)
finally:
await client.close()
if __name__ == "__main__":
asyncio.run(main())
API Overview
Core Operations
- Data Management:
add(),update(),delete() - Processing:
cognify(),memify() - Search:
search()with 19 different search types - Datasets:
list_datasets(),create_dataset(),delete_dataset() - Authentication:
login(),register(),get_current_user()
Advanced Features
- WebSocket:
subscribe_cognify_progress()for real-time updates - Batch Operations:
add_batch()for bulk data operations - Visualization:
visualize()for graph visualization - Sync:
sync_to_cloud(),get_sync_status()for cloud synchronization
Examples
See the examples/ directory for more examples:
- Basic Usage - Core functionality
- File Upload - Different file upload methods
- Async Operations - Concurrent operations
- Search Types - All search types
API Reference
CogneeClient
Main client class for interacting with Cognee API.
client = CogneeClient(
api_url="http://localhost:8000",
api_token="your-token", # Optional
timeout=300.0, # Request timeout
max_retries=3, # Retry attempts
retry_delay=1.0 # Initial retry delay
)
Search Types
Available search types:
SearchType.GRAPH_COMPLETION- Graph-based completion (default)SearchType.RAG_COMPLETION- RAG-based completionSearchType.CHUNKS- Chunk searchSearchType.SUMMARIES- Summary searchSearchType.CODE- Code searchSearchType.CYPHER- Cypher query- And 13 more types...
See models.py for the complete list.
Error Handling
The SDK provides specific exception types:
from cognee_sdk import CogneeClient
from cognee_sdk.exceptions import (
AuthenticationError,
NotFoundError,
ValidationError,
ServerError,
)
try:
await client.search("query")
except AuthenticationError:
print("Authentication failed")
except NotFoundError:
print("Resource not found")
except ValidationError:
print("Invalid request")
except ServerError:
print("Server error")
Requirements
- Python 3.10+
- Cognee API server running (see Cognee documentation)
Development
Setup
# Clone the repository
git clone https://github.com/your-org/cognee-sdk.git
cd cognee-sdk
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -e ".[dev]"
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=cognee_sdk --cov-report=html
# Run specific test file
pytest tests/test_client.py
Code Quality
# Format code
ruff format .
# Check code
ruff check .
# Type checking
mypy cognee_sdk/
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please see our Contributing Guide for details.
Support
Changelog
See CHANGELOG.md for version history.
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 cognee_sdk-0.1.1.tar.gz.
File metadata
- Download URL: cognee_sdk-0.1.1.tar.gz
- Upload date:
- Size: 36.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ed5e1d14c2ee68385beb33d50e45e316d57a983b98953d7621e9bac62bbaf4b
|
|
| MD5 |
531dfd4634eda7bc088887707927e983
|
|
| BLAKE2b-256 |
ba98636148f226bf84d7d6ac4449c2d855d7aa512755daf9ab4fef9191bab5c3
|
File details
Details for the file cognee_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cognee_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a50fe227589a7eb82e6b81a107857d22ba5d63334dbe80d6b7e257c035bdd5ce
|
|
| MD5 |
0db1918c3963cdb5aab3e2bf4350d7ea
|
|
| BLAKE2b-256 |
39396da7cf94c395c414cc008139f37e60f0622132321258630443244bbfce54
|