File size: 6,885 Bytes
0ae3f27 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | ---
title: Memory Export
description: 'Export memories in a structured format using customizable Pydantic schemas'
---
## Overview
The Memory Export feature allows you to create structured exports of memories using customizable Pydantic schemas. This process enables you to transform your stored memories into specific data formats that match your needs. You can apply various filters to narrow down which memories to export and define exactly how the data should be structured.
## Creating a Memory Export
To create a memory export, you'll need to:
1. Define your schema structure
2. Submit an export job
3. Retrieve the exported data
### Define Schema
Here's an example schema for extracting professional profile information:
```json
{
"$defs": {
"EducationLevel": {
"enum": ["high_school", "bachelors", "masters"],
"title": "EducationLevel",
"type": "string"
},
"EmploymentStatus": {
"enum": ["full_time", "part_time", "student"],
"title": "EmploymentStatus",
"type": "string"
}
},
"properties": {
"full_name": {
"anyOf": [
{
"maxLength": 100,
"minLength": 2,
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "The professional's full name",
"title": "Full Name"
},
"current_role": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Current job title or role",
"title": "Current Role"
}
},
"title": "ProfessionalProfile",
"type": "object"
}
```
### Submit Export Job
You can optionally provide additional instructions to guide how memories are processed and structured during export using the `export_instructions` parameter.
<CodeGroup>
```python Python
# Basic export request
filters = {"user_id": "alice"}
response = client.create_memory_export(
schema=json_schema,
filters=filters
)
# Export with custom instructions and additional filters
export_instructions = """
1. Create a comprehensive profile with detailed information in each category
2. Only mark fields as "None" when absolutely no relevant information exists
3. Base all information directly on the user's memories
4. When contradictions exist, prioritize the most recent information
5. Clearly distinguish between factual statements and inferences
"""
filters = {
"AND": [
{"user_id": "alex"},
{"created_at": {"gte": "2024-01-01"}}
]
}
response = client.create_memory_export(
schema=json_schema,
filters=filters,
export_instructions=export_instructions # Optional
)
print(response)
```
```javascript JavaScript
// Basic Export request
const filters = {"user_id": "alice"};
const response = await client.createMemoryExport({
schema: json_schema,
filters: filters
});
// Export with custom instructions and additional filters
const export_instructions = `
1. Create a comprehensive profile with detailed information in each category
2. Only mark fields as "None" when absolutely no relevant information exists
3. Base all information directly on the user's memories
4. When contradictions exist, prioritize the most recent information
5. Clearly distinguish between factual statements and inferences
`;
// For create operation, using only user_id filter as requested
const filters = {
"AND": [
{"user_id": "alex"},
{"created_at": {"gte": "2024-01-01"}}
]
}
const responseWithInstructions = await client.createMemoryExport({
schema: json_schema,
filters: filters,
export_instructions: export_instructions
});
console.log(responseWithInstructions);
```
```bash cURL
curl -X POST "https://api.mem0.ai/v1/memories/export/" \
-H "Authorization: Token your-api-key" \
-H "Content-Type: application/json" \
-d '{
"schema": {json_schema},
"filters": {"user_id": "alice"},
"export_instructions": "1. Create a comprehensive profile with detailed information\n2. Only mark fields as \"None\" when absolutely no relevant information exists"
}'
```
```json Output
{
"message": "Memory export request received. The export will be ready in a few seconds.",
"id": "550e8400-e29b-41d4-a716-446655440000"
}
```
</CodeGroup>
### Retrieve Export
Once the export job is complete, you can retrieve the structured data in two ways:
#### Using Export ID
<CodeGroup>
```python Python
# Retrieve using export ID
response = client.get_memory_export(memory_export_id="550e8400-e29b-41d4-a716-446655440000")
print(response)
```
```javascript JavaScript
// Retrieve using export ID
const memory_export_id = "550e8400-e29b-41d4-a716-446655440000";
const response = await client.getMemoryExport({
memory_export_id: memory_export_id
});
console.log(response);
```
```json Output
{
"full_name": "John Doe",
"current_role": "Senior Software Engineer",
"years_experience": 8,
"employment_status": "full_time",
"education_level": "masters",
"skills": ["Python", "AWS", "Machine Learning"]
}
```
</CodeGroup>
#### Using Filters
<CodeGroup>
```python Python
# Retrieve using filters
filters = {
"AND": [
{"created_at": {"gte": "2024-07-10", "lte": "2024-07-20"}},
{"user_id": "alex"}
]
}
response = client.get_memory_export(filters=filters)
print(response)
```
```javascript JavaScript
// Retrieve using filters
const filters = {
"AND": [
{"created_at": {"gte": "2024-07-10", "lte": "2024-07-20"}},
{"user_id": "alex"}
]
}
const response = await client.getMemoryExport({
filters: filters
});
console.log(response);
```
```json Output
{
"full_name": "John Doe",
"current_role": "Senior Software Engineer",
"years_experience": 8,
"employment_status": "full_time",
"education_level": "masters",
"skills": ["Python", "AWS", "Machine Learning"]
}
```
</CodeGroup>
## Available Filters
You can apply various filters to customize which memories are included in the export:
- `user_id`: Filter memories by specific user
- `agent_id`: Filter memories by specific agent
- `run_id`: Filter memories by specific run
- `session_id`: Filter memories by specific session
- `created_at`: Filter memories by date
<Note>
The export process may take some time to complete, especially when dealing with a large number of memories or complex schemas.
</Note>
If you have any questions, please feel free to reach out to us using one of the following methods:
<Snippet file="get-help.mdx" />
|