Structured Memory
Give Extract saved records as context and keep its output for later workflow calls
Use Structured Memory to give an Extract information missing from a document or save its results for later calls. For example, an invoice workflow can read a supplier's default currency, then save the processed invoice in that supplier's bucket. A claims workflow can keep policy details and extracted correspondence in one bucket per claim.
| Term | What you choose | Invoice example |
|---|---|---|
| Bucket | How to group related entries | Bucket supplier-1042 |
| Kind | What type of record to store | Supplier profile or invoice |
| Kind version | Which schema and file requirements to use | Invoice schema v1 |
| Entry | The record you want to read or save | Invoice INV-2041 |
| Attributes | The values stored in the entry as JSON | Invoice number, amount, and currency |
| Read selector | Which entries to pass to Extract | The supplier profile in supplier-1042 |
In the web app
Open Data → Structured Memory. Buckets and kinds belong to the selected environment.
- In Buckets, select New bucket. Use a key that your workflow caller can supply, such as a supplier or case ID.
- In Kinds, select New kind. Define the JSON fields the entry can store and choose whether each entry requires a file.
- Open the bucket and select Add entry. Choose a kind, enter a key, and fill in the attributes. The dashboard uses the latest published kind version.
Use Read Memory, Write Memory, or both in a workflow.
Read saved records in a workflow
- In the Workflow Editor, add Read Memory and connect it to the Context row of the Extract.
- In Tests → Memory → Read, choose a bucket. Select All kinds to read the whole bucket, or choose a kind and optionally a specific entry.
- Run the test. Open the call details and expand Read Memory to see which selectors and entries the call captured.
For API calls, copy a read selector from a bucket or entry and pass it in memory.read.
Save Extract results in a workflow
- Connect Write Memory after an Extract and choose the kind and version to save. Use the same schema in the Extract and the kind version. Choose a version without a file requirement.
- In Tests → Memory → Write, choose an existing bucket and enter an unused key for that kind.
- Run the test. A successful Write saves a real entry. Open the call details or the bucket to find it.
To add a field or change the file requirements, open the kind and select New version. Publish when the schema and file settings are ready. Update the Write Memory node if you want it to use that version.
Worked example
The API walkthrough shows how to create a supplier profile, use it to fill in a missing invoice currency, and verify the saved invoice in a call output or webhook.
Memory URIs
Use Memory URIs to identify the entries a call reads or the entry it saves.
| Use | URI | Names |
|---|---|---|
| Read a bucket | bem://memory/buckets/{bucketKey} | Every entry in the bucket |
| Read one kind | bem://memory/buckets/{bucketKey}?kind={kindName} | Every entry of the kind in the bucket |
| Read one entry | bem://memory/buckets/{bucketKey}/entries/{entryKey}?kind={kindName} | One entry. kind is required. |
| Write an entry | bem://memory/buckets/{bucketKey}/entries/{entryKey} | The entry the Write Memory node saves |
The Write Memory function sets the kind. Pass only the bucket and entry key in memory.write. bem may normalize selectors when it saves them to the call trace.
Versions and saved entries
- A kind version fixes the schema and file requirements. Publish a new version to change either. Existing entries and Write Memory nodes keep their assigned version.
- A read selector for a kind matches entries of any version of that kind.
- To replace an entry, delete it and add it again with the same bucket, kind, and key.
- Deleting a bucket deletes all its entries and frees the bucket key for reuse.
- Read JSON attributes by key. bem may return fields in a different order than you submitted them.
How a call reads and writes
- A Read Memory call takes 1 to 10 selectors and can match up to 20 entries in total. Every named bucket and kind must exist. If all selectors return zero matches, the Extract runs without memory entries.
- bem resolves read selectors when it accepts the call. The trace stores the selectors and matching entries as
trace.readCapture. Later changes to the bucket leave that snapshot intact. Entries are sorted by kind name, then bucket key, then entry key. - Each call has one write destination. If several Write Memory nodes run, they use that bucket and entry key with their own kinds.
- Create the bucket before making the call. To save an entry, use a key that is free for that kind in the bucket.
Files
A kind version can require one file per entry. In the dashboard, select File required and choose the accepted types. In the API, set file.inputTypes to the accepted types, or omit file for entries that store JSON only.
{"inputTypes": ["pdf", "png"]}accepts only PDF and PNG files.inputTypesis required and holds at least one type.file: {}and an emptyinputTypesarray return400withMEMORY_REQUEST_INVALID.- Entry file types are
pdf,png,jpeg,heic,heif, andwebp.jpgandjfifare accepted asjpeg. - Send the file base64-encoded in
file.inputContentof a JSON body, or as thefilepart of a multipart body. - Responses return the file with a short-lived
downloadURL. Each response signs a new link. - A Write Memory function saves no file, so it can't save under a kind version that has a file rule.
- When a call selects an entry that holds a file, the call input must be one PDF or one image.
Limits
| Limit | Value |
|---|---|
| Selectors per call | 10 |
| Entries per Read Capture | 20 |
| Read Memory nodes per workflow | 1 |
| Entry file size | 50 MB |
| Entry file name | 255 bytes, no control characters |
Page size (limit) | 1 to 200, default 50 |
| Bucket key, kind name | 1 to 128 letters, digits, _, or - |
| Entry key | 1 to 256 letters, digits, _, -, or ., other than . and .. |
Violations listed in MEMORY_ATTRIBUTES_INVALID | 10 |
List and page
Lists return items sorted by key, with totalCount. To get the next page, pass the last key of the page as startingAfter. For kinds, pass the last name. The bucket list also takes endingBefore.
curl -G https://api.bem.ai/v3/structured-memory/buckets/supplier-1042/kinds/invoice/entries \
-H "x-api-key: $BEM_API_KEY" \
-d limit=200 \
-d startingAfter=INV-2041Error codes
A Structured Memory route that fails returns a body with error and code.
{
"error": "attributes do not match the kind schema: at \"\": missing properties 'currency', 'paymentTermsDays'",
"code": "MEMORY_ATTRIBUTES_INVALID"
}A workflow call whose memory input doesn't fit the workflow returns the same shape with status 400, and it runs no function. Use code to handle the response and error for details.
| Code | Status | Meaning |
|---|---|---|
MEMORY_REQUEST_INVALID | 400 | The body, a path key, limit, a cursor, a file rule, an attributesSchema that isn't a valid JSON Schema, or the call's memory input isn't valid. |
MEMORY_BUCKET_NOT_FOUND | 404 or 400 | No live bucket has the key. |
MEMORY_KIND_NOT_FOUND | 404 or 400 | No kind has the name. |
MEMORY_KIND_VERSION_NOT_FOUND | 404 or 400 | The kind has no version with the number. |
MEMORY_ENTRY_NOT_FOUND | 404 | No live entry has the bucket, kind, and key. |
MEMORY_BUCKET_KEY_TAKEN | 409 | A live bucket already has the key. |
MEMORY_KIND_NAME_TAKEN | 409 | A kind already has the name. |
MEMORY_IDENTITY_OCCUPIED | 409 | A live entry of the kind already has the key in the bucket. |
MEMORY_ATTRIBUTES_INVALID | 400 | The attributes don't match the kind version's schema. error lists up to 10 violations. |
MEMORY_FILE_INVALID | 400 | The entry file is missing, not allowed, of a type the rule refuses, not valid base64, or not a file of its type. |
MEMORY_KIND_REQUIRES_FILE | 400 | A Write Memory function names a kind version that has a file rule. |
MEMORY_INPUT_MISSING | 400 | The workflow has a Read Memory or Write Memory node, and the call lacks memory.read or memory.write. |
MEMORY_INPUT_UNEXPECTED | 400 | The call sends memory.read or memory.write, and the workflow has no node that uses it. |
MEMORY_URI_MALFORMED | 400 | A Memory URI in the call's memory input isn't valid. |
MEMORY_READ_LIMIT_EXCEEDED | 400 | The call's selectors resolve to more than 20 entries. |
MEMORY_SOURCE_TYPE_UNSUPPORTED | 400 | A workflow with a Read Memory node can't take this call input. See Read Memory. |
A not-found code returns 404 when the path names the missing item, and 400 when the body or the call names it.
Errors during a run
A Read or a Write that fails after bem accepts the call ends the call with an error event. The error event's kind names the failure, and message says what failed.
kind | Meaning |
|---|---|
memory_read_capture_not_delivered | The Extract couldn't receive the Read Capture, such as when the entry files are too large to send with the document. |
memory_attributes_invalid | The Extract output doesn't match the schema of the Write Memory function's kind version. |
memory_identity_occupied | A live entry of the kind already has the write destination's key in the bucket. |
memory_bucket_not_found | The write destination's bucket doesn't exist when the Write runs, such as after it was deleted. |
memory_write_destination_invalid | The call has no valid memory.write destination. |
memory_write_not_saved | bem couldn't save the entry. |
A failed Write saves nothing. Retrying the Write function call doesn't run the Extract again.