45  One program, two transports

Run the same program against the engine in your process and against a jammi-server, and get the same answer.

When to use this pattern. You prototype locally (file://) and deploy against a server (grpc://), or you serve several clients from one engine. jammi.connect(target) picks the transport from the target alone.

The program is cookbook/recipes/remote_session/example.py; its steps run here in order.

45.1 Watch sessions open and close

jammi.observe calls back on every session the process opens and closes, and returns the function that stops it. A program, or a notebook, uses it to find a session it forgot to close.

import tempfile

import jammi
from jammi import Capability
from jammi.errors import NotSupportedOnBackend
from jammi.testing import LiveServer
from jammi_cookbook import fixtures

CORPUS_URL = str(fixtures.path("tiny_corpus.parquet"))
MODEL = fixtures.model("tiny_bert")

events: list[str] = []
unsubscribe = jammi.observe(
    lambda handle, label: events.append(f"opened {label}"),
    lambda handle, label: events.append(f"closed {label}"),
)

45.2 The target picks the transport

jammi.connect(target) reads the transport from the target alone: file://… runs the engine in this process, grpc://… talks to a jammi-server. jammi.parse_target shows the reading.

local_dir, server_dir = tempfile.mkdtemp(), tempfile.mkdtemp()
for target in (f"file://{local_dir}", "grpc://127.0.0.1:8081"):
    print(f"{target} → {jammi.parse_target(target)}")
file:///tmp/tmpkhxeibgw → LocalTarget(artifact_dir='/tmp/tmpkhxeibgw')
grpc://127.0.0.1:8081 → RemoteTarget(endpoint='127.0.0.1:8081', tls=False)

45.3 One program

Both transports return a Session with the same methods, so one function runs unchanged against either: register the corpus, embed it, and return the five rows nearest a query.

def nearest(db) -> list[int]:
    db.add_source("corpus", url=CORPUS_URL, format="parquet")
    db.generate_embeddings(source="corpus", model=MODEL, columns=["content"], key="id")
    query = db.encode_query(model=MODEL, query="quantum error correction")
    hits = db.search("corpus", query=query, k=5).to_pylist()
    return [int(h["_row_id"]) for h in hits]

45.4 Run it embedded

A few features exist on one transport only. db.supports(capability) says which, and using one the backend lacks raises NotSupportedOnBackend: an embedded engine has no connection, so it has no session_id.

with jammi.connect(f"file://{local_dir}") as embedded:
    local_hits = nearest(embedded)
    print(f"embedded: {local_hits}")
    print(f"embedded supports audit: {embedded.supports(Capability.AUDIT)}")
    try:
        embedded.session_id
        raise AssertionError("the embedded engine has no connection id")
    except NotSupportedOnBackend as absent:
        print(f"embedded session_id: {absent}")
2026-10-03T07:47:47.057768Z  WARN jammi_kernels::admission: fused-kernel domain check failed; falling back to the eager composition op="attention_block_fused" predicate="head_dim_is_attention_block_fixed_head_dim"
embedded: [1, 12, 15, 16, 3]
embedded supports audit: True
embedded session_id: capability session_id is not supported on this backend; call `supports(session_id)` to check before invoking it

45.5 Run it against a server

LiveServer starts a real jammi-server over its own artifact directory and stops it when the block ends. While the remote session is open, jammi.open_session_labels() lists it, and jammi.describe_sessions names it.

with LiveServer(server_dir) as server, jammi.connect(server.endpoint) as remote:
    open_now = dict(jammi.open_session_labels())
    print(f"open sessions: {jammi.describe_sessions(open_now)}")
    assert len(jammi.open_sessions()) == 1, "only the remote session is open"
    remote_hits = nearest(remote)
    print(f"remote ({server.endpoint}): {remote_hits}")
    print(f"remote supports audit: {remote.supports(Capability.AUDIT)}")
    print(f"remote session_id: {remote.session_id}")
    assert remote.supports(Capability.SESSION_ID)
open sessions: '127.0.0.1:35567' (handle 3)
remote (grpc://127.0.0.1:35567): [1, 12, 15, 16, 3]
remote supports audit: False
remote session_id: 08db5196-10f9-48a8-bcb9-3114b5f4d386

45.6 One engine, one answer

Both transports ran the same engine over the same data, so they return the same rows. The journal saw both sessions open and close.

assert local_hits == remote_hits, "one engine, two transports, one answer"

unsubscribe()
print("journal:", "; ".join(events))
assert not jammi.open_session_labels(), "every session was closed"
journal: opened /tmp/tmpkhxeibgw; closed /tmp/tmpkhxeibgw; opened 127.0.0.1:35567; closed 127.0.0.1:35567; opened 127.0.0.1:35567; closed 127.0.0.1:35567