FactGraph (entry point and namespace map)#
The FactGraph class is the SDK entry point: it owns the compiled schema and the append-only ledger, and exposes every user-facing operation through a set of fg.* namespaces. FactGraph is a literal alias of SDKStore (FactGraph = SDKStore); both names refer to the same class and are exported from factgraph.sdk.
FactGraph#
The main SDK graph object. It holds the compiled schema (schema_ir), an append-only Ledger, an optional workspace path, and the namespace managers reachable as properties. Instances are normally built with the classmethod constructors below rather than by calling the class directly.
Attributes (read-only properties):
| Property | Type | Description |
|---|---|---|
store | Store | The underlying runtime store. |
ledger | Ledger | The append-only ledger backing this graph. |
schema_ir | dict[str, Any] | The compiled schema intermediate representation. |
__repr__ returns SDKStore(entities=<count>, schema=<digest>).
FactGraph.create(schema_classes, *, ledger=None, ledger_path=None, path=None, artifact_store_root=None, registry_root=None, registry=None, default_row_format=None) -> SDKStore#
Creates a FactGraph from Python Entity classes. This is the normal SDK constructor. Pass path= when the graph should own a durable workspace that can later be saved with fg.save_workspace() and restored with FactGraph.load_workspace(...).
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
schema_classes | list[type[Entity]] | required | Non-empty list of Entity subclasses. |
ledger | Ledger | None | None | Optional existing ledger object. Mutually exclusive with ledger_path. |
ledger_path | str | None | None | Optional SQLite ledger path. Mutually exclusive with ledger. |
path | str | Path | None | None | Optional workspace directory. When given, the schema object is written into the workspace and the ledger path is resolved from it. |
artifact_store_root | str | None | None | Optional artifact sidecar root. |
registry_root | str | Path | None | None | Removed. Raises SDKStoreError immediately if provided. Pass the workspace path via path= only. |
registry | Any | None | None | Removed. Raises SDKStoreError immediately if provided. |
default_row_format | str | None | None | Optional default output row format for rule evaluation. |
Returns: SDKStore bound to the compiled schema.
Raises:
SDKStoreErrorifregistry_root=orregistry=is provided.SDKStoreErrorifledger_pathconflicts with the ledger path implied bypath.SDKStoreErrorif bothledgerandledger_pathare provided.SDKStoreErrorif the workspace schema object cannot be written, or if the schema classes are invalid.
Example:
from factgraph.sdk import FactGraph
fg = FactGraph.create(schema_classes=[User])With a durable workspace:
fg = FactGraph.create(schema_classes=[User], path="/tmp/my-workspace")
fg.save_workspace()FactGraph.from_schema_classes(classes, *, ledger=None, ledger_path=None, artifact_store_root=None, registry_root=None, registry=None, default_row_format=None) -> SDKStore#
Creates a FactGraph from Entity classes without binding a workspace path. Equivalent to create minus the path= workspace handling.
Parameters: as for create, except the first positional parameter is named classes and there is no path parameter.
Returns: SDKStore bound to the compiled schema.
Raises: SDKStoreError if registry_root= or registry= is provided, or on invalid ledger combinations or schema classes.
fg = FactGraph.from_schema_classes([User])FactGraph.load_workspace(path, *, schema_classes=None, default_row_format=None) -> SDKStore#
Loads a saved FactGraph workspace from disk. The load restores the ledger and validates the workspace schema digest against the supplied schema_classes. Class-less dynamic load is not supported.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
path | str | Path | required | Workspace directory created by fg.save_workspace(...). |
schema_classes | list[type[Entity]] | None | None | Entity classes matching the saved workspace schema. Required: passing None raises SDKStoreError. |
default_row_format | str | None | None | Optional default output row format. |
Returns: SDKStore bound to the restored ledger.
Raises:
SDKStoreErrorifschema_classesisNone.SDKStoreErrorif the workspace contains a legacyregistry/marker (runpython -m factgraph migrate-workspace <path>first).SDKStoreErrorif the workspace schema digest does not match the compiledschema_classes.
fg = FactGraph.load_workspace("/tmp/my-workspace", schema_classes=[User])See persist-and-reload-a-workspace.md and persistence.md.
FactGraph.attach(db, *, schema_classes, view=None, default_row_format=None, **kwargs) -> SDKStore#
Database-owned lifecycle. Attaches a FactGraph to an existing Database. The compiled schema_classes digest must match db.schema_digest. Attached runtimes route writes only through fg.commit_assertions(...); the namespace write methods (fg.fields.set, fg.fields.add, fg.entities.create, etc.) are not available on attached runtimes.
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
db | Database | required | The Database instance to attach to. |
schema_classes | list[type[Entity]] | required | Entity classes whose compiled digest must equal db.schema_digest. |
view | DatabaseFrozenAssertionSet | None | None | Optional durable Database view from db.create_view(...). When given, the attached runtime is read-only. |
default_row_format | str | None | None | Optional default output row format. |
**kwargs | Any | none accepted | Any keyword raises SDKStoreError. Rejected names include artifact_store_root, ledger, ledger_path, path, policy, registry, registry_root, rules, workspace_path. |
Returns: SDKStore attached to the Database (writable when view is None, read-only when view is given).
Raises:
SDKStoreErrorifdbis not aDatabaseinstance.SDKStoreErrorif any keyword argument is passed via**kwargs.SDKStoreErrorif the compiled schema digest does not matchdb.schema_digest.SDKStoreErrorifviewis supplied but is not a Database-anchoredDatabaseFrozenAssertionSet, or itsdb_id,schema_digest, orbase_tx_iddoes not match the Database.
fg.commit_assertions(assertions) -> CommitResult#
Routes a sequence of writes through the attached Database. Available only on FactGraph.attach(db) runtimes.
Parameters:
assertions(Sequence[AssertionInput]): the assertions to commit.
Returns: CommitResult.
Raises:
SDKStoreErrorif the runtime is not attached (usefg.fields.set/fg.fields.addinstead).SDKStoreErrorif the runtime is attached with aview(view-attached runtimes are read-only).
fg.save_workspace(path=None) -> dict[str, Any]#
Persists the graph as a FactGraph workspace. A workspace contains the ledger, schema metadata, the authoring registry, and a workspace manifest.
Parameters:
path(str | Path | None, defaultNone): the workspace directory. If omitted, the graph must already be bound to a workspace path throughFactGraph.create(path=...)or an earlierfg.save_workspace(path).
Returns: dict[str, Any] with keys path (workspace root) and manifest (manifest path), both as strings.
Raises:
SDKStoreErrorif no workspace path is bound and none is passed.SDKStoreErrorif called on an attached runtime.
See persistence.md.
fg.batch(*, meta=None)#
Returns a batch transaction context manager (SDKBatchTx) that groups multiple writes.
Parameters:
meta(dict[str, Any] | None, defaultNone): metadata applied to the batch.
Raises: SDKStoreError if called on an attached runtime.
with fg.batch() as tx:
...fg.add_schema_classes(*schema_class_args, schema_classes=None) -> SchemaAddResult#
Registers new entity types or extends existing ones additively. Accepts either positional Entity subclasses or the schema_classes= keyword list, but not both.
Parameters:
*schema_class_args(type[Entity]): one or moreEntitysubclasses passed positionally.schema_classes(list[type[Entity]] | None, defaultNone): the additions as a list.
Returns: SchemaAddResult.
Raises:
SDKStoreErrorif both positional classes andschema_classes=are given.SchemaConflictError/SchemaNonAdditiveErroron non-additive schema changes.SDKStoreErrorif called on an attached runtime.
See schema.md.
Namespace map#
User-facing operations are grouped into fg.* namespaces, each a read-only manager exposed as a property on the graph. Assigning to a namespace attribute (for example fg.entities.foo = ...) raises FrozenSnapshotError.
| Namespace | Access | Purpose | Reference |
|---|---|---|---|
fg.schema | property | Register and extend schema classes (register, extend, apply); also exposes ingest and validate_provenance. | schema.md |
fg.entities | property | Layer 1 entity macros: create, delete, get, where, match, ref, exists, edit. | three-layer-api.md, write-and-read-facts.md, find-and-match-entities.md |
fg.fields | property | Layer 2 field-cell operations keyed by Field descriptor and e_ref: set, add, get, retract, delete. | three-layer-api.md |
fg.assertions | property | Layer 3 assertion records keyed by asrt_id: by_id, by_ids, active, all, field, where, retract. | three-layer-api.md |
fg.eval | property | Rule and RuleExpr evaluation: evaluate (and the legacy evaluate_candidates). | evaluation.md |
fg.audit | property | Post-hoc audit: explain, conflicts, diff_proof_frames. | audit.md, trace-and-audit-a-fact.md |
fg.rules | property | Read-only rule and derivation structure inspection: inspect. | rules.md |
fg.meta | property | Read-only runtime introspection: capabilities(). | three-layer-api.md |
fg.package | property | Package export and execution: export_package, run_package. | engines-and-semantics.md |
fg.assertion_views | property | Named frozen assertion-id selections: create, update, delete, get, list. | three-layer-api.md |
Two namespace items named in earlier surfaces resolve here:
ingestis reached asfg.schema.ingest(data, *, meta=None, allow_sensitive_meta=False), not a top-levelfg.ingestnamespace.batchis the methodfg.batch(*, meta=None)documented above, not a property namespace.
fg.inferences exists for forward compatibility but exposes no public methods. The legacy accept / CandidateSet / Inference / EmitSpec / Pred / Query flow is not covered here.
For errors raised across these namespaces, see errors.md.